luigi12345 commited on
Commit
d5fb548
1 Parent(s): 8a1d3d2
Files changed (1) hide show
  1. app.py +77 -51
app.py CHANGED
@@ -12,6 +12,7 @@ import pandas as pd
12
  from datetime import datetime
13
  import os
14
  import tempfile
 
15
 
16
  # --- GlaucomaModel Class ---
17
  class GlaucomaModel(object):
@@ -183,6 +184,8 @@ def save_results(results, original_images):
183
  def main():
184
  st.set_page_config(layout="wide", page_title="Glaucoma Screening Tool")
185
 
 
 
186
  st.markdown("""
187
  <h1 style='text-align: center;'>Glaucoma Screening from Retinal Fundus Images</h1>
188
  <p style='text-align: center; color: gray;'>Upload retinal images for automated glaucoma detection and optic disc/cup segmentation</p>
@@ -197,6 +200,8 @@ def main():
197
  help="Support multiple images in PNG, JPEG formats"
198
  )
199
 
 
 
200
  st.sidebar.markdown("### Settings")
201
  max_batch = st.sidebar.number_input("Max Batch Size",
202
  min_value=1,
@@ -204,6 +209,8 @@ def main():
204
  value=20)
205
 
206
  if uploaded_files:
 
 
207
  if len(uploaded_files) > max_batch:
208
  st.warning(f"Please upload maximum {max_batch} images at once.")
209
  return
@@ -211,60 +218,79 @@ def main():
211
  st.markdown(f"Total images: {len(uploaded_files)}")
212
  st.markdown(f"Using: {'GPU' if torch.cuda.is_available() else 'CPU'}")
213
 
214
- # Initialize model
215
- model = GlaucomaModel(device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu"))
216
-
217
- # Process images
218
- images_data = []
219
- original_images = []
220
- for file in uploaded_files:
221
- try:
222
- image = Image.open(file).convert('RGB')
223
- image_np = np.array(image)
224
- images_data.append((file.name, image_np))
225
- original_images.append(image_np)
226
- except Exception as e:
227
- st.error(f"Error loading {file.name}: {str(e)}")
228
- continue
229
-
230
- progress = st.progress(0)
231
- st.write(f"Processing {len(images_data)} images...")
232
-
233
- # Process all images
234
- results = process_batch(model, images_data, progress)
235
-
236
- if results:
237
- # Show results one by one
238
- for result in results:
239
- st.markdown(f"### Results for {result['file_name']}")
240
- st.markdown(f"**Diagnosis:** {result['diagnosis']}")
241
- st.markdown(f"**Confidence:** {result['confidence']:.1f}%")
242
- st.markdown(f"**VCDR:** {result['vcdr']:.3f}")
243
-
244
- # Display images
245
- st.image(result['processed_image'], caption="Segmentation")
246
- st.image(result['cropped_image'], caption="ROI")
247
- st.markdown("---")
248
 
249
- # Generate downloads
250
- zip_data = save_results(results, original_images)
 
 
251
 
252
- st.markdown("### Download Results")
253
- st.download_button(
254
- label="Download All Results (ZIP)",
255
- data=zip_data,
256
- file_name=f"glaucoma_screening_{datetime.now().strftime('%Y%m%d_%H%M%S')}.zip",
257
- mime="application/zip"
258
- )
 
 
 
 
259
 
260
- # Simple summary
261
- st.markdown("### Summary")
262
- glaucoma_count = sum(1 for r in results if r['diagnosis'] == 'Glaucoma')
263
- normal_count = len(results) - glaucoma_count
264
- st.markdown(f"**Total Processed:** {len(results)}")
265
- st.markdown(f"**Glaucoma Detected:** {glaucoma_count}")
266
- st.markdown(f"**Normal:** {normal_count}")
267
- st.markdown(f"**Average Confidence:** {sum(r['confidence'] for r in results) / len(results):.1f}%")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
268
 
269
  if __name__ == "__main__":
 
270
  main()
 
12
  from datetime import datetime
13
  import os
14
  import tempfile
15
+ import base64
16
 
17
  # --- GlaucomaModel Class ---
18
  class GlaucomaModel(object):
 
184
  def main():
185
  st.set_page_config(layout="wide", page_title="Glaucoma Screening Tool")
186
 
187
+ print("App started") # Debug print
188
+
189
  st.markdown("""
190
  <h1 style='text-align: center;'>Glaucoma Screening from Retinal Fundus Images</h1>
191
  <p style='text-align: center; color: gray;'>Upload retinal images for automated glaucoma detection and optic disc/cup segmentation</p>
 
200
  help="Support multiple images in PNG, JPEG formats"
201
  )
202
 
203
+ print(f"Files uploaded: {uploaded_files}") # Debug print
204
+
205
  st.sidebar.markdown("### Settings")
206
  max_batch = st.sidebar.number_input("Max Batch Size",
207
  min_value=1,
 
209
  value=20)
210
 
211
  if uploaded_files:
212
+ print("Processing uploaded files") # Debug print
213
+
214
  if len(uploaded_files) > max_batch:
215
  st.warning(f"Please upload maximum {max_batch} images at once.")
216
  return
 
218
  st.markdown(f"Total images: {len(uploaded_files)}")
219
  st.markdown(f"Using: {'GPU' if torch.cuda.is_available() else 'CPU'}")
220
 
221
+ try:
222
+ # Initialize model
223
+ print("Initializing model") # Debug print
224
+ model = GlaucomaModel(device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
 
226
+ # Process images
227
+ images_data = []
228
+ original_images = []
229
+ print("Starting image processing") # Debug print
230
 
231
+ for file in uploaded_files:
232
+ try:
233
+ print(f"Processing file: {file.name}") # Debug print
234
+ image = Image.open(file).convert('RGB')
235
+ image_np = np.array(image)
236
+ images_data.append((file.name, image_np))
237
+ original_images.append(image_np)
238
+ except Exception as e:
239
+ print(f"Error processing file {file.name}: {str(e)}") # Debug print
240
+ st.error(f"Error loading {file.name}: {str(e)}")
241
+ continue
242
 
243
+ if not images_data:
244
+ st.error("No valid images to process!")
245
+ return
246
+
247
+ progress = st.progress(0)
248
+ st.write(f"Processing {len(images_data)} images...")
249
+
250
+ # Process all images
251
+ print("Starting batch processing") # Debug print
252
+ results = process_batch(model, images_data, progress)
253
+ print(f"Batch processing complete. Results: {len(results)}") # Debug print
254
+
255
+ if results:
256
+ print("Showing results") # Debug print
257
+ # Show results one by one
258
+ for result in results:
259
+ st.markdown(f"### Results for {result['file_name']}")
260
+ st.markdown(f"**Diagnosis:** {result['diagnosis']}")
261
+ st.markdown(f"**Confidence:** {result['confidence']:.1f}%")
262
+ st.markdown(f"**VCDR:** {result['vcdr']:.3f}")
263
+
264
+ # Display images
265
+ st.image(result['processed_image'], caption="Segmentation")
266
+ st.image(result['cropped_image'], caption="ROI")
267
+ st.markdown("---")
268
+
269
+ # Generate downloads
270
+ print("Generating ZIP file") # Debug print
271
+ zip_data = save_results(results, original_images)
272
+
273
+ st.markdown("### Download Results")
274
+ # Replace download_button with direct download link
275
+ filename = f"glaucoma_screening_{datetime.now().strftime('%Y%m%d_%H%M%S')}.zip"
276
+ st.markdown(
277
+ f'<a href="data:application/zip;base64,{base64.b64encode(zip_data).decode()}" download="{filename}">Download All Results (ZIP)</a>',
278
+ unsafe_allow_html=True
279
+ )
280
+
281
+ # Simple summary
282
+ st.markdown("### Summary")
283
+ glaucoma_count = sum(1 for r in results if r['diagnosis'] == 'Glaucoma')
284
+ normal_count = len(results) - glaucoma_count
285
+ st.markdown(f"**Total Processed:** {len(results)}")
286
+ st.markdown(f"**Glaucoma Detected:** {glaucoma_count}")
287
+ st.markdown(f"**Normal:** {normal_count}")
288
+ st.markdown(f"**Average Confidence:** {sum(r['confidence'] for r in results) / len(results):.1f}%")
289
+
290
+ except Exception as e:
291
+ print(f"Error in main processing: {str(e)}") # Debug print
292
+ st.error(f"An error occurred: {str(e)}")
293
 
294
  if __name__ == "__main__":
295
+ print("Starting main") # Debug print
296
  main()