|
import os |
|
import smtplib |
|
import ssl |
|
from email.message import EmailMessage |
|
|
|
|
|
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 |
|
|
|
|
|
model = tf.keras.models.load_model("my_keras_model.h5") |
|
|
|
|
|
report_paths = {} |
|
|
|
|
|
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) |
|
|
|
|
|
with open(report_path, "rb") as file: |
|
msg.add_attachment(file.read(), maintype="application", subtype="pdf", filename=os.path.basename(report_path)) |
|
|
|
|
|
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}!" |
|
|
|
|
|
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 |
|
|
|
|
|
prediction = predict_fracture(xray) |
|
diagnosed_class = "Normal" if prediction > 0.5 else "Fractured" |
|
|
|
|
|
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'}") |
|
|
|
|
|
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() |
|
|
|
|
|
report_paths[name] = report_path |
|
|
|
return report_path |
|
|
|
|
|
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") |
|
|
|
|
|
default_xray_path = "default_xray.png" |
|
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") |
|
|
|
|
|
submit_button.click( |
|
generate_report, |
|
inputs=[name, age, gender, weight, height, allergies, cause, xray], |
|
outputs=[output_file] |
|
) |
|
|
|
|
|
send_email_button.click( |
|
send_email, |
|
inputs=[email, name], |
|
outputs=[gr.Textbox(label="Status")] |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
app.launch() |