medical / app.py
LovnishVerma's picture
Update app.py
7271f9e
raw
history blame
2.37 kB
import streamlit as st
import gradio as gr
import cv2
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras.preprocessing import image
# Loading Models
braintumor_model = load_model('models/brain_tumor_binary.h5')
# Configuring Streamlit
st.set_page_config(page_title="Brain Tumor Prediction App", page_icon=":brain:")
def preprocess_imgs(set_name, img_size):
set_new = []
for img in set_name:
img = cv2.resize(img, dsize=img_size, interpolation=cv2.INTER_CUBIC)
set_new.append(preprocess_input(img))
return np.array(set_new)
# Handle binary decision
def binary_decision(confidence):
return 1 if confidence >= 0.5 else 0
def predict_braintumor(img):
# If it's a NumPy array, use it directly
if isinstance(img, np.ndarray):
img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
else:
# Convert Gradio image data to bytes
img_bytes = img.read()
# Convert to NumPy array
nparr = np.frombuffer(img_bytes, np.uint8)
# Decode image
img_gray = cv2.imdecode(nparr, cv2.IMREAD_GRAYSCALE)
# Crop and preprocess the grayscale image
img_processed = preprocess_imgs([img_gray], (224, 224))
# Make prediction
pred = braintumor_model.predict(img_processed)
# Handle binary decision
confidence = pred[0][0]
return "Brain Tumor Not Found!" if binary_decision(confidence) == 1 else "Brain Tumor Found!"
def main():
st.title("Brain Tumor Prediction App")
uploaded_file = st.file_uploader("Choose an image...", type=["png", "jpg", "jpeg"])
if uploaded_file is not None:
st.image(uploaded_file, caption="Uploaded Image.", use_column_width=True)
st.write("")
st.write("Classifying...")
# Make prediction
result = predict_braintumor(uploaded_file)
# Display prediction
st.subheader("Prediction:")
st.success(result)
if __name__ == "__main__":
# Streamlit app
main()
# Gradio interface
iface = gr.Interface(
fn=predict_braintumor,
inputs="image",
outputs="text",
examples=[["examples/1_no.jpeg"], ["examples/2_no.jpeg"], ["examples/3_no.jpg"], ["examples/Y1.jpg"], ["examples/Y2.jpeg"], ["examples/Y3.jpg"]]
)
iface.launch()