import os import gradio as gr import tensorflow as tf import numpy as np import cv2 import smtplib import ssl from email.message import EmailMessage from PIL import Image from reportlab.lib.pagesizes import letter from reportlab.pdfgen import canvas from reportlab.lib.utils import ImageReader # Disable GPU to avoid CUDA errors os.environ["TF_ENABLE_ONEDNN_OPTS"] = "0" os.environ["CUDA_VISIBLE_DEVICES"] = "-1" # Load the trained model model = tf.keras.models.load_model("my_keras_model.h5") # Email sender credentials (Set your own credentials here) SENDER_EMAIL = "your_email@gmail.com" SENDER_PASSWORD = "your_email_password" def send_email(receiver_email, file_path): """Function to send an email with the generated PDF attached""" msg = EmailMessage() msg["Subject"] = "Bone Fracture Patient Report" msg["From"] = SENDER_EMAIL msg["To"] = receiver_email msg.set_content("Please find the attached bone fracture report.") # Attach PDF file with open(file_path, "rb") as f: file_data = f.read() msg.add_attachment(file_data, maintype="application", subtype="pdf", filename="Fracture_Report.pdf") # Send email 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) def preprocess_image(image): """Preprocess the image for model prediction""" image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2GRAY) image = cv2.resize(image, (224, 224)) image = np.expand_dims(image, axis=-1) image = np.expand_dims(image, axis=0) / 255.0 return image def generate_pdf(name, age, gender, weight, height, allergies, injury_cause, address, parent_name, image, email): """Generate a PDF report""" file_path = "Fracture_Report.pdf" c = canvas.Canvas(file_path, pagesize=letter) width, height = letter # Title c.setFont("Helvetica-Bold", 16) c.drawCentredString(width / 2, height - 50, "Bone Fracture Patient Report") # Patient Info Table c.setFont("Helvetica", 12) data = [ ["Patient Name:", name[:50]], ["Age:", age], ["Gender:", gender], ["Weight (kg):", weight], ["Height (cm):", height], ["Allergies:", allergies[:100]], ["Injury Cause:", " ".join(injury_cause.split()[:100])], # Limit to 100 words ["Address:", address[:100]], ["Parent/Guardian Name:", parent_name[:50]], ] x_start, y_start = 50, height - 100 line_spacing = 20 for row in data: c.drawString(x_start, y_start, f"{row[0]} {row[1]}") y_start -= line_spacing # Add X-ray image if image: img = Image.open(image) img.thumbnail((250, 250)) img_path = "temp_image.jpg" img.save(img_path) c.drawImage(ImageReader(img_path), width / 2 - 125, y_start - 250, 250, 250) # Close and save the PDF c.save() # Send email if email: send_email(email, file_path) return file_path def predict_and_generate_report(name, age, gender, weight, height, allergies, injury_cause, address, parent_name, image, email): """Make a prediction and generate a report""" if image is None: return "Please upload an X-ray image." # Preprocess and make a prediction processed_image = preprocess_image(image) prediction = model.predict(processed_image) confidence = float(prediction[0][0]) * 100 fracture_status = "Yes" if confidence > 50 else "No" # Generate PDF report pdf_path = generate_pdf(name, age, gender, weight, height, allergies, injury_cause, address, parent_name, image, email) return f"Fractured: {fracture_status} (Confidence: {confidence:.2f}%)", pdf_path # Define the Gradio Interface iface = gr.Interface( fn=predict_and_generate_report, inputs=[ gr.Textbox(label="Patient Name (Max 50 chars)"), gr.Number(label="Age", precision=0), gr.Radio(label="Gender", choices=["Male", "Female", "Other"]), gr.Number(label="Weight (kg)"), gr.Number(label="Height (cm)"), gr.Textbox(label="Allergies (Max 100 chars)"), gr.Textbox(label="Cause of Injury (Max 100 words)"), gr.Textbox(label="Address (Max 100 chars)"), gr.Textbox(label="Parent/Guardian Name (Max 50 chars)"), gr.Image(type="pil", label="Upload X-ray Image"), gr.Textbox(label="Email Address (for Report)"), ], outputs=[ gr.Textbox(label="Fracture Prediction"), gr.File(label="Download Report"), ], title="Bone Fracture Detection System", description="Upload an X-ray image, enter patient details, and generate a fracture report." ) if __name__ == "__main__": iface.launch()