rnmee commited on
Commit
8550bf6
·
verified ·
1 Parent(s): 0eb494b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -62
app.py CHANGED
@@ -179,72 +179,70 @@ def segment_image(image: Image.Image, model: nn.Module) -> dict:
179
  'bright_area': (np.sum(final_mask == 1) / total_pixels * 100),
180
  'red_area': (np.sum(final_mask == 2) / total_pixels * 100)
181
  }
182
-
183
  # ====================== PDF REPORT GENERATION ======================
184
  def generate_pdf_report(original_img: Image.Image, mask: np.ndarray, overlay: Image.Image,
185
  diagnosis: str, grade: str, bright_area: float, red_area: float):
186
- pdf = FPDF()
187
- pdf.add_page()
188
-
189
-
190
- # Title
191
- pdf.set_font("Arial", 'B', 16)
192
- pdf.cell(0, 10, "Diabetic Retinopathy Diagnosis Report", 0, 1, 'C')
193
- pdf.ln(10)
194
-
195
- # Patient Info (placeholder)
196
- pdf.set_font("Arial", '', 12)
197
- pdf.cell(0, 10, "Patient: ___________________________", 0, 1)
198
- pdf.cell(0, 10, "Date: _____________________________", 0, 1)
199
- pdf.ln(10)
200
-
201
- # Diagnosis
202
- pdf.set_font("Arial", 'B', 14)
203
- pdf.cell(0, 10, "Diagnosis:", 0, 1)
204
- pdf.set_font("Arial", '', 12)
205
- pdf.cell(0, 10, f"Stage: {diagnosis}", 0, 1)
206
- pdf.cell(0, 10, f"UK Grading: {grade}", 0, 1)
207
- pdf.ln(10)
208
-
209
- # Lesion Analysis
210
- pdf.set_font("Arial", 'B', 14)
211
- pdf.cell(0, 10, "Lesion Analysis:", 0, 1)
212
- pdf.set_font("Arial", '', 12)
213
- pdf.cell(0, 10, f"Bright Lesions: {bright_area:.2f}%", 0, 1)
214
- pdf.cell(0, 10, f"Red Lesions: {red_area:.2f}%", 0, 1)
215
- pdf.cell(0, 10, f"Total Affected Area: {bright_area + red_area:.2f}%", 0, 1)
216
- pdf.ln(15)
217
-
218
- # Convert images to bytes
219
- def pil_to_bytes(img, format='PNG'):
220
- img_byte_arr = io.BytesIO()
221
- img.save(img_byte_arr, format=format)
222
- return img_byte_arr.getvalue()
223
-
224
- # Original Image
225
- pdf.set_font("Arial", 'B', 12)
226
- pdf.cell(0, 10, "Original Retinal Image:", 0, 1)
227
- pdf.image(io.BytesIO(pil_to_bytes(original_img)), x=50, w=100)
228
- pdf.ln(5)
229
-
230
- # Segmentation Mask
231
- mask_img = Image.fromarray((mask * 85).astype(np.uint8))
232
- pdf.set_font("Arial", 'B', 12)
233
- pdf.cell(0, 10, "Lesion Segmentation Mask:", 0, 1)
234
- pdf.image(io.BytesIO(pil_to_bytes(mask_img)), x=50, w=100)
235
- pdf.ln(5)
236
-
237
- # Overlay Image
238
- pdf.set_font("Arial", 'B', 12)
239
- pdf.cell(0, 10, "Lesion Overlay:", 0, 1)
240
- pdf.image(io.BytesIO(pil_to_bytes(overlay)), x=50, w=100)
241
-
242
- # Footer
243
- pdf.ln(15)
244
- pdf.set_font("Arial", 'I', 10)
245
- pdf.cell(0, 10, "This report was generated by DR Analysis System", 0, 1, 'C')
246
 
247
- return pdf.output(dest='S')
 
 
248
 
249
 
250
  # ====================== MAIN APP ======================
 
179
  'bright_area': (np.sum(final_mask == 1) / total_pixels * 100),
180
  'red_area': (np.sum(final_mask == 2) / total_pixels * 100)
181
  }
182
+
183
  # ====================== PDF REPORT GENERATION ======================
184
  def generate_pdf_report(original_img: Image.Image, mask: np.ndarray, overlay: Image.Image,
185
  diagnosis: str, grade: str, bright_area: float, red_area: float):
186
+ try:
187
+ pdf = FPDF()
188
+ pdf.add_page()
189
+
190
+ # Use built-in helvetica font (works without file)
191
+ pdf.set_font("helvetica", "B", 16)
192
+ pdf.cell(0, 10, "Diabetic Retinopathy Diagnosis Report", 0, 1, 'C')
193
+ pdf.ln(10)
194
+
195
+ # Patient Info
196
+ pdf.set_font("helvetica", "", 12)
197
+ pdf.cell(0, 10, "Patient: ___________________________", 0, 1)
198
+ pdf.cell(0, 10, "Date: _____________________________", 0, 1)
199
+ pdf.ln(10)
200
+
201
+ # Diagnosis
202
+ pdf.set_font("helvetica", "B", 14)
203
+ pdf.cell(0, 10, "Diagnosis:", 0, 1)
204
+ pdf.set_font("helvetica", "", 12)
205
+ pdf.cell(0, 10, f"Stage: {diagnosis}", 0, 1)
206
+ pdf.cell(0, 10, f"UK Grading: {grade}", 0, 1)
207
+ pdf.ln(10)
208
+
209
+ # Lesion Analysis
210
+ pdf.set_font("helvetica", "B", 14)
211
+ pdf.cell(0, 10, "Lesion Analysis:", 0, 1)
212
+ pdf.set_font("helvetica", "", 12)
213
+ pdf.cell(0, 10, f"Bright Lesions: {bright_area:.2f}%", 0, 1)
214
+ pdf.cell(0, 10, f"Red Lesions: {red_area:.2f}%", 0, 1)
215
+ pdf.cell(0, 10, f"Total Affected Area: {bright_area + red_area:.2f}%", 0, 1)
216
+ pdf.ln(15)
217
+
218
+ # Helper function to add images
219
+ def add_pdf_image(pdf, image, title, max_width=180):
220
+ img_byte_arr = io.BytesIO()
221
+ image.save(img_byte_arr, format='PNG')
222
+ img_byte_arr = img_byte_arr.getvalue()
223
+
224
+ pdf.set_font("helvetica", "B", 12)
225
+ pdf.cell(0, 10, title, 0, 1)
226
+ pdf.image(io.BytesIO(img_byte_arr), x=10, w=max_width)
227
+ pdf.ln(5)
228
+
229
+ # Add images (resize if needed)
230
+ add_pdf_image(pdf, original_img, "Original Retinal Image:")
231
+ add_pdf_image(pdf, Image.fromarray((mask * 85).astype(np.uint8)), "Lesion Segmentation Mask:")
232
+ add_pdf_image(pdf, overlay, "Lesion Overlay:")
233
+
234
+ # Footer
235
+ pdf.ln(10)
236
+ pdf.set_font("helvetica", "I", 10)
237
+ pdf.cell(0, 10, "This report was generated by DR Analysis System", 0, 1, 'C')
238
+
239
+ # Return PDF bytes
240
+ pdf_output = pdf.output(dest='S').encode('latin1')
241
+ return pdf_output
 
 
 
 
242
 
243
+ except Exception as e:
244
+ st.error(f"PDF generation failed: {str(e)}")
245
+ return None
246
 
247
 
248
  # ====================== MAIN APP ======================