rnmee commited on
Commit
f687492
·
verified ·
1 Parent(s): 0d133f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -23
app.py CHANGED
@@ -184,22 +184,20 @@ def segment_image(image: Image.Image, model: nn.Module) -> dict:
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
- # Create PDF in memory
188
  pdf = FPDF()
189
  pdf.add_page()
190
 
191
- # Use built-in font that supports basic characters
192
  pdf.set_font("helvetica", "B", 16)
193
  pdf.cell(text="Diabetic Retinopathy Diagnosis Report", new_x="LMARGIN", new_y="NEXT", align='C')
194
  pdf.ln(10)
195
 
196
- # Patient Info
197
  pdf.set_font("helvetica", "", 12)
198
  pdf.cell(text="Patient: ___________________________", new_x="LMARGIN", new_y="NEXT")
199
  pdf.cell(text="Date: _____________________________", new_x="LMARGIN", new_y="NEXT")
200
  pdf.ln(10)
201
 
202
- # Diagnosis
203
  pdf.set_font("helvetica", "B", 14)
204
  pdf.cell(text="Diagnosis:", new_x="LMARGIN", new_y="NEXT")
205
  pdf.set_font("helvetica", "", 12)
@@ -207,7 +205,7 @@ def generate_pdf_report(original_img: Image.Image, mask: np.ndarray, overlay: Im
207
  pdf.cell(text=f"UK Grading: {grade}", new_x="LMARGIN", new_y="NEXT")
208
  pdf.ln(10)
209
 
210
- # Lesion Analysis
211
  pdf.set_font("helvetica", "B", 14)
212
  pdf.cell(text="Lesion Analysis:", new_x="LMARGIN", new_y="NEXT")
213
  pdf.set_font("helvetica", "", 12)
@@ -216,30 +214,38 @@ def generate_pdf_report(original_img: Image.Image, mask: np.ndarray, overlay: Im
216
  pdf.cell(text=f"Total Affected Area: {bright_area + red_area:.2f}%", new_x="LMARGIN", new_y="NEXT")
217
  pdf.ln(15)
218
 
219
- # Helper function to add images
220
- def add_pdf_image(pdf, image, title):
221
- img_byte_arr = io.BytesIO()
222
- resized_img = image.resize((150, 150))
223
- resized_img.save(img_byte_arr, format='PNG')
224
- img_bytes = img_byte_arr.getvalue()
225
- pdf.set_font("helvetica", "B", 12)
226
- pdf.cell(text=title, new_x="LMARGIN", new_y="NEXT")
227
- pdf.image(io.BytesIO(img_bytes), x=10, w=100)
228
- pdf.ln(5)
 
 
 
 
 
 
 
 
229
 
230
- # Add images
231
- add_pdf_image(pdf, original_img, "Original Retinal Image:")
232
- add_pdf_image(pdf, Image.fromarray((mask * 85).astype(np.uint8)), "Lesion Segmentation Mask:")
233
- add_pdf_image(pdf, overlay, "Lesion Overlay:")
 
 
234
 
235
- # Footer
236
  pdf.ln(10)
237
  pdf.set_font("helvetica", "I", 10)
238
  pdf.cell(text="This report was generated by DR Analysis System", new_x="LMARGIN", new_y="NEXT", align='C')
239
 
240
- # Get PDF as bytes - updated for fpdf2>=2.7.6
241
- pdf_bytes = bytes(pdf.output()) # Returns bytes directly in newer versions
242
- return pdf_bytes
243
 
244
  except Exception as e:
245
  st.error(f"PDF generation failed: {str(e)}")
 
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
+ # Header and patient info
191
  pdf.set_font("helvetica", "B", 16)
192
  pdf.cell(text="Diabetic Retinopathy Diagnosis Report", new_x="LMARGIN", new_y="NEXT", align='C')
193
  pdf.ln(10)
194
 
 
195
  pdf.set_font("helvetica", "", 12)
196
  pdf.cell(text="Patient: ___________________________", new_x="LMARGIN", new_y="NEXT")
197
  pdf.cell(text="Date: _____________________________", new_x="LMARGIN", new_y="NEXT")
198
  pdf.ln(10)
199
 
200
+ # Diagnosis section
201
  pdf.set_font("helvetica", "B", 14)
202
  pdf.cell(text="Diagnosis:", new_x="LMARGIN", new_y="NEXT")
203
  pdf.set_font("helvetica", "", 12)
 
205
  pdf.cell(text=f"UK Grading: {grade}", new_x="LMARGIN", new_y="NEXT")
206
  pdf.ln(10)
207
 
208
+ # Lesion analysis
209
  pdf.set_font("helvetica", "B", 14)
210
  pdf.cell(text="Lesion Analysis:", new_x="LMARGIN", new_y="NEXT")
211
  pdf.set_font("helvetica", "", 12)
 
214
  pdf.cell(text=f"Total Affected Area: {bright_area + red_area:.2f}%", new_x="LMARGIN", new_y="NEXT")
215
  pdf.ln(15)
216
 
217
+ # Original image on first page
218
+ pdf.set_font("helvetica", "B", 12)
219
+ pdf.cell(text="Original Retinal Image:", new_x="LMARGIN", new_y="NEXT")
220
+ img_byte_arr = io.BytesIO()
221
+ original_img.save(img_byte_arr, format='PNG')
222
+ pdf.image(io.BytesIO(img_byte_arr.getvalue()), x=10, w=100)
223
+ pdf.ln(10)
224
+
225
+ # Add new page for segmentation results
226
+ pdf.add_page()
227
+
228
+ # Segmentation mask
229
+ pdf.set_font("helvetica", "B", 12)
230
+ pdf.cell(text="Lesion Segmentation Mask:", new_x="LMARGIN", new_y="NEXT")
231
+ img_byte_arr = io.BytesIO()
232
+ Image.fromarray((mask * 85).astype(np.uint8)).save(img_byte_arr, format='PNG')
233
+ pdf.image(io.BytesIO(img_byte_arr.getvalue()), x=10, w=100)
234
+ pdf.ln(10)
235
 
236
+ # Lesion overlay
237
+ pdf.set_font("helvetica", "B", 12)
238
+ pdf.cell(text="Lesion Overlay:", new_x="LMARGIN", new_y="NEXT")
239
+ img_byte_arr = io.BytesIO()
240
+ overlay.save(img_byte_arr, format='PNG')
241
+ pdf.image(io.BytesIO(img_byte_arr.getvalue()), x=10, w=100)
242
 
243
+ # Footer on last page
244
  pdf.ln(10)
245
  pdf.set_font("helvetica", "I", 10)
246
  pdf.cell(text="This report was generated by DR Analysis System", new_x="LMARGIN", new_y="NEXT", align='C')
247
 
248
+ return bytes(pdf.output())
 
 
249
 
250
  except Exception as e:
251
  st.error(f"PDF generation failed: {str(e)}")