| 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 |
|
|
|
|
|
|
| |
| 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: |
| |
| image_bytes = uploaded_file.read() |
| image = Image.open(sysio.BytesIO(image_bytes)).convert("RGB") |
| img_original = np.array(image) |
| |
| |
| 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) |
|
|
| |
| prediction = model.predict(img) |
| predicted_class = np.argmax(prediction) |
| |
| if predicted_class == 1: |
| st.markdown("### ⚠️ Tumor Detected! Proceeding to Segmentation...") |
|
|
| |
| predicted_mask = model_seg.predict(img)[0].squeeze() |
| predicted_mask = (predicted_mask > 0.5).astype(int) |
|
|
| |
| img_display = cv2.resize(img_original, (256, 256)) |
|
|
| |
| 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 = img_display.copy() |
| overlay[predicted_mask == 1] = [0, 255, 0] |
| st.image(overlay, caption="MRI with Predicted Tumor Overlay", use_column_width=True) |
|
|
| else: |
| st.success("✅ No Tumor Detected.") |
|
|
|
|