Update app.py
Browse files
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 |
-
#
|
| 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
|
| 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 |
-
#
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
-
#
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 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)}")
|