Spaces:
Sleeping
Sleeping
File size: 1,614 Bytes
77610aa f295947 52f2963 f295947 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
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**")
|