ftx7go commited on
Commit
1e8d05a
·
verified ·
1 Parent(s): adb5771

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -52
app.py CHANGED
@@ -3,9 +3,9 @@ import tensorflow as tf
3
  import numpy as np
4
  from tensorflow.keras.preprocessing import image
5
  from PIL import Image
 
 
6
  import os
7
- from fpdf import FPDF
8
- import datetime
9
 
10
  # Load the trained model
11
  model = tf.keras.models.load_model("my_keras_model.h5")
@@ -13,56 +13,61 @@ model = tf.keras.models.load_model("my_keras_model.h5")
13
  # Define image size based on the model's input requirement
14
  image_size = (224, 224)
15
 
16
- # Function to make predictions
17
- def predict_image(img):
18
- img = img.resize(image_size) # Resize image to model's expected size
19
- img_array = image.img_to_array(img)
20
- img_array = np.expand_dims(img_array, axis=0) / 255.0 # Normalize
21
- prediction = model.predict(img_array)
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- # Assuming binary classification (fractured or normal)
24
- class_names = ['Fractured', 'Normal']
25
- predicted_class = class_names[int(prediction[0] > 0.5)] # Threshold at 0.5
26
- confidence = prediction[0][0]
 
 
 
27
 
28
- return predicted_class, confidence
 
 
 
 
29
 
30
- # Function to generate a PDF report
31
- def generate_report(name, age, weight, height, img):
32
- # Predict result
33
- predicted_class, confidence = predict_image(img)
34
 
35
- # Create PDF
36
- pdf = FPDF()
37
- pdf.set_auto_page_break(auto=True, margin=15)
38
- pdf.add_page()
39
- pdf.set_font("Arial", size=12)
 
 
 
 
 
 
 
 
 
40
 
41
- # Add title
42
- pdf.set_font("Arial", style='B', size=16)
43
- pdf.cell(200, 10, "Bone Fracture Detection Report", ln=True, align='C')
44
- pdf.ln(10)
45
-
46
- # Add patient details
47
- pdf.set_font("Arial", size=12)
48
- pdf.cell(200, 10, f"Patient Name: {name}", ln=True)
49
- pdf.cell(200, 10, f"Age: {age}", ln=True)
50
- pdf.cell(200, 10, f"Weight: {weight} kg", ln=True)
51
- pdf.cell(200, 10, f"Height: {height} cm", ln=True)
52
- pdf.cell(200, 10, f"Diagnosis Date: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True)
53
- pdf.ln(10)
54
-
55
- # Add prediction result
56
- pdf.set_font("Arial", style='B', size=14)
57
- pdf.cell(200, 10, f"Diagnosis: {predicted_class}", ln=True)
58
- pdf.set_font("Arial", size=12)
59
- pdf.cell(200, 10, f"Confidence: {confidence:.2f}", ln=True)
60
-
61
- # Save PDF
62
- pdf_filename = "patient_report.pdf"
63
- pdf.output(pdf_filename)
64
-
65
- return pdf_filename
66
 
67
  # Define Gradio Interface
68
  interface = gr.Interface(
@@ -70,13 +75,13 @@ interface = gr.Interface(
70
  inputs=[
71
  gr.Textbox(label="Patient Name"),
72
  gr.Number(label="Age"),
73
- gr.Number(label="Weight (kg)"),
74
- gr.Number(label="Height (cm)"),
75
- gr.Image(type="pil", label="X-ray Image")
76
  ],
77
  outputs=gr.File(label="Download Report"),
78
- title="Bone Fracture Detection & Diagnosis",
79
- description="Fill in the patient details and upload an X-ray image. The system will analyze the image and generate a PDF report with the diagnosis."
80
  )
81
 
82
  # Launch the Gradio app
 
3
  import numpy as np
4
  from tensorflow.keras.preprocessing import image
5
  from PIL import Image
6
+ from reportlab.lib.pagesizes import letter
7
+ from reportlab.pdfgen import canvas
8
  import os
 
 
9
 
10
  # Load the trained model
11
  model = tf.keras.models.load_model("my_keras_model.h5")
 
13
  # Define image size based on the model's input requirement
14
  image_size = (224, 224)
15
 
16
+ # Function to analyze injury severity
17
+ def analyze_injury(prediction):
18
+ if prediction < 0.3:
19
+ severity = "Mild"
20
+ treatment = "Rest, pain relievers, and follow-up X-ray."
21
+ gov_cost = "₹2,000 - ₹5,000"
22
+ private_cost = "₹10,000 - ₹20,000"
23
+ elif 0.3 <= prediction < 0.7:
24
+ severity = "Moderate"
25
+ treatment = "Plaster cast or splint; possible minor surgery."
26
+ gov_cost = "₹8,000 - ₹15,000"
27
+ private_cost = "₹30,000 - ₹60,000"
28
+ else:
29
+ severity = "Severe"
30
+ treatment = "Major surgery with metal implants, extensive physiotherapy."
31
+ gov_cost = "₹20,000 - ₹50,000"
32
+ private_cost = "₹1,00,000+"
33
+
34
+ return severity, treatment, gov_cost, private_cost
35
 
36
+ # Function to generate report
37
+ def generate_report(patient_name, age, gender, xray1, xray2):
38
+ # Process X-ray 1
39
+ img1 = Image.open(xray1).resize(image_size)
40
+ img_array1 = image.img_to_array(img1)
41
+ img_array1 = np.expand_dims(img_array1, axis=0) / 255.0
42
+ prediction1 = model.predict(img_array1)[0][0]
43
 
44
+ # Process X-ray 2
45
+ img2 = Image.open(xray2).resize(image_size)
46
+ img_array2 = image.img_to_array(img2)
47
+ img_array2 = np.expand_dims(img_array2, axis=0) / 255.0
48
+ prediction2 = model.predict(img_array2)[0][0]
49
 
50
+ # Get final analysis
51
+ avg_prediction = (prediction1 + prediction2) / 2
52
+ predicted_class = "Fractured" if avg_prediction > 0.5 else "Normal"
53
+ severity, treatment, gov_cost, private_cost = analyze_injury(avg_prediction)
54
 
55
+ # Generate PDF
56
+ report_path = f"{patient_name}_fracture_report.pdf"
57
+ c = canvas.Canvas(report_path, pagesize=letter)
58
+ c.setFont("Helvetica", 12)
59
+ c.drawString(100, 750, f"Patient Name: {patient_name}")
60
+ c.drawString(100, 730, f"Age: {age}")
61
+ c.drawString(100, 710, f"Gender: {gender}")
62
+ c.drawString(100, 690, f"Diagnosis: {predicted_class}")
63
+ c.drawString(100, 670, f"Injury Severity: {severity}")
64
+ c.drawString(100, 650, f"Recommended Treatment: {treatment}")
65
+ c.drawString(100, 630, f"Estimated Cost (Govt Hospital): {gov_cost}")
66
+ c.drawString(100, 610, f"Estimated Cost (Private Hospital): {private_cost}")
67
+
68
+ c.save()
69
 
70
+ return report_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
72
  # Define Gradio Interface
73
  interface = gr.Interface(
 
75
  inputs=[
76
  gr.Textbox(label="Patient Name"),
77
  gr.Number(label="Age"),
78
+ gr.Radio(["Male", "Female", "Other"], label="Gender"),
79
+ gr.Image(type="file", label="Upload X-ray Image 1"),
80
+ gr.Image(type="file", label="Upload X-ray Image 2"),
81
  ],
82
  outputs=gr.File(label="Download Report"),
83
+ title="Bone Fracture Detection & Medical Report",
84
+ description="Enter patient details, upload two X-ray images, and generate a detailed medical report with treatment suggestions and cost estimates."
85
  )
86
 
87
  # Launch the Gradio app