Update app.py
Browse files
app.py
CHANGED
@@ -7,43 +7,33 @@ from reportlab.lib.pagesizes import letter
|
|
7 |
from reportlab.pdfgen import canvas
|
8 |
import os
|
9 |
|
10 |
-
# Load the trained model
|
11 |
model = tf.keras.models.load_model("my_keras_model.h5")
|
12 |
-
|
13 |
-
# Define image size based on the model's input requirement
|
14 |
-
image_size = (224, 224)
|
15 |
|
16 |
# Function to analyze injury severity
|
17 |
def analyze_injury(prediction):
|
18 |
if prediction < 0.3:
|
19 |
-
|
20 |
-
treatment = "Rest, pain relievers, and follow-up X-ray."
|
21 |
-
gov_cost = "₹2,000 - ₹5,000"
|
22 |
-
private_cost = "₹10,000 - ₹20,000"
|
23 |
elif 0.3 <= prediction < 0.7:
|
24 |
-
|
25 |
-
treatment = "Plaster cast or splint; possible minor surgery."
|
26 |
-
gov_cost = "₹8,000 - ₹15,000"
|
27 |
-
private_cost = "₹30,000 - ₹60,000"
|
28 |
else:
|
29 |
-
|
30 |
-
treatment = "Major surgery with metal implants, extensive physiotherapy."
|
31 |
-
gov_cost = "₹20,000 - ₹50,000"
|
32 |
-
private_cost = "₹1,00,000+"
|
33 |
-
|
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]
|
@@ -65,10 +55,12 @@ def generate_report(patient_name, age, gender, xray1_path, xray2_path):
|
|
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 |
-
|
|
|
|
|
|
|
72 |
|
73 |
except Exception as e:
|
74 |
return f"Error generating report: {str(e)}"
|
@@ -88,6 +80,5 @@ interface = gr.Interface(
|
|
88 |
description="Enter patient details, upload two X-ray images, and generate a detailed medical report with treatment suggestions and cost estimates."
|
89 |
)
|
90 |
|
91 |
-
# Launch the Gradio app
|
92 |
if __name__ == "__main__":
|
93 |
-
interface
|
|
|
7 |
from reportlab.pdfgen import canvas
|
8 |
import os
|
9 |
|
10 |
+
# Load the trained model once
|
11 |
model = tf.keras.models.load_model("my_keras_model.h5")
|
12 |
+
image_size = (224, 224) # Ensure consistent image size
|
|
|
|
|
13 |
|
14 |
# Function to analyze injury severity
|
15 |
def analyze_injury(prediction):
|
16 |
if prediction < 0.3:
|
17 |
+
return "Mild", "Rest and pain relief.", "₹2,000 - ₹5,000", "₹10,000 - ₹20,000"
|
|
|
|
|
|
|
18 |
elif 0.3 <= prediction < 0.7:
|
19 |
+
return "Moderate", "Plaster cast or minor surgery.", "₹8,000 - ₹15,000", "₹30,000 - ₹60,000"
|
|
|
|
|
|
|
20 |
else:
|
21 |
+
return "Severe", "Major surgery with metal implants.", "₹20,000 - ₹50,000", "₹1,00,000+"
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
# Function to generate report
|
24 |
def generate_report(patient_name, age, gender, xray1_path, xray2_path):
|
25 |
+
if not os.path.exists(xray1_path) or not os.path.exists(xray2_path):
|
26 |
+
return "Error: One or both X-ray images are missing!"
|
27 |
+
|
28 |
try:
|
29 |
# Process X-ray 1
|
30 |
+
img1 = Image.open(xray1_path).resize(image_size).convert("RGB")
|
31 |
img_array1 = image.img_to_array(img1)
|
32 |
img_array1 = np.expand_dims(img_array1, axis=0) / 255.0
|
33 |
prediction1 = model.predict(img_array1)[0][0]
|
34 |
|
35 |
# Process X-ray 2
|
36 |
+
img2 = Image.open(xray2_path).resize(image_size).convert("RGB")
|
37 |
img_array2 = image.img_to_array(img2)
|
38 |
img_array2 = np.expand_dims(img_array2, axis=0) / 255.0
|
39 |
prediction2 = model.predict(img_array2)[0][0]
|
|
|
55 |
c.drawString(100, 650, f"Recommended Treatment: {treatment}")
|
56 |
c.drawString(100, 630, f"Estimated Cost (Govt Hospital): {gov_cost}")
|
57 |
c.drawString(100, 610, f"Estimated Cost (Private Hospital): {private_cost}")
|
|
|
58 |
c.save()
|
59 |
|
60 |
+
if os.path.exists(report_path):
|
61 |
+
return report_path
|
62 |
+
else:
|
63 |
+
return "Error: Report generation failed!"
|
64 |
|
65 |
except Exception as e:
|
66 |
return f"Error generating report: {str(e)}"
|
|
|
80 |
description="Enter patient details, upload two X-ray images, and generate a detailed medical report with treatment suggestions and cost estimates."
|
81 |
)
|
82 |
|
|
|
83 |
if __name__ == "__main__":
|
84 |
+
interface
|