luigi12345 commited on
Commit
27efa08
1 Parent(s): d5fb548
Files changed (1) hide show
  1. app.py +44 -87
app.py CHANGED
@@ -182,115 +182,72 @@ def save_results(results, original_images):
182
 
183
  # --- Streamlit Interface ---
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>
192
- """, unsafe_allow_html=True)
193
-
194
- # Simple sidebar without columns
195
- st.sidebar.markdown("### 📤 Upload Images")
196
  uploaded_files = st.sidebar.file_uploader(
197
- "Upload Retinal Images",
198
  type=['png', 'jpeg', 'jpg'],
199
- accept_multiple_files=True,
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,
208
- max_value=100,
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
217
-
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()
 
182
 
183
  # --- Streamlit Interface ---
184
  def main():
185
+ # Use the old layout setting method
186
+ st.set_page_config(layout="wide")
187
 
188
+ # Use simple title instead of markdown
189
+ st.title("Glaucoma Screening from Retinal Fundus Images")
190
+ st.write("Upload retinal images for automated glaucoma detection and optic disc/cup segmentation")
191
 
192
+ # Sidebar using old method
193
+ st.sidebar.title("Upload Images")
194
+ st.set_option('deprecation.showfileUploaderEncoding', False) # Important for old versions
 
 
 
 
195
  uploaded_files = st.sidebar.file_uploader(
196
+ "Upload retinal images",
197
  type=['png', 'jpeg', 'jpg'],
198
+ accept_multiple_files=True
 
199
  )
200
 
201
+ # Simple explanation in sidebar
202
+ st.sidebar.markdown("""
203
+ ### Understanding Results:
204
+ - Diagnosis Confidence: AI certainty level
205
+ - VCDR: Cup to disc ratio (>0.7 high risk)
206
+ - Segmentation: Accuracy of detection
207
+ """)
208
 
209
  if uploaded_files:
210
+ st.write("Loading AI models...")
 
 
 
 
 
 
 
211
 
212
  try:
 
 
213
  model = GlaucomaModel(device=torch.device("cuda:0" if torch.cuda.is_available() else "cpu"))
214
 
 
 
 
 
 
215
  for file in uploaded_files:
216
  try:
217
+ # Process each image
218
+ st.write(f"Processing: {file.name}")
219
  image = Image.open(file).convert('RGB')
220
  image_np = np.array(image)
221
+
222
+ # Get predictions
223
+ disease_idx, disc_cup_image, vcdr, cls_conf, cup_conf, disc_conf, cropped_image = model.process(image_np)
224
+
225
+ # Display results using old methods
226
+ st.write("---")
227
+ st.write(f"Results for {file.name}")
228
+
229
+ # Show diagnosis
230
+ diagnosis = model.cls_id2label[disease_idx]
231
+ st.write(f"Diagnosis: {diagnosis}")
232
+ st.write(f"Confidence: {cls_conf:.1f}%")
233
+ st.write(f"VCDR: {vcdr:.3f}")
234
+
235
+ # Display images
236
+ st.write("Segmentation Result:")
237
+ st.image(disc_cup_image, caption="Green: Optic Disc | Red: Optic Cup")
238
+ st.write("Region of Interest:")
239
+ st.image(cropped_image, caption="ROI")
240
+
241
  except Exception as e:
242
+ st.error(f"Error processing {file.name}: {str(e)}")
 
243
  continue
244
 
245
+ # Simple summary at the end
246
+ st.write("---")
247
+ st.write("Processing complete!")
 
 
 
 
 
 
 
 
248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
  except Exception as e:
 
250
  st.error(f"An error occurred: {str(e)}")
251
 
252
  if __name__ == "__main__":
 
253
  main()