ftx7go commited on
Commit
7bc8b30
·
verified ·
1 Parent(s): 5a94c6e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -32
app.py CHANGED
@@ -34,40 +34,44 @@ def analyze_injury(prediction):
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(
@@ -76,8 +80,8 @@ interface = gr.Interface(
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",
 
34
  return severity, treatment, gov_cost, private_cost
35
 
36
  # Function to generate report
37
+ def generate_report(patient_name, age, gender, xray1_path, xray2_path):
38
+ try:
39
+ # Process X-ray 1
40
+ img1 = Image.open(xray1_path).resize(image_size)
41
+ img_array1 = image.img_to_array(img1)
42
+ img_array1 = np.expand_dims(img_array1, axis=0) / 255.0
43
+ prediction1 = model.predict(img_array1)[0][0]
44
 
45
+ # Process X-ray 2
46
+ img2 = Image.open(xray2_path).resize(image_size)
47
+ img_array2 = image.img_to_array(img2)
48
+ img_array2 = np.expand_dims(img_array2, axis=0) / 255.0
49
+ prediction2 = model.predict(img_array2)[0][0]
50
 
51
+ # Get final analysis
52
+ avg_prediction = (prediction1 + prediction2) / 2
53
+ predicted_class = "Fractured" if avg_prediction > 0.5 else "Normal"
54
+ severity, treatment, gov_cost, private_cost = analyze_injury(avg_prediction)
55
 
56
+ # Generate PDF Report
57
+ report_path = f"{patient_name}_fracture_report.pdf"
58
+ c = canvas.Canvas(report_path, pagesize=letter)
59
+ c.setFont("Helvetica", 12)
60
+ c.drawString(100, 750, f"Patient Name: {patient_name}")
61
+ c.drawString(100, 730, f"Age: {age}")
62
+ c.drawString(100, 710, f"Gender: {gender}")
63
+ c.drawString(100, 690, f"Diagnosis: {predicted_class}")
64
+ c.drawString(100, 670, f"Injury Severity: {severity}")
65
+ c.drawString(100, 650, f"Recommended Treatment: {treatment}")
66
+ c.drawString(100, 630, f"Estimated Cost (Govt Hospital): {gov_cost}")
67
+ c.drawString(100, 610, f"Estimated Cost (Private Hospital): {private_cost}")
68
+
69
+ c.save()
70
+
71
+ return report_path
72
 
73
+ except Exception as e:
74
+ return f"Error generating report: {str(e)}"
75
 
76
  # Define Gradio Interface
77
  interface = gr.Interface(
 
80
  gr.Textbox(label="Patient Name"),
81
  gr.Number(label="Age"),
82
  gr.Radio(["Male", "Female", "Other"], label="Gender"),
83
+ gr.Image(type="filepath", label="Upload X-ray Image 1"),
84
+ gr.Image(type="filepath", label="Upload X-ray Image 2"),
85
  ],
86
  outputs=gr.File(label="Download Report"),
87
  title="Bone Fracture Detection & Medical Report",