-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
217 lines (178 loc) · 7.53 KB
/
app.py
File metadata and controls
217 lines (178 loc) · 7.53 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import streamlit as st
from ultralytics import YOLO
import numpy as np
from PIL import Image
import io
import os
# Set page configuration
st.set_page_config(
page_title="Pneumonia Detector",
page_icon="🫁",
layout="wide"
)
# Title and description
st.title("🫁 Pneumonia Detector")
st.markdown("Upload a chest X-ray image to detect pneumonia or classify it as normal.")
# Load the trained model
@st.cache_resource
def load_model():
"""Load the trained YOLO model"""
try:
model = YOLO('runs/classify/train/weights/last.pt')
return model
except Exception as e:
st.error(f"Error loading model: {e}")
return None
# Load model
model = load_model()
if model is None:
st.error("Failed to load the model. Please check if the model file exists at 'runs/classify/train/weights/last.pt'")
st.stop()
# File uploader
uploaded_file = st.file_uploader(
"Choose a chest X-ray image",
type=['jpg', 'jpeg', 'png', 'bmp', 'tiff'],
help="Upload a chest X-ray image to detect pneumonia"
)
# Function to make prediction
def predict_image(image):
"""Make prediction on the uploaded image"""
try:
# Save the uploaded image temporarily
temp_path = "temp_uploaded_image.jpg"
image.save(temp_path, format='JPEG')
# Make prediction using file path (same as predict.py)
results = model(temp_path)
# Clean up temporary file
if os.path.exists(temp_path):
os.remove(temp_path)
# Get class names and probabilities
names_dict = results[0].names
probs_data = results[0].probs.data.tolist()
# Get the predicted class
predicted_class_idx = results[0].probs.top1
predicted_class_name = names_dict[predicted_class_idx]
predicted_confidence = results[0].probs.top1conf
return {
'class_names': names_dict,
'probabilities': probs_data,
'predicted_class': predicted_class_name,
'confidence': predicted_confidence,
'predicted_class_idx': predicted_class_idx
}
except Exception as e:
st.error(f"Error making prediction: {e}")
return None
# Main application logic
if uploaded_file is not None:
# Display the uploaded image
col1, col2 = st.columns([1, 1])
with col1:
st.subheader("📸 Uploaded Image")
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_container_width=True)
with col2:
st.subheader("🔍 Prediction Results")
# Show loading spinner while processing
with st.spinner("Analyzing image..."):
# Make prediction
result = predict_image(image)
if result:
# Display prediction results
predicted_class = result['predicted_class']
confidence = result['confidence']
class_names = result['class_names']
probabilities = result['probabilities']
# Create a more visually appealing result display
if predicted_class == "normal":
st.success(f"✅ **Prediction: {predicted_class.upper()}**")
st.metric("Confidence", f"{confidence:.2%}")
else:
st.error(f"⚠️ **Prediction: {predicted_class.upper()}**")
st.metric("Confidence", f"{confidence:.2%}")
# Display probability breakdown
st.subheader("📊 Probability Breakdown")
# Create a bar chart for probabilities
prob_data = {
'Class': list(class_names.values()),
'Probability': [f"{prob:.2%}" for prob in probabilities]
}
# Display as a table
import pandas as pd
df = pd.DataFrame(prob_data)
st.dataframe(df, use_container_width=True)
# Add some styling based on prediction
if predicted_class == "normal":
st.info("🎉 The chest X-ray appears normal. No signs of pneumonia detected.")
else:
st.warning("🫁 Pneumonia detected. Please consult with a healthcare professional for proper diagnosis and treatment.")
# Add some helpful information
st.markdown("---")
st.markdown("""
### ℹ️ About this Pneumonia Detector
This application uses a deep learning model trained on chest X-ray images to detect:
- **Normal**: No signs of pneumonia
- **Suffering**: Signs of pneumonia detected
**Note**: This is for educational/demonstration purposes only. Always consult with healthcare professionals for medical diagnosis.
""")
else:
# Show placeholder when no image is uploaded
st.info("👆 Please upload a chest X-ray image to get started!")
# Sample images section
st.markdown("---")
st.subheader("📥 Sample Images for Testing")
st.markdown("Don't have a chest X-ray image? Download these sample images to test the application:")
# Create sample images using actual training data
col1, col2 = st.columns(2)
with col1:
st.markdown("**Sample Normal X-ray**")
# Load actual normal image from training data
normal_image_path = "NORMAL2-IM-1427-0001.jpeg"
try:
normal_img = Image.open(normal_image_path)
st.image(normal_img, caption="Sample Normal X-ray", use_container_width=True)
# Create download button for normal sample
with open(normal_image_path, "rb") as file:
normal_data = file.read()
st.download_button(
label="📥 Download Normal Sample",
data=normal_data,
file_name="sample_normal_xray.jpeg",
mime="image/jpeg"
)
except Exception as e:
st.error(f"Error loading normal sample: {e}")
with col2:
st.markdown("**Sample Pneumonia X-ray**")
# Load actual pneumonia image from training data
pneumonia_image_path = "person1950_bacteria_4881.jpeg"
try:
pneumonia_img = Image.open(pneumonia_image_path)
st.image(pneumonia_img, caption="Sample Pneumonia X-ray", use_container_width=True)
# Create download button for pneumonia sample
with open(pneumonia_image_path, "rb") as file:
pneumonia_data = file.read()
st.download_button(
label="📥 Download Pneumonia Sample",
data=pneumonia_data,
file_name="sample_pneumonia_xray.jpeg",
mime="image/jpeg"
)
except Exception as e:
st.error(f"Error loading pneumonia sample: {e}")
st.markdown("---")
st.markdown("""
### 📋 Supported Image Formats
- JPG/JPEG
- PNG
- BMP
- TIFF
### 🎯 How it works
1. Upload a chest X-ray image using the file uploader above (or download sample images)
2. The AI model will analyze the image for signs of pneumonia
3. Results will show the predicted class and confidence level
4. A detailed probability breakdown will be displayed
""")
# Footer
st.markdown("---")
st.markdown("*Built with Streamlit and YOLOv8*")