rnmee commited on
Commit
474c097
·
verified ·
1 Parent(s): 8550bf6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -184,10 +184,11 @@ 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
  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)
@@ -216,17 +217,15 @@ def generate_pdf_report(original_img: Image.Image, mask: np.ndarray, overlay: Im
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:")
@@ -236,9 +235,9 @@ def generate_pdf_report(original_img: Image.Image, mask: np.ndarray, overlay: Im
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)}")
 
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(0, 10, "Diabetic Retinopathy Diagnosis Report", 0, 1, 'C')
194
  pdf.ln(10)
 
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
  image.save(img_byte_arr, format='PNG')
 
 
223
  pdf.set_font("helvetica", "B", 12)
224
  pdf.cell(0, 10, title, 0, 1)
225
+ pdf.image(io.BytesIO(img_byte_arr.getvalue()), x=10, w=180)
226
  pdf.ln(5)
227
 
228
+ # Add images
229
  add_pdf_image(pdf, original_img, "Original Retinal Image:")
230
  add_pdf_image(pdf, Image.fromarray((mask * 85).astype(np.uint8)), "Lesion Segmentation Mask:")
231
  add_pdf_image(pdf, overlay, "Lesion Overlay:")
 
235
  pdf.set_font("helvetica", "I", 10)
236
  pdf.cell(0, 10, "This report was generated by DR Analysis System", 0, 1, 'C')
237
 
238
+ # Get PDF bytes without encoding
239
+ pdf_output = pdf.output(dest='S')
240
+ return pdf_output.encode('latin1') if isinstance(pdf_output, str) else pdf_output
241
 
242
  except Exception as e:
243
  st.error(f"PDF generation failed: {str(e)}")