ftx7go commited on
Commit
37c3266
·
verified ·
1 Parent(s): de1919e

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +162 -0
  2. requirements.txt +10 -0
app.py ADDED
@@ -0,0 +1,162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Force TensorFlow to use CPU
3
+
4
+ import gradio as gr
5
+ import tensorflow as tf
6
+ import numpy as np
7
+ from tensorflow.keras.preprocessing import image
8
+ from PIL import Image
9
+ from reportlab.lib.pagesizes import letter
10
+ from reportlab.pdfgen import canvas
11
+ from reportlab.lib import colors
12
+ from reportlab.platypus import Table, TableStyle
13
+
14
+ # Load the trained model
15
+ model = tf.keras.models.load_model("my_keras_model.h5")
16
+
17
+ # Read HTML content from `re.html`
18
+ with open("templates/re.html", "r", encoding="utf-8") as file:
19
+ html_content = file.read()
20
+
21
+ # List of sample images
22
+ sample_images = [f"samples/{img}" for img in os.listdir("samples") if img.endswith((".png", ".jpg", ".jpeg"))]
23
+
24
+ # Function to process X-ray and generate a PDF report
25
+ def generate_report(name, age, gender, weight, height, allergies, cause, xray):
26
+ image_size = (224, 224)
27
+
28
+ def predict_fracture(xray_path):
29
+ img = Image.open(xray_path).resize(image_size)
30
+ img_array = image.img_to_array(img) / 255.0
31
+ img_array = np.expand_dims(img_array, axis=0)
32
+ prediction = model.predict(img_array)[0][0]
33
+ return prediction
34
+
35
+ # Predict fracture
36
+ prediction = predict_fracture(xray)
37
+ diagnosed_class = "normal" if prediction > 0.5 else "Fractured"
38
+
39
+ # Injury severity classification
40
+ severity = "Mild" if prediction < 0.3 else "Moderate" if prediction < 0.7 else "Severe"
41
+
42
+ # Treatment details table
43
+ treatment_data = [
44
+ ["Severity Level", "Recommended Treatment", "Recovery Duration"],
45
+ ["Mild", "Rest, pain relievers, and follow-up X-ray", "4-6 weeks"],
46
+ ["Moderate", "Plaster cast, minor surgery if needed", "6-10 weeks"],
47
+ ["Severe", "Major surgery, metal implants, physiotherapy", "Several months"]
48
+ ]
49
+
50
+ # Estimated cost & duration table
51
+ cost_duration_data = [
52
+ ["Hospital Type", "Estimated Cost", "Recovery Time"],
53
+ ["Government Hospital", f"₹{2000 if severity == 'Mild' else 8000 if severity == 'Moderate' else 20000} - ₹{5000 if severity == 'Mild' else 15000 if severity == 'Moderate' else 50000}", "4-12 weeks"],
54
+ ["Private Hospital", f"₹{10000 if severity == 'Mild' else 30000 if severity == 'Moderate' else 100000}+", "6 weeks - Several months"]
55
+ ]
56
+
57
+ # Save X-ray image for report
58
+ img = Image.open(xray).resize((300, 300))
59
+ img_path = f"{name}_xray.png"
60
+ img.save(img_path)
61
+
62
+ # Generate PDF report
63
+ report_path = f"{name}_fracture_report.pdf"
64
+ c = canvas.Canvas(report_path, pagesize=letter)
65
+
66
+ # Report title
67
+ c.setFont("Helvetica-Bold", 16)
68
+ c.drawString(200, 770, "Bone Fracture Detection Report")
69
+
70
+ # Patient details table
71
+ patient_data = [
72
+ ["Patient Name", name],
73
+ ["Age", age],
74
+ ["Gender", gender],
75
+ ["Weight", f"{weight} kg"],
76
+ ["Height", f"{height} cm"],
77
+ ["Allergies", allergies if allergies else "None"],
78
+ ["Cause of Injury", cause if cause else "Not Provided"],
79
+ ["Diagnosis", diagnosed_class],
80
+ ["Injury Severity", severity]
81
+ ]
82
+
83
+ # Format and align tables
84
+ def format_table(data):
85
+ table = Table(data, colWidths=[270, 270]) # Set 90% width
86
+ table.setStyle(TableStyle([
87
+ ('BACKGROUND', (0, 0), (-1, 0), colors.darkblue),
88
+ ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
89
+ ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
90
+ ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
91
+ ('BOTTOMPADDING', (0, 0), (-1, 0), 12),
92
+ ('GRID', (0, 0), (-1, -1), 1, colors.black),
93
+ ('VALIGN', (0, 0), (-1, -1), 'MIDDLE')
94
+ ]))
95
+ return table
96
+
97
+ # Draw patient details table
98
+ patient_table = format_table(patient_data)
99
+ patient_table.wrapOn(c, 480, 500)
100
+ patient_table.drawOn(c, 50, 620)
101
+
102
+ # Load and insert X-ray image
103
+ c.drawInlineImage(img_path, 50, 320, width=250, height=250)
104
+ c.setFont("Helvetica-Bold", 12)
105
+ c.drawString(120, 290, f"Fractured: {'Yes' if diagnosed_class == 'Fractured' else 'No'}")
106
+
107
+ # Draw treatment and cost tables
108
+ treatment_table = format_table(treatment_data)
109
+ treatment_table.wrapOn(c, 480, 200)
110
+ treatment_table.drawOn(c, 50, 200)
111
+
112
+ cost_table = format_table(cost_duration_data)
113
+ cost_table.wrapOn(c, 480, 150)
114
+ cost_table.drawOn(c, 50, 80)
115
+
116
+ c.save()
117
+
118
+ return report_path # Return path for auto-download
119
+
120
+ # Function to select a sample image
121
+ def use_sample_image(sample_image_path):
122
+ return sample_image_path # Returns selected sample image filepath
123
+
124
+ # Define Gradio Interface
125
+ with gr.Blocks() as app:
126
+ gr.HTML(html_content) # Display `re.html` content in Gradio
127
+ gr.Markdown("## Bone Fracture Detection System")
128
+
129
+ with gr.Row():
130
+ name = gr.Textbox(label="Patient Name")
131
+ age = gr.Number(label="Age")
132
+ gender = gr.Radio(["Male", "Female", "Other"], label="Gender")
133
+
134
+ with gr.Row():
135
+ weight = gr.Number(label="Weight (kg)")
136
+ height = gr.Number(label="Height (cm)")
137
+
138
+ with gr.Row():
139
+ allergies = gr.Textbox(label="Allergies (if any)")
140
+ cause = gr.Textbox(label="Cause of Injury")
141
+
142
+ with gr.Row():
143
+ xray = gr.Image(type="filepath", label="Upload X-ray Image")
144
+
145
+ with gr.Row():
146
+ sample_selector = gr.Dropdown(choices=sample_images, label="Use Sample Image")
147
+ select_button = gr.Button("Load Sample Image")
148
+
149
+ submit_button = gr.Button("Generate Report")
150
+ output_file = gr.File(label="Download Report")
151
+
152
+ select_button.click(use_sample_image, inputs=[sample_selector], outputs=[xray])
153
+
154
+ submit_button.click(
155
+ generate_report,
156
+ inputs=[name, age, gender, weight, height, allergies, cause, xray],
157
+ outputs=[output_file],
158
+ )
159
+
160
+ # Launch the Gradio app
161
+ if __name__ == "__main__":
162
+ app.launch()
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ flask
2
+ numpy
3
+ opencv-python-headless
4
+ fpdf
5
+ torch
6
+ torchvision
7
+ gradio
8
+ tensorflow
9
+ Pillow
10
+ reportlab