Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +129 -0
- chest_xray_model.h5 +3 -0
- requirements.txt +0 -0
app.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
+
import requests
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
# Load the pre-trained chest X-ray classification model
|
| 9 |
+
try:
|
| 10 |
+
model = load_model('D:/1-brain_insipred/cv/chest_xray/models/chest_xray_model.h5')
|
| 11 |
+
except Exception as e:
|
| 12 |
+
st.error("Error loading model")
|
| 13 |
+
st.stop()
|
| 14 |
+
|
| 15 |
+
# Define the class names for prediction output
|
| 16 |
+
class_names = ['Normal', 'Pneumonia']
|
| 17 |
+
|
| 18 |
+
# Prediction class for encapsulating the prediction logic
|
| 19 |
+
class Prediction:
|
| 20 |
+
def __init__(self, model):
|
| 21 |
+
self.model = model
|
| 22 |
+
|
| 23 |
+
def classify_image(self, image):
|
| 24 |
+
try:
|
| 25 |
+
image = Image.fromarray(image).convert('RGB')
|
| 26 |
+
image = image.resize((512, 512))
|
| 27 |
+
image_array = np.array(image).astype(np.float32) / 255.0
|
| 28 |
+
image_array = np.expand_dims(image_array, axis=0)
|
| 29 |
+
|
| 30 |
+
predictions = self.model.predict(image_array)[0]
|
| 31 |
+
predicted_class_idx = np.argmax(predictions)
|
| 32 |
+
predicted_class = class_names[predicted_class_idx]
|
| 33 |
+
predicted_confidence = predictions[predicted_class_idx] * 100
|
| 34 |
+
|
| 35 |
+
return predicted_class, predicted_confidence, predictions
|
| 36 |
+
except Exception as e:
|
| 37 |
+
st.error("Error during classification")
|
| 38 |
+
return None, None, None
|
| 39 |
+
|
| 40 |
+
# Initialize the Prediction class
|
| 41 |
+
predictor = Prediction(model)
|
| 42 |
+
|
| 43 |
+
# Streamlit app layout
|
| 44 |
+
st.title("📊 Chest X-Ray Classification")
|
| 45 |
+
st.markdown("""
|
| 46 |
+
Upload one or more chest X-ray images or provide an image URL, and the model will classify each as either **Normal** or **Pneumonia**.
|
| 47 |
+
""")
|
| 48 |
+
|
| 49 |
+
# Input option selection
|
| 50 |
+
input_option = st.radio("Choose how to upload the image(s):", ("Upload Image(s)", "Image URL"))
|
| 51 |
+
|
| 52 |
+
# Initialize images list
|
| 53 |
+
images = []
|
| 54 |
+
|
| 55 |
+
# Patient name input
|
| 56 |
+
patient_name = st.text_input("### Patient Name")
|
| 57 |
+
|
| 58 |
+
if input_option == "Upload Image(s)":
|
| 59 |
+
uploaded_images = st.file_uploader("### Step 1: Upload Your Chest X-Ray Image(s)", type=["jpg", "jpeg", "png"],
|
| 60 |
+
accept_multiple_files=True)
|
| 61 |
+
if uploaded_images:
|
| 62 |
+
for uploaded_image in uploaded_images:
|
| 63 |
+
try:
|
| 64 |
+
image = np.array(Image.open(uploaded_image))
|
| 65 |
+
images.append((image, uploaded_image.name))
|
| 66 |
+
except Exception as e:
|
| 67 |
+
st.error("Error loading image")
|
| 68 |
+
|
| 69 |
+
elif input_option == "Image URL":
|
| 70 |
+
image_url = st.text_input("### Step 1: Enter the Image URL")
|
| 71 |
+
if image_url:
|
| 72 |
+
try:
|
| 73 |
+
response = requests.get(image_url)
|
| 74 |
+
if response.status_code == 200:
|
| 75 |
+
images.append((np.array(Image.open(BytesIO(response.content))), image_url))
|
| 76 |
+
st.markdown(f"[Image URL]({image_url})")
|
| 77 |
+
else:
|
| 78 |
+
st.error("Error fetching image from URL: Unable to retrieve the image.")
|
| 79 |
+
except Exception as e:
|
| 80 |
+
st.error("Error fetching image from URL")
|
| 81 |
+
|
| 82 |
+
# Store classification results
|
| 83 |
+
results = []
|
| 84 |
+
|
| 85 |
+
if images:
|
| 86 |
+
submit_button = st.button("Submit", key="submit")
|
| 87 |
+
if submit_button:
|
| 88 |
+
st.write("### Step 2: Review the Uploaded Image(s) and Results")
|
| 89 |
+
|
| 90 |
+
for idx, (image, image_name) in enumerate(images):
|
| 91 |
+
patient_display_name = f"[{patient_name}](#{image_name})"
|
| 92 |
+
st.write(f"#### Patient: {patient_display_name}")
|
| 93 |
+
|
| 94 |
+
col1, col2 = st.columns([2, 1])
|
| 95 |
+
with col1:
|
| 96 |
+
st.image(image, caption=image_name, use_column_width=True, clamp=True)
|
| 97 |
+
|
| 98 |
+
with col2:
|
| 99 |
+
st.subheader("Prediction Results")
|
| 100 |
+
with st.spinner("Processing..."):
|
| 101 |
+
predicted_class, predicted_confidence, predictions = predictor.classify_image(image)
|
| 102 |
+
|
| 103 |
+
if predicted_class is not None:
|
| 104 |
+
st.markdown(f"""
|
| 105 |
+
<div style="border: 2px solid #2196F3; padding: 10px; border-radius: 5px;">
|
| 106 |
+
<p style="font-size: 16px; font-weight: bold;">Predicted Class: {predicted_class}</p>
|
| 107 |
+
<p style="font-size: 16px;">Confidence: {predicted_confidence:.2f}%</p>
|
| 108 |
+
<p style="font-size: 16px; font-weight: bold;">Class Confidence Levels:</p>
|
| 109 |
+
<ul style="list-style-type: none; padding: 0;">
|
| 110 |
+
<li style="color: #4CAF50;">Normal: {predictions[0] * 100:.1f}%</li>
|
| 111 |
+
<li style="color: #F44336;">Pneumonia: {predictions[1] * 100:.1f}%</li>
|
| 112 |
+
</ul>
|
| 113 |
+
</div>
|
| 114 |
+
""", unsafe_allow_html=True)
|
| 115 |
+
|
| 116 |
+
results.append((idx + 1, patient_name, predicted_class, predicted_confidence, predictions))
|
| 117 |
+
|
| 118 |
+
st.markdown("---")
|
| 119 |
+
|
| 120 |
+
# Button to manually start a new session
|
| 121 |
+
if st.button("Start New Session"):
|
| 122 |
+
images.clear()
|
| 123 |
+
st.experimental_rerun() # Rerun the app to refresh the state
|
| 124 |
+
|
| 125 |
+
st.write("### Additional Information")
|
| 126 |
+
st.markdown("""
|
| 127 |
+
- This model is trained to differentiate between normal and pneumonia-affected chest X-rays.
|
| 128 |
+
- Confidence levels are displayed as a percentage for each class.
|
| 129 |
+
""")
|
chest_xray_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ce61eb31b48a0bdbb7e25d05329a26d4f4ac323a60723f09b8273cd0791431ea
|
| 3 |
+
size 28994168
|
requirements.txt
ADDED
|
Binary file (64.4 kB). View file
|
|
|