Update app.py
Browse files
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 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 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 |
-
|
|
|
|
|
|
|
| 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 ======================
|