ariankhalfani commited on
Commit
1f8c223
1 Parent(s): 8b3a389

Create app4.py

Browse files
Files changed (1) hide show
  1. app4.py +179 -0
app4.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ import cv2
4
+ import numpy as np
5
+ from PIL import Image, ImageDraw, ImageFont
6
+ import base64
7
+ from io import BytesIO
8
+ import zipfile
9
+ import os
10
+ from pathlib import Path
11
+
12
+ # Load YOLOv8 model
13
+ model = YOLO("best.pt")
14
+
15
+ # Define paths for uploaded and predicted images
16
+ uploaded_folder = Path('Uploaded_Picture')
17
+ predicted_folder = Path('Predicted_Picture')
18
+ uploaded_folder.mkdir(parents=True, exist_ok=True)
19
+ predicted_folder.mkdir(parents=True, exist_ok=True)
20
+
21
+ # Path for HTML database file
22
+ html_db_file = Path('patient_predictions.html')
23
+
24
+ # Initialize HTML file if not present
25
+ if not html_db_file.exists():
26
+ with open(html_db_file, 'w') as f:
27
+ f.write("""
28
+ <html>
29
+ <head><title>Patient Prediction Database</title></head>
30
+ <body>
31
+ <h1>Patient Prediction Database</h1>
32
+ <table border="1" style="width:100%; border-collapse: collapse; text-align: center;">
33
+ <thead>
34
+ <tr>
35
+ <th>Name</th>
36
+ <th>Age</th>
37
+ <th>Medical Record</th>
38
+ <th>Sex</th>
39
+ <th>Result</th>
40
+ <th>Predicted Image</th>
41
+ </tr>
42
+ </thead>
43
+ <tbody>
44
+ """)
45
+
46
+ def predict_image(input_image, name, age, medical_record, sex):
47
+ # Ensure input image is provided
48
+ if input_image is None:
49
+ return None, "Please upload an image for prediction."
50
+
51
+ # Convert PIL image to NumPy array
52
+ image_np = np.array(input_image)
53
+
54
+ # Perform YOLO prediction
55
+ results = model(image_np)
56
+ image_with_boxes = image_np.copy()
57
+ label = "Unknown"
58
+
59
+ if results[0].boxes:
60
+ # Take the result with the highest confidence
61
+ best_result = max(results[0].boxes, key=lambda x: x.conf.item())
62
+ class_index = best_result.cls.item()
63
+
64
+ # Determine class label
65
+ if class_index == 0:
66
+ label = "Immature"
67
+ color = (0, 255, 255)
68
+ elif class_index == 1:
69
+ label = "Mature"
70
+ color = (255, 0, 0)
71
+ else:
72
+ label = "Normal"
73
+ color = (0, 255, 0)
74
+
75
+ confidence = best_result.conf.item()
76
+ xmin, ymin, xmax, ymax = map(int, best_result.xyxy[0])
77
+
78
+ # Draw bounding box and label on image
79
+ cv2.rectangle(image_with_boxes, (xmin, ymin), (xmax, ymax), color, 2)
80
+ font_scale, thickness = 1.0, 2
81
+ cv2.putText(image_with_boxes, f'{label} {confidence:.2f}', (xmin, ymin - 10), cv2.FONT_HERSHEY_SIMPLEX, font_scale, color, thickness)
82
+
83
+ # Convert the annotated image back to PIL
84
+ pil_image_with_boxes = Image.fromarray(image_with_boxes)
85
+
86
+ # Save images to folders
87
+ image_name = f"{name}_{age}_{medical_record}_{sex}.png"
88
+ input_image.save(uploaded_folder / image_name)
89
+ pil_image_with_boxes.save(predicted_folder / image_name)
90
+
91
+ # Convert predicted image to base64 for embedding in HTML
92
+ buffered = BytesIO()
93
+ pil_image_with_boxes.save(buffered, format="PNG")
94
+ predicted_image_base64 = base64.b64encode(buffered.getvalue()).decode()
95
+
96
+ # Append patient information to HTML
97
+ append_patient_info_to_html(name, age, medical_record, sex, label, predicted_image_base64)
98
+
99
+ raw_prediction = f"Name: {name}, Age: {age}, Medical Record: {medical_record}, Sex: {sex}, Result: {label}"
100
+
101
+ return pil_image_with_boxes, raw_prediction
102
+
103
+ def append_patient_info_to_html(name, age, medical_record, sex, result, predicted_image_base64):
104
+ # Append a new patient entry to the HTML file
105
+ html_entry = f"""
106
+ <tr>
107
+ <td>{name}</td>
108
+ <td>{age}</td>
109
+ <td>{medical_record}</td>
110
+ <td>{sex}</td>
111
+ <td>{result}</td>
112
+ <td><img src="data:image/png;base64,{predicted_image_base64}" alt="Predicted Image" width="150"></td>
113
+ </tr>
114
+ """
115
+
116
+ with open(html_db_file, 'a') as f:
117
+ f.write(html_entry)
118
+
119
+ # Close the HTML file after writing (for proper structure)
120
+ with open(html_db_file, 'a') as f:
121
+ f.write("</tbody></table></body></html>")
122
+
123
+ return str(html_db_file)
124
+
125
+ def download_uploaded_folder():
126
+ # Create a zip file of the uploaded folder
127
+ zip_path = 'uploaded_images.zip'
128
+ with zipfile.ZipFile(zip_path, 'w') as zf:
129
+ for file in uploaded_folder.iterdir():
130
+ zf.write(file, arcname=file.name)
131
+ return zip_path
132
+
133
+ def download_predicted_folder():
134
+ # Create a zip file of the predicted folder
135
+ zip_path = 'predicted_images.zip'
136
+ with zipfile.ZipFile(zip_path, 'w') as zf:
137
+ for file in predicted_folder.iterdir():
138
+ zf.write(file, arcname=file.name)
139
+ return zip_path
140
+
141
+ # Launch Gradio Interface
142
+ with gr.Blocks() as demo:
143
+ with gr.Column():
144
+ gr.Markdown("# Cataract Detection System")
145
+ gr.Markdown("Upload an image to detect cataract and add patient details.")
146
+ gr.Markdown("This application uses YOLOv8 with mAP=0.981")
147
+
148
+ with gr.Column():
149
+ name = gr.Textbox(label="Name")
150
+ age = gr.Number(label="Age")
151
+ medical_record = gr.Number(label="Medical Record")
152
+ sex = gr.Radio(["Male", "Female"], label="Sex")
153
+ input_image = gr.Image(type="pil", label="Upload an Image", image_mode="RGB")
154
+
155
+ with gr.Column():
156
+ submit_btn = gr.Button("Submit")
157
+ output_image = gr.Image(type="pil", label="Predicted Image")
158
+
159
+ with gr.Row():
160
+ raw_result = gr.Textbox(label="Prediction Result")
161
+
162
+ with gr.Row():
163
+ download_html_btn = gr.Button("Download Patient Information (HTML)")
164
+ download_uploaded_btn = gr.Button("Download Uploaded Images")
165
+ download_predicted_btn = gr.Button("Download Predicted Images")
166
+
167
+ # Add file download output components for the uploaded and predicted images
168
+ patient_info_file = gr.File(label="Patient Information HTML File")
169
+ uploaded_folder_file = gr.File(label="Uploaded Images Zip File")
170
+ predicted_folder_file = gr.File(label="Predicted Images Zip File")
171
+
172
+ # Connect functions with components
173
+ submit_btn.click(fn=predict_image, inputs=[name, age, medical_record, sex, input_image], outputs=[output_image, raw_result])
174
+ download_html_btn.click(fn=append_patient_info_to_html, inputs=[name, age, medical_record, sex, raw_result], outputs=patient_info_file)
175
+ download_uploaded_btn.click(fn=download_uploaded_folder, outputs=uploaded_folder_file)
176
+ download_predicted_btn.click(fn=download_predicted_folder, outputs=predicted_folder_file)
177
+
178
+ # Launch Gradio app
179
+ demo.launch()