Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
4 |
-
import cv2
|
5 |
from tensorflow.keras.preprocessing import image
|
6 |
from PIL import Image, ImageDraw
|
7 |
from reportlab.lib.pagesizes import letter
|
@@ -14,21 +13,17 @@ model = tf.keras.models.load_model("my_keras_model.h5")
|
|
14 |
# Define image size
|
15 |
image_size = (224, 224)
|
16 |
|
17 |
-
# Function to detect fracture
|
18 |
def detect_fracture(xray):
|
19 |
img = Image.open(xray).convert("RGB").resize(image_size)
|
20 |
img_array = image.img_to_array(img) / 255.0
|
21 |
img_array = np.expand_dims(img_array, axis=0)
|
22 |
-
|
23 |
prediction = model.predict(img_array)[0][0]
|
24 |
-
|
25 |
-
# Dummy bounding box for now (assuming model enhancement needed)
|
26 |
-
# Ideally, the model should return bounding box coordinates
|
27 |
bbox = (50, 50, 150, 150) if prediction > 0.5 else None # Placeholder
|
28 |
-
|
29 |
return prediction, bbox, img
|
30 |
|
31 |
-
# Function to analyze
|
32 |
def analyze_injury(prediction):
|
33 |
if prediction < 0.3:
|
34 |
severity = "Mild"
|
@@ -45,12 +40,11 @@ def analyze_injury(prediction):
|
|
45 |
treatment = "Major surgery with metal implants, extensive physiotherapy."
|
46 |
gov_cost = "₹20,000 - ₹50,000"
|
47 |
private_cost = "₹1,00,000+"
|
48 |
-
|
49 |
return severity, treatment, gov_cost, private_cost
|
50 |
|
51 |
# Function to generate report
|
52 |
def generate_report(name, age, gender, xray1, xray2):
|
53 |
-
# Analyze X-ray images
|
54 |
prediction1, bbox1, img1 = detect_fracture(xray1)
|
55 |
prediction2, bbox2, img2 = detect_fracture(xray2)
|
56 |
|
@@ -58,7 +52,7 @@ def generate_report(name, age, gender, xray1, xray2):
|
|
58 |
diagnosed_class = "Fractured" if avg_prediction > 0.5 else "Normal"
|
59 |
severity, treatment, gov_cost, private_cost = analyze_injury(avg_prediction)
|
60 |
|
61 |
-
# Draw bounding box if fracture
|
62 |
if bbox1:
|
63 |
draw = ImageDraw.Draw(img1)
|
64 |
draw.rectangle(bbox1, outline="red", width=5)
|
@@ -67,7 +61,7 @@ def generate_report(name, age, gender, xray1, xray2):
|
|
67 |
draw = ImageDraw.Draw(img2)
|
68 |
draw.rectangle(bbox2, outline="red", width=5)
|
69 |
|
70 |
-
# Save
|
71 |
img1_path = f"{name}_xray1.png"
|
72 |
img2_path = f"{name}_xray2.png"
|
73 |
img1.save(img1_path)
|
@@ -91,30 +85,65 @@ def generate_report(name, age, gender, xray1, xray2):
|
|
91 |
|
92 |
return report_path, img1_path, img2_path, diagnosed_class, severity, treatment, gov_cost, private_cost
|
93 |
|
94 |
-
#
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
if __name__ == "__main__":
|
120 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
|
|
4 |
from tensorflow.keras.preprocessing import image
|
5 |
from PIL import Image, ImageDraw
|
6 |
from reportlab.lib.pagesizes import letter
|
|
|
13 |
# Define image size
|
14 |
image_size = (224, 224)
|
15 |
|
16 |
+
# Function to detect fracture
|
17 |
def detect_fracture(xray):
|
18 |
img = Image.open(xray).convert("RGB").resize(image_size)
|
19 |
img_array = image.img_to_array(img) / 255.0
|
20 |
img_array = np.expand_dims(img_array, axis=0)
|
21 |
+
|
22 |
prediction = model.predict(img_array)[0][0]
|
|
|
|
|
|
|
23 |
bbox = (50, 50, 150, 150) if prediction > 0.5 else None # Placeholder
|
|
|
24 |
return prediction, bbox, img
|
25 |
|
26 |
+
# Function to analyze severity
|
27 |
def analyze_injury(prediction):
|
28 |
if prediction < 0.3:
|
29 |
severity = "Mild"
|
|
|
40 |
treatment = "Major surgery with metal implants, extensive physiotherapy."
|
41 |
gov_cost = "₹20,000 - ₹50,000"
|
42 |
private_cost = "₹1,00,000+"
|
43 |
+
|
44 |
return severity, treatment, gov_cost, private_cost
|
45 |
|
46 |
# Function to generate report
|
47 |
def generate_report(name, age, gender, xray1, xray2):
|
|
|
48 |
prediction1, bbox1, img1 = detect_fracture(xray1)
|
49 |
prediction2, bbox2, img2 = detect_fracture(xray2)
|
50 |
|
|
|
52 |
diagnosed_class = "Fractured" if avg_prediction > 0.5 else "Normal"
|
53 |
severity, treatment, gov_cost, private_cost = analyze_injury(avg_prediction)
|
54 |
|
55 |
+
# Draw bounding box if fracture detected
|
56 |
if bbox1:
|
57 |
draw = ImageDraw.Draw(img1)
|
58 |
draw.rectangle(bbox1, outline="red", width=5)
|
|
|
61 |
draw = ImageDraw.Draw(img2)
|
62 |
draw.rectangle(bbox2, outline="red", width=5)
|
63 |
|
64 |
+
# Save images
|
65 |
img1_path = f"{name}_xray1.png"
|
66 |
img2_path = f"{name}_xray2.png"
|
67 |
img1.save(img1_path)
|
|
|
85 |
|
86 |
return report_path, img1_path, img2_path, diagnosed_class, severity, treatment, gov_cost, private_cost
|
87 |
|
88 |
+
# UI Components
|
89 |
+
with gr.Blocks() as app:
|
90 |
+
gr.Markdown("# 🏥 Bone Fracture Detection & Medical Report")
|
91 |
+
gr.Markdown(
|
92 |
+
"### A radiologist is a doctor who specializes in reading medical images like X-rays, MRIs, and CT scans to diagnose diseases and injuries."
|
93 |
+
)
|
94 |
+
gr.Image("x.jpg", label="X-Ray Example")
|
95 |
+
gr.Markdown(
|
96 |
+
"""
|
97 |
+
## **Understanding Bone Fractures**
|
98 |
+
- **Closed (Simple):** Bone doesn't pierce skin.
|
99 |
+
- **Open (Compound):** Bone breaks skin.
|
100 |
+
- **Hairline:** Small stress fracture.
|
101 |
+
- **Comminuted:** Bone shatters into pieces.
|
102 |
+
- **Avulsion:** Tendon pulls bone fragment.
|
103 |
+
- **Compression:** Bones forced together.
|
104 |
+
"""
|
105 |
+
)
|
106 |
+
gr.Markdown(
|
107 |
+
"""
|
108 |
+
## **First Aid**
|
109 |
+
- Immobilize the injured area.
|
110 |
+
- Control bleeding, cover wounds.
|
111 |
+
- Don't straighten broken bones.
|
112 |
+
- Use splints, slings for support.
|
113 |
+
- Apply cold packs.
|
114 |
+
- Seek emergency help.
|
115 |
+
"""
|
116 |
+
)
|
117 |
+
|
118 |
+
with gr.Row():
|
119 |
+
gr.Markdown("## 📝 Patient Information Form")
|
120 |
+
|
121 |
+
with gr.Column():
|
122 |
+
name = gr.Textbox(label="Patient Name")
|
123 |
+
age = gr.Number(label="Age")
|
124 |
+
gender = gr.Radio(["Male", "Female", "Other"], label="Gender")
|
125 |
+
|
126 |
+
with gr.Column():
|
127 |
+
xray1 = gr.Image(type="file", label="Upload X-ray Image 1")
|
128 |
+
xray2 = gr.Image(type="file", label="Upload X-ray Image 2")
|
129 |
+
|
130 |
+
submit_button = gr.Button("Generate Report")
|
131 |
+
|
132 |
+
output_file = gr.File(label="Download Report")
|
133 |
+
xray1_output = gr.Image(label="X-ray 1 with Fracture Highlight")
|
134 |
+
xray2_output = gr.Image(label="X-ray 2 with Fracture Highlight")
|
135 |
+
diagnosis_output = gr.Textbox(label="Fracture Detected")
|
136 |
+
severity_output = gr.Textbox(label="Injury Severity")
|
137 |
+
treatment_output = gr.Textbox(label="Recommended Treatment")
|
138 |
+
gov_cost_output = gr.Textbox(label="Estimated Cost (Govt Hospital)")
|
139 |
+
private_cost_output = gr.Textbox(label="Estimated Cost (Private Hospital)")
|
140 |
+
|
141 |
+
submit_button.click(
|
142 |
+
generate_report,
|
143 |
+
inputs=[name, age, gender, xray1, xray2],
|
144 |
+
outputs=[output_file, xray1_output, xray2_output, diagnosis_output, severity_output, treatment_output, gov_cost_output, private_cost_output],
|
145 |
+
)
|
146 |
+
|
147 |
+
# Run app
|
148 |
if __name__ == "__main__":
|
149 |
+
app.launch()
|