Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,8 @@ import numpy as np
|
|
4 |
from tensorflow.keras.preprocessing import image
|
5 |
from PIL import Image
|
6 |
import os
|
|
|
|
|
7 |
|
8 |
# Load the trained model
|
9 |
model = tf.keras.models.load_model("my_keras_model.h5")
|
@@ -21,45 +23,60 @@ def predict_image(img):
|
|
21 |
# Assuming binary classification (fractured or normal)
|
22 |
class_names = ['Fractured', 'Normal']
|
23 |
predicted_class = class_names[int(prediction[0] > 0.5)] # Threshold at 0.5
|
|
|
24 |
|
25 |
-
return
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
-
|
|
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
-
|
|
|
|
|
34 |
|
35 |
-
#
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
# Define Gradio Interface
|
44 |
interface = gr.Interface(
|
45 |
-
fn=
|
46 |
-
inputs=
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
- The
|
56 |
-
|
57 |
-
## Capabilities
|
58 |
-
- Detects bone fractures from X-ray images.
|
59 |
-
- Uses a trained deep learning model for classification.
|
60 |
-
|
61 |
-
{get_download_links()}
|
62 |
-
"""
|
63 |
)
|
64 |
|
65 |
# Launch the Gradio app
|
|
|
4 |
from tensorflow.keras.preprocessing import image
|
5 |
from PIL import Image
|
6 |
import os
|
7 |
+
from fpdf import FPDF
|
8 |
+
import datetime
|
9 |
|
10 |
# Load the trained model
|
11 |
model = tf.keras.models.load_model("my_keras_model.h5")
|
|
|
23 |
# Assuming binary classification (fractured or normal)
|
24 |
class_names = ['Fractured', 'Normal']
|
25 |
predicted_class = class_names[int(prediction[0] > 0.5)] # Threshold at 0.5
|
26 |
+
confidence = prediction[0][0]
|
27 |
|
28 |
+
return predicted_class, confidence
|
29 |
|
30 |
+
# Function to generate a PDF report
|
31 |
+
def generate_report(name, age, weight, height, img):
|
32 |
+
# Predict result
|
33 |
+
predicted_class, confidence = predict_image(img)
|
34 |
|
35 |
+
# Create PDF
|
36 |
+
pdf = FPDF()
|
37 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
38 |
+
pdf.add_page()
|
39 |
+
pdf.set_font("Arial", size=12)
|
40 |
|
41 |
+
# Add title
|
42 |
+
pdf.set_font("Arial", style='B', size=16)
|
43 |
+
pdf.cell(200, 10, "Bone Fracture Detection Report", ln=True, align='C')
|
44 |
+
pdf.ln(10)
|
45 |
+
|
46 |
+
# Add patient details
|
47 |
+
pdf.set_font("Arial", size=12)
|
48 |
+
pdf.cell(200, 10, f"Patient Name: {name}", ln=True)
|
49 |
+
pdf.cell(200, 10, f"Age: {age}", ln=True)
|
50 |
+
pdf.cell(200, 10, f"Weight: {weight} kg", ln=True)
|
51 |
+
pdf.cell(200, 10, f"Height: {height} cm", ln=True)
|
52 |
+
pdf.cell(200, 10, f"Diagnosis Date: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True)
|
53 |
+
pdf.ln(10)
|
54 |
+
|
55 |
+
# Add prediction result
|
56 |
+
pdf.set_font("Arial", style='B', size=14)
|
57 |
+
pdf.cell(200, 10, f"Diagnosis: {predicted_class}", ln=True)
|
58 |
+
pdf.set_font("Arial", size=12)
|
59 |
+
pdf.cell(200, 10, f"Confidence: {confidence:.2f}", ln=True)
|
60 |
+
|
61 |
+
# Save PDF
|
62 |
+
pdf_filename = "patient_report.pdf"
|
63 |
+
pdf.output(pdf_filename)
|
64 |
+
|
65 |
+
return pdf_filename
|
66 |
|
67 |
# Define Gradio Interface
|
68 |
interface = gr.Interface(
|
69 |
+
fn=generate_report,
|
70 |
+
inputs=[
|
71 |
+
gr.Textbox(label="Patient Name"),
|
72 |
+
gr.Number(label="Age"),
|
73 |
+
gr.Number(label="Weight (kg)"),
|
74 |
+
gr.Number(label="Height (cm)"),
|
75 |
+
gr.Image(type="pil", label="X-ray Image")
|
76 |
+
],
|
77 |
+
outputs=gr.File(label="Download Report"),
|
78 |
+
title="Bone Fracture Detection & Diagnosis",
|
79 |
+
description="Fill in the patient details and upload an X-ray image. The system will analyze the image and generate a PDF report with the diagnosis."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
)
|
81 |
|
82 |
# Launch the Gradio app
|