|
|
|
|
|
|
|
|
import streamlit as st
|
|
|
import tensorflow as tf
|
|
|
from tensorflow.keras.preprocessing import image
|
|
|
import numpy as np
|
|
|
from PIL import Image
|
|
|
import cv2
|
|
|
|
|
|
|
|
|
MODEL_PATH = 'model.keras'
|
|
|
IMAGE_SIZE = (224, 224)
|
|
|
CLASS_NAMES = ['No Tumor', 'Tumor']
|
|
|
|
|
|
|
|
|
|
|
|
@st.cache_resource
|
|
|
def load_model():
|
|
|
try:
|
|
|
model = tf.keras.models.load_model(MODEL_PATH)
|
|
|
return model
|
|
|
except Exception as e:
|
|
|
st.error(f"Error loading model: {e}")
|
|
|
return None
|
|
|
|
|
|
|
|
|
def preprocess_image(img):
|
|
|
"""Uploaded image ko model input ke liye ready karna."""
|
|
|
|
|
|
img_array = np.array(img.convert('RGB'))
|
|
|
|
|
|
|
|
|
img_resized = cv2.resize(img_array, IMAGE_SIZE)
|
|
|
img_normalized = img_resized / 255.0
|
|
|
|
|
|
|
|
|
img_batch = np.expand_dims(img_normalized, axis=0)
|
|
|
|
|
|
return img_batch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
st.title("🧠Brain Tumor Detection System (AI Powered)")
|
|
|
|
|
|
|
|
|
model = load_model()
|
|
|
|
|
|
if model is None:
|
|
|
st.warning("Model file 'model.keras' not found. Please run 'train_model.py' first and place the file here.")
|
|
|
else:
|
|
|
|
|
|
uploaded_file = st.file_uploader(
|
|
|
"Upload a Brain MRI Image (.jpg, .png) for Classification:",
|
|
|
type=["jpg", "png", "jpeg"]
|
|
|
)
|
|
|
|
|
|
if uploaded_file is not None:
|
|
|
|
|
|
image_preview = Image.open(uploaded_file)
|
|
|
st.image(image_preview, caption='Uploaded MRI Image', use_column_width=True)
|
|
|
st.write("")
|
|
|
|
|
|
|
|
|
if st.button("🔬 Detect Tumor"):
|
|
|
st.write("Processing...")
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
processed_img = preprocess_image(image_preview)
|
|
|
|
|
|
|
|
|
prediction = model.predict(processed_img)
|
|
|
|
|
|
|
|
|
|
|
|
confidence_score = prediction[0][0] * 100
|
|
|
|
|
|
if confidence_score > 50:
|
|
|
|
|
|
result_index = 1
|
|
|
else:
|
|
|
|
|
|
result_index = 0
|
|
|
|
|
|
confidence_score = 100 - confidence_score
|
|
|
|
|
|
predicted_class = CLASS_NAMES[result_index]
|
|
|
|
|
|
|
|
|
st.subheader("✅ Prediction Results")
|
|
|
|
|
|
if predicted_class == 'Tumor':
|
|
|
st.error(f"**Prediction:** **{predicted_class}** - Immediate medical consultation is advised.")
|
|
|
else:
|
|
|
st.success(f"**Prediction:** **{predicted_class}** - Looks clear.")
|
|
|
|
|
|
|
|
|
st.info(f"**Confidence Score:** **{confidence_score:.2f}%**")
|
|
|
|
|
|
except Exception as e:
|
|
|
st.error(f"An error occurred during prediction: {e}")
|
|
|
|
|
|
else:
|
|
|
st.info("Please upload an image file to begin detection.")
|
|
|
|
|
|
|