Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import tensorflow as tf | |
| from PIL import Image | |
| import numpy as np | |
| import os | |
| # Load the saved model | |
| 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**") | |