File size: 4,399 Bytes
f2ca99e
0a42e87
 
 
 
f2ca99e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1eb8aa
 
f2ca99e
0a42e87
e1eb8aa
 
 
 
 
 
0a42e87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29c4191
 
e1eb8aa
41658fe
e1eb8aa
 
 
f2ca99e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e1eb8aa
f2ca99e
 
 
e1eb8aa
 
 
 
f2ca99e
 
 
 
29c4191
f2ca99e
 
 
 
29c4191
f2ca99e
 
 
 
0a42e87
 
 
f2ca99e
 
 
 
41658fe
f2ca99e
 
e1eb8aa
 
 
 
 
 
 
 
 
 
 
 
 
41658fe
f2ca99e
 
 
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
90
91
92
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
import smtplib
import ssl
from email.message import EmailMessage

os.environ["CUDA_VISIBLE_DEVICES"] = "-1"  # Force TensorFlow to use CPU

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
from reportlab.lib import colors
from reportlab.platypus import Table, TableStyle

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

# Store generated report file path
report_paths = {}

# Function to send email
def send_email(patient_email, patient_name):
    if patient_name not in report_paths:
        return "Error: Generate the report first before sending."

    report_path = report_paths[patient_name]

    sender_email = "[email protected]"
    sender_password = "your_email_password"

    subject = f"Bone Fracture Report for {patient_name}"
    body = f"""
    Dear {patient_name},

    Your bone fracture diagnosis report is attached.

    If you have any concerns, consult your doctor.

    Regards,
    Hospital Team
    """

    msg = EmailMessage()
    msg["From"] = sender_email
    msg["To"] = patient_email
    msg["Subject"] = subject
    msg.set_content(body)

    # Attach PDF file
    with open(report_path, "rb") as file:
        msg.add_attachment(file.read(), maintype="application", subtype="pdf", filename=os.path.basename(report_path))

    # Secure email sending
    context = ssl.create_default_context()
    with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server:
        server.login(sender_email, sender_password)
        server.send_message(msg)

    return f"Report sent successfully to {patient_email}!"

# Function to generate report
def generate_report(name, age, gender, weight, height, allergies, cause, xray):
    if not name:
        return "Error: Please enter a patient name."

    image_size = (224, 224)

    def predict_fracture(xray_path):
        img = Image.open(xray_path).resize(image_size)
        img_array = image.img_to_array(img) / 255.0
        img_array = np.expand_dims(img_array, axis=0)
        prediction = model.predict(img_array)[0][0]
        return prediction

    # Predict fracture
    prediction = predict_fracture(xray)
    diagnosed_class = "normal" if prediction > 0.5 else "Fractured"

    # Injury severity classification
    severity = "Mild" if prediction < 0.3 else "Moderate" if prediction < 0.7 else "Severe"

    # Save X-ray image for report
    img = Image.open(xray).resize((300, 300))
    img_path = f"{name}_xray.png"
    img.save(img_path)

    # Generate PDF report
    report_path = f"{name}_fracture_report.pdf"
    c = canvas.Canvas(report_path, pagesize=letter)
    
    c.setFont("Helvetica-Bold", 16)
    c.drawString(200, 770, "Bone Fracture Detection Report")

    c.drawString(120, 290, f"Fractured: {'Yes' if diagnosed_class == 'Fractured' else 'No'}")

    c.drawInlineImage(img_path, 50, 320, width=250, height=250)

    c.save()

    # Store the file path
    report_paths[name] = report_path

    return report_path  # Return file path

# Define Gradio Interface
with gr.Blocks() as app:
    gr.Markdown("## Bone Fracture Detection System")

    with gr.Row():
        name = gr.Textbox(label="Patient Name")
        age = gr.Number(label="Age")
        gender = gr.Radio(["Male", "Female", "Other"], label="Gender")

    with gr.Row():
        weight = gr.Number(label="Weight (kg)")
        height = gr.Number(label="Height (cm)")

    with gr.Row():
        email = gr.Textbox(label="Patient Email", type="email")

    with gr.Row():
        xray = gr.Image(type="filepath", label="Upload X-ray Image")

    submit_button = gr.Button("Generate Report")
    send_email_button = gr.Button("Send Report via Email")
    output_file = gr.File(label="Download Report")

    # When clicking "Generate Report", save the file path and allow downloading
    submit_button.click(
        generate_report,
        inputs=[name, age, gender, weight, height, allergies, cause, xray],
        outputs=[output_file]
    )

    # When clicking "Send Report via Email", send the stored report file
    send_email_button.click(
        send_email,
        inputs=[email, name],
        outputs=[gr.Textbox(label="Status")]
    )

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