Update app.py
Browse files
app.py
CHANGED
@@ -34,40 +34,44 @@ def analyze_injury(prediction):
|
|
34 |
return severity, treatment, gov_cost, private_cost
|
35 |
|
36 |
# Function to generate report
|
37 |
-
def generate_report(patient_name, age, gender,
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
69 |
|
70 |
-
|
|
|
71 |
|
72 |
# Define Gradio Interface
|
73 |
interface = gr.Interface(
|
@@ -76,8 +80,8 @@ interface = gr.Interface(
|
|
76 |
gr.Textbox(label="Patient Name"),
|
77 |
gr.Number(label="Age"),
|
78 |
gr.Radio(["Male", "Female", "Other"], label="Gender"),
|
79 |
-
gr.Image(type="
|
80 |
-
gr.Image(type="
|
81 |
],
|
82 |
outputs=gr.File(label="Download Report"),
|
83 |
title="Bone Fracture Detection & Medical Report",
|
|
|
34 |
return severity, treatment, gov_cost, private_cost
|
35 |
|
36 |
# Function to generate report
|
37 |
+
def generate_report(patient_name, age, gender, xray1_path, xray2_path):
|
38 |
+
try:
|
39 |
+
# Process X-ray 1
|
40 |
+
img1 = Image.open(xray1_path).resize(image_size)
|
41 |
+
img_array1 = image.img_to_array(img1)
|
42 |
+
img_array1 = np.expand_dims(img_array1, axis=0) / 255.0
|
43 |
+
prediction1 = model.predict(img_array1)[0][0]
|
44 |
|
45 |
+
# Process X-ray 2
|
46 |
+
img2 = Image.open(xray2_path).resize(image_size)
|
47 |
+
img_array2 = image.img_to_array(img2)
|
48 |
+
img_array2 = np.expand_dims(img_array2, axis=0) / 255.0
|
49 |
+
prediction2 = model.predict(img_array2)[0][0]
|
50 |
|
51 |
+
# Get final analysis
|
52 |
+
avg_prediction = (prediction1 + prediction2) / 2
|
53 |
+
predicted_class = "Fractured" if avg_prediction > 0.5 else "Normal"
|
54 |
+
severity, treatment, gov_cost, private_cost = analyze_injury(avg_prediction)
|
55 |
|
56 |
+
# Generate PDF Report
|
57 |
+
report_path = f"{patient_name}_fracture_report.pdf"
|
58 |
+
c = canvas.Canvas(report_path, pagesize=letter)
|
59 |
+
c.setFont("Helvetica", 12)
|
60 |
+
c.drawString(100, 750, f"Patient Name: {patient_name}")
|
61 |
+
c.drawString(100, 730, f"Age: {age}")
|
62 |
+
c.drawString(100, 710, f"Gender: {gender}")
|
63 |
+
c.drawString(100, 690, f"Diagnosis: {predicted_class}")
|
64 |
+
c.drawString(100, 670, f"Injury Severity: {severity}")
|
65 |
+
c.drawString(100, 650, f"Recommended Treatment: {treatment}")
|
66 |
+
c.drawString(100, 630, f"Estimated Cost (Govt Hospital): {gov_cost}")
|
67 |
+
c.drawString(100, 610, f"Estimated Cost (Private Hospital): {private_cost}")
|
68 |
+
|
69 |
+
c.save()
|
70 |
+
|
71 |
+
return report_path
|
72 |
|
73 |
+
except Exception as e:
|
74 |
+
return f"Error generating report: {str(e)}"
|
75 |
|
76 |
# Define Gradio Interface
|
77 |
interface = gr.Interface(
|
|
|
80 |
gr.Textbox(label="Patient Name"),
|
81 |
gr.Number(label="Age"),
|
82 |
gr.Radio(["Male", "Female", "Other"], label="Gender"),
|
83 |
+
gr.Image(type="filepath", label="Upload X-ray Image 1"),
|
84 |
+
gr.Image(type="filepath", label="Upload X-ray Image 2"),
|
85 |
],
|
86 |
outputs=gr.File(label="Download Report"),
|
87 |
title="Bone Fracture Detection & Medical Report",
|