Spaces:
Sleeping
Sleeping
nisharg nargund
commited on
Commit
·
95451fd
1
Parent(s):
36fbbdf
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
import sys
|
| 6 |
+
|
| 7 |
+
# Create a Streamlit app
|
| 8 |
+
st.title("Brain Tumor Detection")
|
| 9 |
+
|
| 10 |
+
# Upload an image or multiple images
|
| 11 |
+
images = st.file_uploader("Upload MRI images of brains", type=["jpg", "jpeg", "png"], accept_multiple_files=True)
|
| 12 |
+
|
| 13 |
+
# Check if TensorFlow is available
|
| 14 |
+
if 'tensorflow' not in sys.modules:
|
| 15 |
+
st.warning("TensorFlow is not available in this environment. Please ensure that you have the correct environment activated.")
|
| 16 |
+
else:
|
| 17 |
+
# Load the TensorFlow model from the .h5 file
|
| 18 |
+
model = tf.keras.models.load_model("model.h5")
|
| 19 |
+
|
| 20 |
+
# Threshold for tumor detection
|
| 21 |
+
threshold = 0.1
|
| 22 |
+
|
| 23 |
+
if images:
|
| 24 |
+
st.write("Analyzed uploaded images...")
|
| 25 |
+
for image in images:
|
| 26 |
+
# Display the original image
|
| 27 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 28 |
+
|
| 29 |
+
# Preprocess the image
|
| 30 |
+
image = Image.open(image)
|
| 31 |
+
image = image.resize((128, 128)) # Resize to match model's input size
|
| 32 |
+
image = np.array(image)
|
| 33 |
+
image = image / 255.0 # Normalize
|
| 34 |
+
image = np.expand_dims(image, axis=0) # Add batch dimension
|
| 35 |
+
|
| 36 |
+
# Make predictions
|
| 37 |
+
predictions = model.predict(image)
|
| 38 |
+
|
| 39 |
+
# Extract the prediction probability for the positive class
|
| 40 |
+
tumor_probability = predictions[0][1]
|
| 41 |
+
|
| 42 |
+
# Calculate the average probability of tumor detection
|
| 43 |
+
average_probability = np.mean(tumor_probability)
|
| 44 |
+
|
| 45 |
+
# Check if the average probability is greater than the threshold
|
| 46 |
+
if average_probability > threshold:
|
| 47 |
+
st.write("Prediction: Tumor detected with confidence {:.2f}".format(average_probability))
|
| 48 |
+
else:
|
| 49 |
+
st.write("Prediction: No tumor detected with confidence {:.2f}".format(2 - average_probability))
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# Add a separator between images
|
| 53 |
+
st.write("---")
|
| 54 |
+
|
| 55 |
+
# User instructions
|
| 56 |
+
st.sidebar.header("Instructions")
|
| 57 |
+
st.sidebar.markdown(
|
| 58 |
+
"""
|
| 59 |
+
- Upload MRI images of brains using the file uploader.
|
| 60 |
+
- The app will analyze and provide predictions for each image.
|
| 61 |
+
- A confidence score is displayed to indicate prediction confidence.
|
| 62 |
+
- Adjust the threshold for tumor detection as needed.
|
| 63 |
+
- Explore different images to evaluate the model's performance.
|
| 64 |
+
"""
|
| 65 |
+
)
|