File size: 3,297 Bytes
cdd4621
 
 
 
 
1e8d05a
 
615f871
cdd4621
 
 
 
 
 
 
1e8d05a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdd4621
1e8d05a
 
 
 
 
 
 
cdd4621
1e8d05a
 
 
 
 
cdd4621
1e8d05a
 
 
 
cdd4621
1e8d05a
 
 
 
 
 
 
 
 
 
 
 
 
 
64614f4
1e8d05a
64614f4
cdd4621
 
8079fc6
 
 
 
1e8d05a
 
 
8079fc6
 
1e8d05a
 
cdd4621
 
 
 
06c8325
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import gradio as gr
import tensorflow as tf
import numpy as np
from tensorflow.keras.preprocessing import image
from PIL import Image
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
import os

# Load the trained model
model = tf.keras.models.load_model("my_keras_model.h5")

# Define image size based on the model's input requirement
image_size = (224, 224)

# Function to analyze injury severity
def analyze_injury(prediction):
    if prediction < 0.3:
        severity = "Mild"
        treatment = "Rest, pain relievers, and follow-up X-ray."
        gov_cost = "₹2,000 - ₹5,000"
        private_cost = "₹10,000 - ₹20,000"
    elif 0.3 <= prediction < 0.7:
        severity = "Moderate"
        treatment = "Plaster cast or splint; possible minor surgery."
        gov_cost = "₹8,000 - ₹15,000"
        private_cost = "₹30,000 - ₹60,000"
    else:
        severity = "Severe"
        treatment = "Major surgery with metal implants, extensive physiotherapy."
        gov_cost = "₹20,000 - ₹50,000"
        private_cost = "₹1,00,000+"
    
    return severity, treatment, gov_cost, private_cost

# Function to generate report
def generate_report(patient_name, age, gender, xray1, xray2):
    # Process X-ray 1
    img1 = Image.open(xray1).resize(image_size)
    img_array1 = image.img_to_array(img1)
    img_array1 = np.expand_dims(img_array1, axis=0) / 255.0  
    prediction1 = model.predict(img_array1)[0][0]

    # Process X-ray 2
    img2 = Image.open(xray2).resize(image_size)
    img_array2 = image.img_to_array(img2)
    img_array2 = np.expand_dims(img_array2, axis=0) / 255.0  
    prediction2 = model.predict(img_array2)[0][0]

    # Get final analysis
    avg_prediction = (prediction1 + prediction2) / 2
    predicted_class = "Fractured" if avg_prediction > 0.5 else "Normal"
    severity, treatment, gov_cost, private_cost = analyze_injury(avg_prediction)

    # Generate PDF
    report_path = f"{patient_name}_fracture_report.pdf"
    c = canvas.Canvas(report_path, pagesize=letter)
    c.setFont("Helvetica", 12)
    c.drawString(100, 750, f"Patient Name: {patient_name}")
    c.drawString(100, 730, f"Age: {age}")
    c.drawString(100, 710, f"Gender: {gender}")
    c.drawString(100, 690, f"Diagnosis: {predicted_class}")
    c.drawString(100, 670, f"Injury Severity: {severity}")
    c.drawString(100, 650, f"Recommended Treatment: {treatment}")
    c.drawString(100, 630, f"Estimated Cost (Govt Hospital): {gov_cost}")
    c.drawString(100, 610, f"Estimated Cost (Private Hospital): {private_cost}")
    
    c.save()

    return report_path

# Define Gradio Interface
interface = gr.Interface(
    fn=generate_report,
    inputs=[
        gr.Textbox(label="Patient Name"),
        gr.Number(label="Age"),
        gr.Radio(["Male", "Female", "Other"], label="Gender"),
        gr.Image(type="file", label="Upload X-ray Image 1"),
        gr.Image(type="file", label="Upload X-ray Image 2"),
    ],
    outputs=gr.File(label="Download Report"),
    title="Bone Fracture Detection & Medical Report",
    description="Enter patient details, upload two X-ray images, and generate a detailed medical report with treatment suggestions and cost estimates."
)

# Launch the Gradio app
if __name__ == "__main__":
    interface.launch()