Update app.py
Browse files
app.py
CHANGED
@@ -18,8 +18,8 @@ model = tf.keras.models.load_model("my_keras_model.h5")
|
|
18 |
with open("templates/re.html", "r", encoding="utf-8") as file:
|
19 |
html_content = file.read()
|
20 |
|
21 |
-
# Function to process X-
|
22 |
-
def generate_report(name, age, gender, allergies, cause,
|
23 |
image_size = (224, 224)
|
24 |
|
25 |
def predict_fracture(xray_path):
|
@@ -29,18 +29,16 @@ def generate_report(name, age, gender, allergies, cause, xray1, xray2):
|
|
29 |
prediction = model.predict(img_array)[0][0]
|
30 |
return prediction
|
31 |
|
32 |
-
# Predict
|
33 |
-
|
34 |
-
|
35 |
-
avg_prediction = (prediction1 + prediction2) / 2
|
36 |
-
diagnosed_class = "Fractured" if avg_prediction > 0.5 else "Normal"
|
37 |
|
38 |
# Injury severity classification
|
39 |
-
severity = "Mild" if
|
40 |
treatment_details = {
|
41 |
-
"Mild": "Your
|
42 |
-
"Moderate": "Your
|
43 |
-
"Severe": "Your
|
44 |
}
|
45 |
treatment = treatment_details[severity]
|
46 |
|
@@ -51,13 +49,10 @@ def generate_report(name, age, gender, allergies, cause, xray1, xray2):
|
|
51 |
["Private Hospital", f"₹{10000 if severity == 'Mild' else 30000 if severity == 'Moderate' else 100000}+", "6 weeks - Several months"]
|
52 |
]
|
53 |
|
54 |
-
# Save X-ray
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
img2_path = f"{name}_xray2.png"
|
59 |
-
img1.save(img1_path)
|
60 |
-
img2.save(img2_path)
|
61 |
|
62 |
# Generate PDF report
|
63 |
report_path = f"{name}_fracture_report.pdf"
|
@@ -65,39 +60,56 @@ def generate_report(name, age, gender, allergies, cause, xray1, xray2):
|
|
65 |
c.setFont("Helvetica-Bold", 14)
|
66 |
c.drawString(200, 770, "Bone Fracture Detection Report")
|
67 |
|
68 |
-
# Patient details
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
84 |
|
85 |
-
# Load and insert X-ray
|
86 |
-
c.drawInlineImage(
|
87 |
-
c.
|
|
|
88 |
|
89 |
# Cost estimation table
|
90 |
-
|
91 |
-
|
92 |
-
('BACKGROUND', (0, 0), (-1, 0), colors.
|
93 |
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
|
94 |
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
|
95 |
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
|
96 |
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
|
97 |
('GRID', (0, 0), (-1, -1), 1, colors.black)
|
98 |
]))
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
c.save()
|
103 |
|
@@ -112,21 +124,24 @@ with gr.Blocks() as app:
|
|
112 |
name = gr.Textbox(label="Patient Name")
|
113 |
age = gr.Number(label="Age")
|
114 |
gender = gr.Radio(["Male", "Female", "Other"], label="Gender")
|
115 |
-
|
|
|
|
|
|
|
|
|
116 |
with gr.Row():
|
117 |
allergies = gr.Textbox(label="Allergies (if any)")
|
118 |
cause = gr.Textbox(label="Cause of Injury")
|
119 |
|
120 |
with gr.Row():
|
121 |
-
|
122 |
-
xray2 = gr.Image(type="filepath", label="Upload X-ray Image 2")
|
123 |
|
124 |
submit_button = gr.Button("Generate Report")
|
125 |
output_file = gr.File(label="Download Report")
|
126 |
|
127 |
submit_button.click(
|
128 |
generate_report,
|
129 |
-
inputs=[name, age, gender, allergies, cause,
|
130 |
outputs=[output_file],
|
131 |
)
|
132 |
|
|
|
18 |
with open("templates/re.html", "r", encoding="utf-8") as file:
|
19 |
html_content = file.read()
|
20 |
|
21 |
+
# Function to process X-ray and generate a PDF report
|
22 |
+
def generate_report(name, age, gender, weight, height, allergies, cause, xray):
|
23 |
image_size = (224, 224)
|
24 |
|
25 |
def predict_fracture(xray_path):
|
|
|
29 |
prediction = model.predict(img_array)[0][0]
|
30 |
return prediction
|
31 |
|
32 |
+
# Predict fracture
|
33 |
+
prediction = predict_fracture(xray)
|
34 |
+
diagnosed_class = "Fractured" if prediction > 0.5 else "Normal"
|
|
|
|
|
35 |
|
36 |
# Injury severity classification
|
37 |
+
severity = "Mild" if prediction < 0.3 else "Moderate" if prediction < 0.7 else "Severe"
|
38 |
treatment_details = {
|
39 |
+
"Mild": "Your injury is classified as **Mild**. It may heal with rest, pain relievers, and a follow-up X-ray. Avoid excessive movement of the affected area.",
|
40 |
+
"Moderate": "Your injury is classified as **Moderate**. You may require a plaster cast, splint, or minor surgery. Recovery takes **4-8 weeks**.",
|
41 |
+
"Severe": "Your injury is classified as **Severe**. Surgery with metal implants and extensive physiotherapy is required. Recovery takes **several months** with proper rehabilitation."
|
42 |
}
|
43 |
treatment = treatment_details[severity]
|
44 |
|
|
|
49 |
["Private Hospital", f"₹{10000 if severity == 'Mild' else 30000 if severity == 'Moderate' else 100000}+", "6 weeks - Several months"]
|
50 |
]
|
51 |
|
52 |
+
# Save X-ray image for report
|
53 |
+
img = Image.open(xray).resize((300, 300))
|
54 |
+
img_path = f"{name}_xray.png"
|
55 |
+
img.save(img_path)
|
|
|
|
|
|
|
56 |
|
57 |
# Generate PDF report
|
58 |
report_path = f"{name}_fracture_report.pdf"
|
|
|
60 |
c.setFont("Helvetica-Bold", 14)
|
61 |
c.drawString(200, 770, "Bone Fracture Detection Report")
|
62 |
|
63 |
+
# Patient details table
|
64 |
+
patient_data = [
|
65 |
+
["Attribute", "Details"],
|
66 |
+
["Patient Name", name],
|
67 |
+
["Age", age],
|
68 |
+
["Gender", gender],
|
69 |
+
["Weight", f"{weight} kg"],
|
70 |
+
["Height", f"{height} cm"],
|
71 |
+
["Allergies", allergies if allergies else "None"],
|
72 |
+
["Cause of Injury", cause if cause else "Not Provided"],
|
73 |
+
["Diagnosis", diagnosed_class],
|
74 |
+
["Injury Severity", severity]
|
75 |
+
]
|
76 |
|
77 |
+
table = Table(patient_data)
|
78 |
+
table.setStyle(TableStyle([
|
79 |
+
('BACKGROUND', (0, 0), (-1, 0), colors.grey),
|
80 |
+
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
|
81 |
+
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
|
82 |
+
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
|
83 |
+
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
|
84 |
+
('GRID', (0, 0), (-1, -1), 1, colors.black)
|
85 |
+
]))
|
86 |
+
table.wrapOn(c, 400, 500)
|
87 |
+
table.drawOn(c, 50, 600)
|
88 |
|
89 |
+
# Load and insert X-ray image
|
90 |
+
c.drawInlineImage(img_path, 50, 320, width=250, height=250)
|
91 |
+
c.setFont("Helvetica-Bold", 12)
|
92 |
+
c.drawString(120, 290, f"Fractured: {'Yes' if diagnosed_class == 'Fractured' else 'No'}")
|
93 |
|
94 |
# Cost estimation table
|
95 |
+
cost_table = Table(cost_duration_data)
|
96 |
+
cost_table.setStyle(TableStyle([
|
97 |
+
('BACKGROUND', (0, 0), (-1, 0), colors.darkblue),
|
98 |
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
|
99 |
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
|
100 |
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
|
101 |
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
|
102 |
('GRID', (0, 0), (-1, -1), 1, colors.black)
|
103 |
]))
|
104 |
+
cost_table.wrapOn(c, 400, 200)
|
105 |
+
cost_table.drawOn(c, 50, 120)
|
106 |
+
|
107 |
+
# Add Treatment Recommendations
|
108 |
+
c.setFont("Helvetica-Bold", 12)
|
109 |
+
c.drawString(50, 100, "Treatment & Recovery Recommendations:")
|
110 |
+
c.setFont("Helvetica", 10)
|
111 |
+
c.drawString(50, 80, treatment)
|
112 |
+
c.drawString(50, 60, "Follow proper medical care and consult your doctor regularly.")
|
113 |
|
114 |
c.save()
|
115 |
|
|
|
124 |
name = gr.Textbox(label="Patient Name")
|
125 |
age = gr.Number(label="Age")
|
126 |
gender = gr.Radio(["Male", "Female", "Other"], label="Gender")
|
127 |
+
|
128 |
+
with gr.Row():
|
129 |
+
weight = gr.Number(label="Weight (kg)")
|
130 |
+
height = gr.Number(label="Height (cm)")
|
131 |
+
|
132 |
with gr.Row():
|
133 |
allergies = gr.Textbox(label="Allergies (if any)")
|
134 |
cause = gr.Textbox(label="Cause of Injury")
|
135 |
|
136 |
with gr.Row():
|
137 |
+
xray = gr.Image(type="filepath", label="Upload X-ray Image")
|
|
|
138 |
|
139 |
submit_button = gr.Button("Generate Report")
|
140 |
output_file = gr.File(label="Download Report")
|
141 |
|
142 |
submit_button.click(
|
143 |
generate_report,
|
144 |
+
inputs=[name, age, gender, weight, height, allergies, cause, xray],
|
145 |
outputs=[output_file],
|
146 |
)
|
147 |
|