File size: 2,355 Bytes
37f989c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import streamlit as st
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img, img_to_array
import numpy as np
import os

# Set page config
st.set_page_config(
    page_title="Brain Tumor Detection",
    page_icon="🧠",
    layout="centered"
)

# Load the trained model
try:
    MODEL_PATH = 'model.h5'
    if not os.path.exists(MODEL_PATH):
        st.error("Model file not found. Please ensure model.h5 exists in the 'models' directory")
        st.stop()
    model = load_model(MODEL_PATH)
except Exception as e:
    st.error(f"Error loading model: {str(e)}")
    st.stop()

# Class labels
class_labels = ['pituitary', 'glioma', 'notumor', 'meningioma']

# Helper function to predict tumor type
def predict_tumor(image):
    IMAGE_SIZE = 128
    img = load_img(image, target_size=(IMAGE_SIZE, IMAGE_SIZE))
    img_array = img_to_array(img) / 255.0  # Normalize pixel values
    img_array = np.expand_dims(img_array, axis=0)  # Add batch dimension

    predictions = model.predict(img_array)
    predicted_class_index = np.argmax(predictions, axis=1)[0]
    confidence_score = np.max(predictions, axis=1)[0]

    if class_labels[predicted_class_index] == 'notumor':
        return "No Tumor", confidence_score
    else:
        return f"Tumor: {class_labels[predicted_class_index]}", confidence_score

# Main UI
st.title("Brain Tumor Detection")
st.write("Upload an MRI scan to detect the presence and type of brain tumor")

# File uploader
uploaded_file = st.file_uploader("Choose an MRI image file", type=['jpg', 'jpeg', 'png'])

if uploaded_file is not None:
    # Display the uploaded image
    st.image(uploaded_file, caption="Uploaded MRI Scan", use_container_width=True)
    
    # Add a predict button
    if st.button("Predict"):
        with st.spinner("Analyzing image..."):
            # Make prediction
            result, confidence = predict_tumor(uploaded_file)
            
            # Display results
            st.success("Analysis Complete!")
            st.write(f"**Prediction:** {result}")
            st.write(f"**Confidence:** {confidence*100:.2f}%")
            
            # Display additional information based on the result
            if "No Tumor" not in result:
                st.warning("Please consult with a healthcare professional for proper medical advice.")