medical / app.py
LovnishVerma's picture
Update app.py
5f57a29
raw
history blame
1.73 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:")
# Configuring Gradio
iface = gr.Interface(
fn="predict_braintumor",
inputs="image",
outputs="text",
live=True,
interpretation="default",
examples=[["examples/1_no.jpeg"], ["examples/2_no.jpeg"], ["examples/3_no.jpg"], ["examples/Y1.jpg"], ["examples/Y2.jpg"], ["examples/Y3.jpg"]]
)
iface.launch()
def preprocess_image(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))
return img_processed
# Handle binary decision
def binary_decision(confidence):
return 1 if confidence >= 0.5 else 0
def predict_braintumor(img):
# Preprocess the image
img_processed = preprocess_image(img)
# 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!"