Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from tensorflow.keras.models import load_model
|
3 |
+
from tensorflow.keras.preprocessing.image import load_img, img_to_array
|
4 |
+
import numpy as np
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Set page config
|
8 |
+
st.set_page_config(
|
9 |
+
page_title="Brain Tumor Detection",
|
10 |
+
page_icon="🧠",
|
11 |
+
layout="centered"
|
12 |
+
)
|
13 |
+
|
14 |
+
# Load the trained model
|
15 |
+
try:
|
16 |
+
MODEL_PATH = 'model.h5'
|
17 |
+
if not os.path.exists(MODEL_PATH):
|
18 |
+
st.error("Model file not found. Please ensure model.h5 exists in the 'models' directory")
|
19 |
+
st.stop()
|
20 |
+
model = load_model(MODEL_PATH)
|
21 |
+
except Exception as e:
|
22 |
+
st.error(f"Error loading model: {str(e)}")
|
23 |
+
st.stop()
|
24 |
+
|
25 |
+
# Class labels
|
26 |
+
class_labels = ['pituitary', 'glioma', 'notumor', 'meningioma']
|
27 |
+
|
28 |
+
# Helper function to predict tumor type
|
29 |
+
def predict_tumor(image):
|
30 |
+
IMAGE_SIZE = 128
|
31 |
+
img = load_img(image, target_size=(IMAGE_SIZE, IMAGE_SIZE))
|
32 |
+
img_array = img_to_array(img) / 255.0 # Normalize pixel values
|
33 |
+
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
34 |
+
|
35 |
+
predictions = model.predict(img_array)
|
36 |
+
predicted_class_index = np.argmax(predictions, axis=1)[0]
|
37 |
+
confidence_score = np.max(predictions, axis=1)[0]
|
38 |
+
|
39 |
+
if class_labels[predicted_class_index] == 'notumor':
|
40 |
+
return "No Tumor", confidence_score
|
41 |
+
else:
|
42 |
+
return f"Tumor: {class_labels[predicted_class_index]}", confidence_score
|
43 |
+
|
44 |
+
# Main UI
|
45 |
+
st.title("Brain Tumor Detection")
|
46 |
+
st.write("Upload an MRI scan to detect the presence and type of brain tumor")
|
47 |
+
|
48 |
+
# File uploader
|
49 |
+
uploaded_file = st.file_uploader("Choose an MRI image file", type=['jpg', 'jpeg', 'png'])
|
50 |
+
|
51 |
+
if uploaded_file is not None:
|
52 |
+
# Display the uploaded image
|
53 |
+
st.image(uploaded_file, caption="Uploaded MRI Scan", use_container_width=True)
|
54 |
+
|
55 |
+
# Add a predict button
|
56 |
+
if st.button("Predict"):
|
57 |
+
with st.spinner("Analyzing image..."):
|
58 |
+
# Make prediction
|
59 |
+
result, confidence = predict_tumor(uploaded_file)
|
60 |
+
|
61 |
+
# Display results
|
62 |
+
st.success("Analysis Complete!")
|
63 |
+
st.write(f"**Prediction:** {result}")
|
64 |
+
st.write(f"**Confidence:** {confidence*100:.2f}%")
|
65 |
+
|
66 |
+
# Display additional information based on the result
|
67 |
+
if "No Tumor" not in result:
|
68 |
+
st.warning("Please consult with a healthcare professional for proper medical advice.")
|