|
a |
|
b/utils/report_generator.py |
|
|
1 |
from fpdf import FPDF |
|
|
2 |
from datetime import datetime |
|
|
3 |
|
|
|
4 |
class PDFReport(FPDF): |
|
|
5 |
def header(self): |
|
|
6 |
self.set_font('Arial', 'B', 14) |
|
|
7 |
self.cell(0, 10, 'Lung Disease Detection Report', ln=True, align='C') |
|
|
8 |
self.ln(10) |
|
|
9 |
|
|
|
10 |
def add_patient_info(self, name, age, gender): |
|
|
11 |
self.set_font('Arial', '', 12) |
|
|
12 |
self.cell(0, 10, f'Name: {name} Age: {age} Gender: {gender}', ln=True) |
|
|
13 |
self.cell(0, 10, f'Date: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}', ln=True) |
|
|
14 |
self.ln(5) |
|
|
15 |
|
|
|
16 |
def add_images(self, xray_path, mask_path): |
|
|
17 |
self.cell(0, 10, 'X-ray Image:', ln=True) |
|
|
18 |
self.image(xray_path, w=90) |
|
|
19 |
self.ln(5) |
|
|
20 |
self.cell(0, 10, 'Segmented Output:', ln=True) |
|
|
21 |
self.image(mask_path, w=90) |
|
|
22 |
self.ln(10) |
|
|
23 |
|
|
|
24 |
def add_diagnosis(self, disease, severity, comment): |
|
|
25 |
self.set_font('Arial', 'B', 12) |
|
|
26 |
self.cell(0, 10, f'Diagnosis: {disease}', ln=True) |
|
|
27 |
self.cell(0, 10, f'Severity: {severity}', ln=True) |
|
|
28 |
self.set_font('Arial', '', 12) |
|
|
29 |
self.multi_cell(0, 10, f'Comments:\n{comment}') |
|
|
30 |
self.ln(10) |
|
|
31 |
|
|
|
32 |
def footer(self): |
|
|
33 |
self.set_y(-15) |
|
|
34 |
self.set_font('Arial', 'I', 10) |
|
|
35 |
self.cell(0, 10, 'AI-generated report. Please consult a certified radiologist.', align='C') |
|
|
36 |
|
|
|
37 |
|
|
|
38 |
def get_disease_comment(disease, severity): |
|
|
39 |
comments = { |
|
|
40 |
"COVID-19": { |
|
|
41 |
"Mild": "Ground-glass opacities are faintly visible, typically associated with mild COVID-19. Recommend monitoring and follow-up scan.", |
|
|
42 |
"Moderate": "Bilateral opacities with peripheral distribution indicate moderate COVID-19 pneumonia. Suggest home isolation and treatment unless symptoms worsen.", |
|
|
43 |
"Severe": "Extensive ground-glass opacities and consolidation detected. Suggest immediate hospitalization for advanced COVID-19 treatment." |
|
|
44 |
}, |
|
|
45 |
"Pneumonia": { |
|
|
46 |
"Mild": "Localized patchy infiltrates suggest early-stage pneumonia. Recommend antibiotics and follow-up.", |
|
|
47 |
"Moderate": "Lung lobes show dense consolidation, typical of moderate pneumonia. Clinical treatment and rest advised.", |
|
|
48 |
"Severe": "Widespread opacities indicate severe pneumonia. Urgent clinical attention required." |
|
|
49 |
}, |
|
|
50 |
"Tuberculosis": { |
|
|
51 |
"Mild": "Apical scarring may indicate early-stage tuberculosis. Recommend sputum test and anti-TB medication.", |
|
|
52 |
"Moderate": "Fibronodular lesions seen, likely due to active TB. Suggest starting anti-tubercular therapy immediately.", |
|
|
53 |
"Severe": "Extensive cavitary lesions observed, typical of severe TB. Hospitalization and long-term treatment needed." |
|
|
54 |
}, |
|
|
55 |
"Normal": { |
|
|
56 |
"Mild": "Lungs appear healthy. No abnormal findings.", |
|
|
57 |
"Moderate": "Slight deviations detected but not indicative of major disease.", |
|
|
58 |
"Severe": "Abnormal scan reported, but not matching major disease pattern. Recommend detailed physical exam." |
|
|
59 |
} |
|
|
60 |
} |
|
|
61 |
|
|
|
62 |
return comments.get(disease, {}).get(severity, "No specific comment available.") |
|
|
63 |
|
|
|
64 |
|
|
|
65 |
def generate_report(name, age, gender, xray_path, mask_path, disease, severity, output_path): |
|
|
66 |
comment = get_disease_comment(disease, severity) |
|
|
67 |
pdf = PDFReport() |
|
|
68 |
pdf.add_page() |
|
|
69 |
pdf.add_patient_info(name, age, gender) |
|
|
70 |
pdf.add_images(xray_path, mask_path) |
|
|
71 |
pdf.add_diagnosis(disease, severity, comment) |
|
|
72 |
pdf.output(output_path) |