Nood2PRO's picture
Update app.py
53aa1fe verified
import streamlit as st
import tensorflow as tf
import numpy as np
import cv2
from skimage import io
from PIL import Image
import matplotlib.pyplot as plt
import io as sysio
# Load models
model = tf.keras.models.load_model("your_cnn_model.keras", compile=False)
model_seg = tf.keras.models.load_model("model_seg30.keras", compile=False)
st.title("🧠 Brain Tumor Detection & Segmentation")
st.write("Upload an MRI scan to detect and segment tumor regions.")
uploaded_file = st.file_uploader("Choose an MRI Image", type=["png", "jpg", "jpeg"])
if uploaded_file is not None:
# Load image using skimage.io for consistency with your Colab code
image_bytes = uploaded_file.read()
image = Image.open(sysio.BytesIO(image_bytes)).convert("RGB")
img_original = np.array(image)
# Preprocessing
img = img_original.copy()
img = cv2.resize(img, (256, 256))
img = np.array(img, dtype=np.float64)
img -= img.mean()
img /= img.std()
img = np.expand_dims(img, axis=0)
# Detection
prediction = model.predict(img)
predicted_class = np.argmax(prediction)
if predicted_class == 1:
st.markdown("### ⚠️ Tumor Detected! Proceeding to Segmentation...")
# Segmentation
predicted_mask = model_seg.predict(img)[0].squeeze()
predicted_mask = (predicted_mask > 0.5).astype(int)
# Resize original for display
img_display = cv2.resize(img_original, (256, 256))
# Plot original and mask
st.pyplot(plt.figure(figsize=(10, 5)))
plt.subplot(1, 2, 1)
plt.imshow(img_display)
plt.title('Uploaded MRI Image')
plt.axis('off')
plt.subplot(1, 2, 2)
plt.imshow(predicted_mask, cmap='gray')
plt.title('Predicted Tumor Mask')
plt.axis('off')
st.pyplot()
# Overlay
overlay = img_display.copy()
overlay[predicted_mask == 1] = [0, 255, 0] # green
st.image(overlay, caption="MRI with Predicted Tumor Overlay", use_column_width=True)
else:
st.success("✅ No Tumor Detected.")