luigi12345 commited on
Commit
f192f0b
β€’
1 Parent(s): 131b493
Files changed (1) hide show
  1. app.py +65 -99
app.py CHANGED
@@ -112,117 +112,83 @@ def get_confidence_level(confidence):
112
  def main():
113
  st.set_page_config(layout="wide", page_title="Glaucoma Screening Tool")
114
 
115
- # Header with better styling
116
  st.markdown("""
117
  <h1 style='text-align: center;'>Glaucoma Screening from Retinal Fundus Images</h1>
118
  <p style='text-align: center; color: gray;'>Upload retinal images for automated glaucoma detection and optic disc/cup segmentation</p>
119
  """, unsafe_allow_html=True)
120
 
121
- # Sidebar with better organization
122
- with st.sidebar:
123
- st.markdown("### Upload Settings")
124
- uploaded_files = st.file_uploader("Upload Retinal Images",
125
- type=['png', 'jpeg', 'jpg'],
126
- accept_multiple_files=True,
127
- help="Support multiple images in PNG, JPEG formats")
128
-
129
- st.markdown("### Analysis Settings")
130
- st.info("πŸ“Š Set confidence threshold to filter results")
131
- confidence_threshold = st.slider(
132
- "Classification Confidence Threshold (%)",
133
- 0, 100, 70,
134
- help="Images with confidence above this threshold will be marked as reliable predictions")
135
 
136
  if uploaded_files:
137
  for uploaded_file in uploaded_files:
138
  image = Image.open(uploaded_file).convert('RGB')
139
  image_np = np.array(image).astype(np.uint8)
140
 
 
 
141
  with st.spinner(f'πŸ”„ Processing {uploaded_file.name}...'):
142
  model = GlaucomaModel(device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu"))
143
  disease_idx, disc_cup_image, vcdr, cls_conf, cup_conf, disc_conf, cropped_image = model.process(image_np)
144
 
145
- # Create expandable section for each image
146
- with st.expander(f"πŸ“Š Analysis Results: {uploaded_file.name}", expanded=True):
147
- # Image display section
148
- cols = st.columns(3)
149
- cols[0].image(image_np, caption="Original Image", use_column_width=True)
150
- cols[1].image(disc_cup_image, caption="Segmentation Overlay", use_column_width=True)
151
- cols[2].image(cropped_image, caption="Region of Interest", use_column_width=True)
152
-
153
- # Metrics section with clear separation
154
- st.markdown("---")
155
- metric_cols = st.columns(3)
156
-
157
- # Classification Results
158
- with metric_cols[0]:
159
- st.markdown("### πŸ” Classification")
160
- diagnosis = model.cls_id2label[disease_idx]
161
- is_confident = cls_conf >= confidence_threshold
162
-
163
- # Color-coded diagnosis
164
- if diagnosis == "Glaucoma":
165
- st.markdown(f"<div style='padding: 10px; background-color: #ffebee; border-radius: 5px;'>"
166
- f"<h4 style='color: #c62828;'>Diagnosis: {diagnosis}</h4></div>",
167
- unsafe_allow_html=True)
168
- else:
169
- st.markdown(f"<div style='padding: 10px; background-color: #e8f5e9; border-radius: 5px;'>"
170
- f"<h4 style='color: #2e7d32;'>Diagnosis: {diagnosis}</h4></div>",
171
- unsafe_allow_html=True)
172
-
173
- st.metric("Classification Confidence", f"{cls_conf:.1f}%")
174
- if not is_confident:
175
- st.warning("⚠️ Below confidence threshold")
176
-
177
- # Segmentation Results
178
- with metric_cols[1]:
179
- st.markdown("### 🎯 Segmentation Quality")
180
- st.metric("Optic Cup Confidence", f"{cup_conf:.1f}%")
181
- st.metric("Optic Disc Confidence", f"{disc_conf:.1f}%")
182
-
183
- # Confidence level explanation
184
- cup_level = get_confidence_level(cup_conf)
185
- disc_level = get_confidence_level(disc_conf)
186
- st.info(f"Cup Detection: {cup_level}\nDisc Detection: {disc_level}")
187
-
188
- # Clinical Metrics
189
- with metric_cols[2]:
190
- st.markdown("### πŸ“ Clinical Metrics")
191
- st.metric("Cup-to-Disc Ratio (CDR)", f"{vcdr:.3f}")
192
-
193
- # CDR interpretation
194
- if vcdr > 0.7:
195
- st.warning("⚠️ Elevated CDR (>0.7)")
196
- elif vcdr > 0.5:
197
- st.info("ℹ️ Borderline CDR (0.5-0.7)")
198
- else:
199
- st.success("βœ… Normal CDR (<0.5)")
200
-
201
- # Download section
202
- if download_confident_images:
203
- st.sidebar.markdown("---")
204
- st.sidebar.markdown("### Download Results")
205
- with zipfile.ZipFile("confident_cropped_images.zip", "w") as zf:
206
- for cropped_image, name in download_confident_images:
207
- img_buffer = io.BytesIO()
208
- Image.fromarray(cropped_image).save(img_buffer, format="PNG")
209
- zf.writestr(f"{name}_cropped.png", img_buffer.getvalue())
210
-
211
- st.sidebar.download_button(
212
- label="πŸ“₯ Download Analysis Results",
213
- data=open("confident_cropped_images.zip", "rb"),
214
- file_name="glaucoma_analysis_results.zip",
215
- mime="application/zip",
216
- help="Download cropped images and analysis results"
217
- )
218
- else:
219
- # Welcome message when no files are uploaded
220
- st.markdown("""
221
- <div style='text-align: center; padding: 50px;'>
222
- <h3>πŸ‘‹ Welcome to the Glaucoma Screening Tool</h3>
223
- <p>Upload retinal fundus images using the sidebar to begin analysis</p>
224
- </div>
225
- """, unsafe_allow_html=True)
226
-
227
- if __name__ == '__main__':
228
- main()
 
112
  def main():
113
  st.set_page_config(layout="wide", page_title="Glaucoma Screening Tool")
114
 
 
115
  st.markdown("""
116
  <h1 style='text-align: center;'>Glaucoma Screening from Retinal Fundus Images</h1>
117
  <p style='text-align: center; color: gray;'>Upload retinal images for automated glaucoma detection and optic disc/cup segmentation</p>
118
  """, unsafe_allow_html=True)
119
 
120
+ # Sidebar settings
121
+ st.sidebar.markdown("### Upload Settings")
122
+ uploaded_files = st.sidebar.file_uploader("Upload Retinal Images",
123
+ type=['png', 'jpeg', 'jpg'],
124
+ accept_multiple_files=True,
125
+ help="Support multiple images in PNG, JPEG formats")
126
+
127
+ st.sidebar.markdown("### Analysis Settings")
128
+ st.sidebar.info("πŸ“Š Set confidence threshold to filter results")
129
+ confidence_threshold = st.sidebar.slider(
130
+ "Classification Confidence Threshold (%)",
131
+ 0, 100, 70,
132
+ help="Images with confidence above this threshold will be marked as reliable predictions")
 
133
 
134
  if uploaded_files:
135
  for uploaded_file in uploaded_files:
136
  image = Image.open(uploaded_file).convert('RGB')
137
  image_np = np.array(image).astype(np.uint8)
138
 
139
+ st.markdown(f"## πŸ“Š Analysis Results: {uploaded_file.name}")
140
+
141
  with st.spinner(f'πŸ”„ Processing {uploaded_file.name}...'):
142
  model = GlaucomaModel(device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu"))
143
  disease_idx, disc_cup_image, vcdr, cls_conf, cup_conf, disc_conf, cropped_image = model.process(image_np)
144
 
145
+ # Image display section
146
+ st.subheader("Original Image")
147
+ st.image(image_np, use_column_width=True)
148
+ st.subheader("Segmentation Overlay")
149
+ st.image(disc_cup_image, use_column_width=True)
150
+ st.subheader("Region of Interest")
151
+ st.image(cropped_image, use_column_width=True)
152
+
153
+ st.markdown("---")
154
+
155
+ # Classification Results
156
+ st.markdown("### πŸ” Classification")
157
+ diagnosis = model.cls_id2label[disease_idx]
158
+ is_confident = cls_conf >= confidence_threshold
159
+
160
+ if diagnosis == "Glaucoma":
161
+ st.markdown(f"<div style='padding: 10px; background-color: #ffebee; border-radius: 5px;'>"
162
+ f"<h4 style='color: #c62828;'>Diagnosis: {diagnosis}</h4></div>",
163
+ unsafe_allow_html=True)
164
+ else:
165
+ st.markdown(f"<div style='padding: 10px; background-color: #e8f5e9; border-radius: 5px;'>"
166
+ f"<h4 style='color: #2e7d32;'>Diagnosis: {diagnosis}</h4></div>",
167
+ unsafe_allow_html=True)
168
+
169
+ st.write(f"Classification Confidence: {cls_conf:.1f}%")
170
+ if not is_confident:
171
+ st.warning("⚠️ Below confidence threshold")
172
+
173
+ # Segmentation Results
174
+ st.markdown("### 🎯 Segmentation Quality")
175
+ st.write(f"Optic Cup Confidence: {cup_conf:.1f}%")
176
+ st.write(f"Optic Disc Confidence: {disc_conf:.1f}%")
177
+
178
+ cup_level = get_confidence_level(cup_conf)
179
+ disc_level = get_confidence_level(disc_conf)
180
+ st.info(f"Cup Detection: {cup_level}\nDisc Detection: {disc_level}")
181
+
182
+ # Clinical Metrics
183
+ st.markdown("### πŸ“ Clinical Metrics")
184
+ st.write(f"Cup-to-Disc Ratio (CDR): {vcdr:.3f}")
185
+
186
+ if vcdr > 0.7:
187
+ st.warning("⚠️ Elevated CDR (>0.7)")
188
+ elif vcdr > 0.5:
189
+ st.info("ℹ️ Borderline CDR (0.5-0.7)")
190
+ else:
191
+ st.success("βœ… Normal CDR (<0.5)")
192
+
193
+ st.markdown("---")
194
+ # ... rest of the code remains the same ...