Views
No views yet
yes/ and no/.(150, 150, 3)sigmoidbinary_crossentropyAdam1import streamlit as st
2import tensorflow as tf
3import numpy as np
4from tensorflow.keras.preprocessing import image
5from PIL import Image
6
7# Load the trained model
8@st.cache_resource
9def load_model():
10 return tf.keras.models.load_model('models/brain_tumor_model.h5') # Update path if needed
11
12model = load_model()
13
14# Define class labels
15class_names = ['glioma_tumor', 'meningioma_tumor', 'no_tumor', 'pituitary_tumor']
16
17# UI
18st.title("🧠 Brain Tumor Detection from MRI")
19st.write("Upload an MRI image to detect the type of brain tumor.")
20
21# Upload image
22uploaded_file = st.file_uploader("Choose an MRI image", type=["jpg", "jpeg", "png"])
23
24if uploaded_file is not None:
25 # Show image
26 img = Image.open(uploaded_file)
27 st.image(img, caption="🖼️ Uploaded Image", use_container_width=True)
28
29 # Preprocessing
30 img = img.resize((224, 224)) # ✅ Make sure it matches your model's input size
31 img_array = image.img_to_array(img)
32 img_array = np.expand_dims(img_array, axis=0) / 255.0
33
34 # Prediction
35 predictions = model.predict(img_array)
36 confidence = float(np.max(predictions)) * 100
37 predicted_class = class_names[np.argmax(predictions)]
38
39 # Output
40 st.success(f"🎯 Predicted Tumor Type: **{predicted_class}**")
41 st.info(f"📊 Model Confidence: **{confidence:.2f}%**")