abhinavyadav11's picture
Update app.py
2cc6376 verified
import streamlit as st
from PIL import Image
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg16 import preprocess_input
# Load your trained model
model = load_model('BT(Deploy).h5') # Replace with your model's path
# Set Streamlit page config for a better layout
st.set_page_config(page_title="Brain Tumor Detection", page_icon="🧠", layout="centered")
# Add a title and description
st.title("Brain Tumor Detection 🧠")
st.markdown("""
Upload a brain MRI scan to detect whether it contains a brain tumor or not.
Our model uses advanced deep learning to analyze your scan and provide a prediction.
""")
# File uploader with custom styling
uploaded_file = st.file_uploader("Upload a Brain MRI Scan", type=["jpg", "png", "jpeg"], label_visibility="collapsed")
# Function to preprocess the image
def preprocess_image(img):
img = img.resize((224, 224)) # Resize to 224x224
img_array = np.array(img) # Convert image to numpy array
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
img_array = preprocess_input(img_array) # Preprocess image for VGG16
return img_array
if uploaded_file is not None:
# Display the uploaded image
img = Image.open(uploaded_file)
st.image(img, caption="Uploaded MRI Scan", use_container_width=True)
# Preprocess and predict
try:
processed_image = preprocess_image(img)
st.write("Image successfully preprocessed!")
# Model prediction
prediction = model.predict(processed_image)
# Display prediction result with styling
st.subheader("Prediction Results")
if prediction[0][0] > 0.5:
st.markdown('<p style="font-size:18px;color:red;">⚠️ Brain Tumor Detected</p>', unsafe_allow_html=True)
else:
st.markdown('<p style="font-size:18px;color:green;">βœ… No Brain Tumor Detected</p>', unsafe_allow_html=True)
except Exception as e:
st.error(f"Error in preprocessing or prediction: {e}")
# Add footer and additional information
st.markdown("""
---
**Developed with πŸ’™ by [Abhinav]**
This project is aimed at helping doctors detect brain tumors from MRI scans using deep learning models.
""")
# Custom styling for Streamlit components
st.markdown("""
<style>
.css-1v0mbdj {
font-size: 20px;
font-weight: bold;
}
.css-5wyi5j {
background-color: #f0f0f5;
}
</style>
""", unsafe_allow_html=True)