muddasser's picture
Update app.py
1f53010 verified
import streamlit as st
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
from PIL import Image
# ------------------------------
# Main app
# ------------------------------
def main():
st.title("Brain Tumor Detection (MRI Images)")
# --------------------------
# Load the trained CNN model
# --------------------------
@st.cache_resource
def load_cnn_model():
return load_model("multi_modal_brain_tumor_model.h5") # Make sure this file is in the same directory
model = load_cnn_model()
# --------------------------
# Upload image
# --------------------------
uploaded_file = st.file_uploader("Upload a brain MRI image", type=["jpg", "jpeg", "png"])
if uploaded_file is not None:
img = Image.open(uploaded_file).convert("RGB")
st.image(img, caption="Uploaded Image", use_container_width=True)
# ----------------------
# Preprocess for CNN
# ----------------------
img = img.resize((128, 128)) # Update to your model input size
x = image.img_to_array(img) / 255.0
x = np.expand_dims(x, axis=0)
# ----------------------
# Predict
# ----------------------
pred = model.predict(x)
class_idx = int(np.round(pred[0][0]))
result = "Malignant" if class_idx == 1 else "Benign"
st.success(f"Prediction: {result}")
# ------------------------------
# Run the app
# ------------------------------
if __name__ == "__main__":
main()