Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import numpy as np
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
from tensorflow.keras.preprocessing import image
|
| 6 |
+
from tensorflow.keras.applications.vgg16 import preprocess_input
|
| 7 |
+
|
| 8 |
+
# Load your trained model
|
| 9 |
+
model = load_model('/Users/abhinavyadav/Downloads/BT(Deploy).h5') # Replace with your model's path
|
| 10 |
+
|
| 11 |
+
# Set Streamlit page config for a better layout
|
| 12 |
+
st.set_page_config(page_title="Brain Tumor Detection", page_icon="🧠", layout="centered")
|
| 13 |
+
|
| 14 |
+
# Add a title and description
|
| 15 |
+
st.title("Brain Tumor Detection 🧠")
|
| 16 |
+
st.markdown("""
|
| 17 |
+
Upload a brain MRI scan to detect whether it contains a brain tumor or not.
|
| 18 |
+
Our model uses advanced deep learning to analyze your scan and provide a prediction.
|
| 19 |
+
""")
|
| 20 |
+
|
| 21 |
+
# File uploader with custom styling
|
| 22 |
+
uploaded_file = st.file_uploader("Upload a Brain MRI Scan", type=["jpg", "png", "jpeg"], label_visibility="collapsed")
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# Function to preprocess the image
|
| 26 |
+
def preprocess_image(img):
|
| 27 |
+
img = img.resize((224, 224)) # Resize to 224x224
|
| 28 |
+
img_array = np.array(img) # Convert image to numpy array
|
| 29 |
+
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
| 30 |
+
img_array = preprocess_input(img_array) # Preprocess image for VGG16
|
| 31 |
+
return img_array
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
if uploaded_file is not None:
|
| 35 |
+
# Display the uploaded image
|
| 36 |
+
img = Image.open(uploaded_file)
|
| 37 |
+
st.image(img, caption="Uploaded MRI Scan", use_column_width=True)
|
| 38 |
+
|
| 39 |
+
# Preprocess and predict
|
| 40 |
+
try:
|
| 41 |
+
processed_image = preprocess_image(img)
|
| 42 |
+
st.write("Image successfully preprocessed!")
|
| 43 |
+
|
| 44 |
+
# Model prediction
|
| 45 |
+
prediction = model.predict(processed_image)
|
| 46 |
+
|
| 47 |
+
# Display prediction result with styling
|
| 48 |
+
st.subheader("Prediction Results")
|
| 49 |
+
if prediction[0][0] > 0.5:
|
| 50 |
+
st.markdown('<p style="font-size:18px;color:red;">⚠️ Brain Tumor Detected</p>', unsafe_allow_html=True)
|
| 51 |
+
else:
|
| 52 |
+
st.markdown('<p style="font-size:18px;color:green;">✅ No Brain Tumor Detected</p>', unsafe_allow_html=True)
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
st.error(f"Error in preprocessing or prediction: {e}")
|
| 56 |
+
|
| 57 |
+
# Add footer and additional information
|
| 58 |
+
st.markdown("""
|
| 59 |
+
---
|
| 60 |
+
**Developed with 💙 by [Abhinav]**
|
| 61 |
+
This project is aimed at helping doctors detect brain tumors from MRI scans using deep learning models.
|
| 62 |
+
""")
|
| 63 |
+
|
| 64 |
+
# Custom styling for Streamlit components
|
| 65 |
+
st.markdown("""
|
| 66 |
+
<style>
|
| 67 |
+
.css-1v0mbdj {
|
| 68 |
+
font-size: 20px;
|
| 69 |
+
font-weight: bold;
|
| 70 |
+
}
|
| 71 |
+
.css-5wyi5j {
|
| 72 |
+
background-color: #f0f0f5;
|
| 73 |
+
}
|
| 74 |
+
</style>
|
| 75 |
+
""", unsafe_allow_html=True)
|