File size: 5,100 Bytes
f4ce306 a06c7aa f4ce306 e90dfc8 a06c7aa e90dfc8 a06c7aa e90dfc8 a06c7aa e90dfc8 a06c7aa f4ce306 a06c7aa f4ce306 e90dfc8 f4ce306 e90dfc8 f4ce306 e90dfc8 f4ce306 e90dfc8 a06c7aa e90dfc8 f4ce306 e90dfc8 f4ce306 e90dfc8 f4ce306 e90dfc8 f4ce306 e90dfc8 f4ce306 e90dfc8 f4ce306 a06c7aa f4ce306 |
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 148 |
import os
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
import smtplib
from email.message import EmailMessage
# Load the trained model
model = tf.keras.models.load_model("my_keras_model.h5")
# Read HTML content from `re.html`
with open("templates/re.html", "r", encoding="utf-8") as file:
html_content = file.read()
# Create reports directory
REPORTS_DIR = "reports"
os.makedirs(REPORTS_DIR, exist_ok=True)
# Email configuration from environment variables
SENDER_EMAIL = os.getenv("SENDER_EMAIL", "[email protected]")
SENDER_PASSWORD = os.getenv("SENDER_PASSWORD", "your-app-password")
# Function to send email with PDF attachment
def send_email_with_attachment(to_email, file_path, patient_name):
msg = EmailMessage()
msg["Subject"] = f"Bone Fracture Report for {patient_name}"
msg["From"] = SENDER_EMAIL
msg["To"] = to_email
msg.set_content(f"Dear {patient_name},\n\nAttached is your bone fracture detection report.\n\nThank you!")
with open(file_path, "rb") as f:
file_data = f.read()
msg.add_attachment(file_data, maintype="application", subtype="pdf", filename="report.pdf")
try:
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
server.login(SENDER_EMAIL, SENDER_PASSWORD)
server.send_message(msg)
print(f"β
Email sent to {to_email}")
except Exception as e:
print(f"β Failed to send email: {e}")
# Function to generate report
def generate_report(name, age, gender, weight, height, allergies, cause, xray, email):
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"
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 = os.path.join(REPORTS_DIR, "xray.png")
img.save(img_path)
# Generate PDF report
report_path = os.path.join(REPORTS_DIR, "report.pdf")
c = canvas.Canvas(report_path, pagesize=letter)
c.setFont("Helvetica-Bold", 16)
c.drawString(200, 770, "Bone Fracture Detection Report")
patient_data = [
["Patient Name", name],
["Age", age],
["Gender", gender],
["Weight", f"{weight} kg"],
["Height", f"{height} cm"],
["Allergies", allergies if allergies else "None"],
["Cause of Injury", cause if cause else "Not Provided"],
["Diagnosis", diagnosed_class],
["Injury Severity", severity]
]
# Format and align tables
def format_table(data):
table = Table(data, colWidths=[270, 270])
table.setStyle(TableStyle([
('BACKGROUND', (0, 0), (-1, 0), colors.darkblue),
('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke),
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'),
('BOTTOMPADDING', (0, 0), (-1, 0), 12),
('GRID', (0, 0), (-1, -1), 1, colors.black),
('VALIGN', (0, 0), (-1, -1), 'MIDDLE')
]))
return table
patient_table = format_table(patient_data)
patient_table.wrapOn(c, 480, 500)
patient_table.drawOn(c, 50, 620)
c.drawInlineImage(img_path, 50, 320, width=250, height=250)
c.setFont("Helvetica-Bold", 12)
c.drawString(120, 290, f"Fractured: {'Yes' if diagnosed_class == 'Fractured' else 'No'}")
c.save()
# Send report via email
send_email_with_attachment(email, report_path, name)
return report_path
# Define Gradio Interface
with gr.Blocks() as app:
gr.HTML(html_content)
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 (if any)")
cause = gr.Textbox(label="Cause of Injury")
email = gr.Textbox(label="Patient Email")
with gr.Row():
xray = gr.Image(type="filepath", label="Upload X-ray Image")
submit_button = gr.Button("Generate Report")
output_file = gr.File(label="Download Report")
submit_button.click(
generate_report,
inputs=[name, age, gender, weight, height, allergies, cause, xray, email],
outputs=[output_file],
)
if __name__ == "__main__":
app.launch() |