PneumoniaDetection / src /streamlit_app.py
ATllll's picture
Update src/streamlit_app.py
52f2963 verified
import streamlit as st
import tensorflow as tf
from PIL import Image
import numpy as np
import os
# Load the saved model
@st.cache_resource
def load_model():
model = tf.keras.models.load_model('pneumonia_cnn_model.keras')
return model
model = load_model()
st.title("🫁 Pneumonia Detection from Chest X-ray Images")
st.markdown("Upload your own X-ray or try one of the sample images below.")
# === Sample Image Section ===
sample_images = {
"Choose a sample image": None,
"🧍 Normal Sample": "samples/normal.jpg",
"πŸ€’ Pneumonia Sample": "samples/pneumonia.jpg"
}
selected_sample = st.selectbox("πŸ“‚ Select a sample image", list(sample_images.keys()))
uploaded_file = st.file_uploader("πŸ“ Or upload a chest X-ray image...", type=["jpg", "jpeg", "png"])
# Determine which image to use
if selected_sample != "Choose a sample image":
image_path = sample_images[selected_sample]
image = Image.open(image_path).convert("RGB")
st.image(image, caption=f'πŸ–ΌοΈ {selected_sample}', use_column_width=True)
elif uploaded_file is not None:
image = Image.open(uploaded_file).convert("RGB")
st.image(image, caption='πŸ–ΌοΈ Uploaded Image', use_column_width=True)
else:
image = None
# === Predict Button ===
if image and st.button('πŸ” Predict'):
img = image.resize((150, 150))
img_array = np.array(img) / 255.0
img_array = np.expand_dims(img_array, axis=0)
prediction = model.predict(img_array)
if prediction[0][0] > 0.5:
st.error("🩺 **Prediction: Pneumonia Detected**")
else:
st.success("βœ… **Prediction: Normal**")