ftx7go commited on
Commit
f494b68
·
verified ·
1 Parent(s): 554d7bc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -0
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
8
+ from reportlab.pdfgen import canvas
9
+ import os
10
+
11
+ # Load the trained model
12
+ model = tf.keras.models.load_model("my_keras_model.h5")
13
+
14
+ # Define image size
15
+ image_size = (224, 224)
16
+
17
+ # Function to detect fracture and get bounding box
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 injury severity
32
+ def analyze_injury(prediction):
33
+ if prediction < 0.3:
34
+ severity = "Mild"
35
+ treatment = "Rest, pain relievers, and follow-up X-ray."
36
+ gov_cost = "₹2,000 - ₹5,000"
37
+ private_cost = "₹10,000 - ₹20,000"
38
+ elif 0.3 <= prediction < 0.7:
39
+ severity = "Moderate"
40
+ treatment = "Plaster cast or splint; possible minor surgery."
41
+ gov_cost = "₹8,000 - ₹15,000"
42
+ private_cost = "₹30,000 - ₹60,000"
43
+ else:
44
+ severity = "Severe"
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
+
57
+ avg_prediction = (prediction1 + prediction2) / 2
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 is detected
62
+ if bbox1:
63
+ draw = ImageDraw.Draw(img1)
64
+ draw.rectangle(bbox1, outline="red", width=5)
65
+
66
+ if bbox2:
67
+ draw = ImageDraw.Draw(img2)
68
+ draw.rectangle(bbox2, outline="red", width=5)
69
+
70
+ # Save modified images
71
+ img1_path = f"{name}_xray1.png"
72
+ img2_path = f"{name}_xray2.png"
73
+ img1.save(img1_path)
74
+ img2.save(img2_path)
75
+
76
+ # Generate PDF report
77
+ report_path = f"{name}_fracture_report.pdf"
78
+ c = canvas.Canvas(report_path, pagesize=letter)
79
+ c.setFont("Helvetica", 12)
80
+
81
+ c.drawString(100, 750, f"Patient Name: {name}")
82
+ c.drawString(100, 730, f"Age: {age}")
83
+ c.drawString(100, 710, f"Gender: {gender}")
84
+ c.drawString(100, 690, f"Diagnosis: {diagnosed_class}")
85
+ c.drawString(100, 670, f"Injury Severity: {severity}")
86
+ c.drawString(100, 650, f"Recommended Treatment: {treatment}")
87
+ c.drawString(100, 630, f"Estimated Cost (Govt Hospital): {gov_cost}")
88
+ c.drawString(100, 610, f"Estimated Cost (Private Hospital): {private_cost}")
89
+
90
+ c.save()
91
+
92
+ return report_path, img1_path, img2_path, diagnosed_class, severity, treatment, gov_cost, private_cost
93
+
94
+ # Define Gradio interface
95
+ interface = gr.Interface(
96
+ fn=generate_report,
97
+ inputs=[
98
+ gr.Textbox(label="Patient Name"),
99
+ gr.Number(label="Age"),
100
+ gr.Radio(["Male", "Female", "Other"], label="Gender"),
101
+ gr.Image(type="file", label="Upload X-ray Image 1"),
102
+ gr.Image(type="file", label="Upload X-ray Image 2"),
103
+ ],
104
+ outputs=[
105
+ gr.File(label="Download Report"),
106
+ gr.Image(label="X-ray 1 with Fracture Highlight"),
107
+ gr.Image(label="X-ray 2 with Fracture Highlight"),
108
+ gr.Textbox(label="Fracture Detected"),
109
+ gr.Textbox(label="Injury Severity"),
110
+ gr.Textbox(label="Recommended Treatment"),
111
+ gr.Textbox(label="Estimated Cost (Govt Hospital)"),
112
+ gr.Textbox(label="Estimated Cost (Private Hospital)")
113
+ ],
114
+ title="Bone Fracture Detection & Medical Report",
115
+ description="Enter patient details, upload two X-ray images, and generate a detailed medical report with treatment suggestions and cost estimates."
116
+ )
117
+
118
+ # Launch the app
119
+ if __name__ == "__main__":
120
+ interface.launch()