File size: 4,311 Bytes
f2ca99e
0a42e87
 
 
 
752f355
434ed8b
f2ca99e
 
 
 
 
 
 
 
 
 
 
 
752f355
e1eb8aa
f2ca99e
752f355
e1eb8aa
752f355
e1eb8aa
 
 
 
0a42e87
 
 
 
752f355
0a42e87
 
 
 
 
 
 
752f355
0a42e87
 
 
752f355
0a42e87
 
 
 
 
29c4191
 
e1eb8aa
41658fe
e1eb8aa
 
 
f2ca99e
 
 
 
 
 
 
 
 
 
 
434ed8b
f2ca99e
752f355
f2ca99e
 
 
 
 
 
 
434ed8b
 
 
 
 
e1eb8aa
f2ca99e
 
752f355
e1eb8aa
 
 
f2ca99e
752f355
f2ca99e
 
29c4191
f2ca99e
 
 
 
29c4191
f2ca99e
 
 
 
434ed8b
 
 
 
0a42e87
 
 
752f355
 
 
434ed8b
f2ca99e
434ed8b
 
f2ca99e
 
 
434ed8b
e1eb8aa
 
 
 
 
 
434ed8b
e1eb8aa
 
 
 
 
41658fe
752f355
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
import os
import smtplib
import ssl
from email.message import EmailMessage

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

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

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

# Store generated report paths
report_paths = {}

# Function to send email
def send_email(patient_email, patient_name):
    if patient_name not in report_paths or not os.path.exists(report_paths[patient_name]):
        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},\n\nYour bone fracture diagnosis report is attached.\n\nBest Regards,\nHospital Team"

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

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

    # Send email securely
    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"

    # Generate PDF
    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'}")

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

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

    # Store file path for sending email
    report_paths[name] = report_path

    return report_path  # Return file path

# 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():
        allergies = gr.Textbox(label="Allergies")
        cause = gr.Textbox(label="Cause of Injury")

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

    # Preloaded X-ray image
    default_xray_path = "default_xray.png"  # Ensure this file exists in your project
    xray = gr.Image(type="filepath", label="Upload X-ray Image", value=default_xray_path)

    with gr.Row():
        submit_button = gr.Button("Generate Report")
        send_email_button = gr.Button("Send Report via Email")

    output_file = gr.File(label="Download Report")

    # Generate Report Button
    submit_button.click(
        generate_report,
        inputs=[name, age, gender, weight, height, allergies, cause, xray],
        outputs=[output_file]
    )

    # Send Email Button
    send_email_button.click(
        send_email,
        inputs=[email, name],
        outputs=[gr.Textbox(label="Status")]
    )

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