Spaces:
Sleeping
Sleeping
ariankhalfani
commited on
Commit
•
657afd9
1
Parent(s):
749de3f
Update app.py
Browse files
app.py
CHANGED
@@ -3,12 +3,10 @@ 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 tempfile
|
9 |
import os
|
10 |
from pathlib import Path
|
11 |
import shutil
|
|
|
12 |
|
13 |
# Load YOLOv8 model
|
14 |
model = YOLO("best.pt")
|
@@ -19,10 +17,9 @@ predicted_folder = Path('Predicted_Picture')
|
|
19 |
uploaded_folder.mkdir(parents=True, exist_ok=True)
|
20 |
predicted_folder.mkdir(parents=True, exist_ok=True)
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
|
25 |
-
# Function to predict image and add bounding box, text, circle, and watermark
|
26 |
def predict_image(input_image, name, age, medical_record, sex):
|
27 |
if input_image is None:
|
28 |
return None, "Please Input The Image"
|
@@ -39,7 +36,7 @@ def predict_image(input_image, name, age, medical_record, sex):
|
|
39 |
# Perform prediction
|
40 |
results = model(image_np)
|
41 |
|
42 |
-
# Draw bounding boxes on the image
|
43 |
image_with_boxes = image_np.copy()
|
44 |
raw_predictions = []
|
45 |
|
@@ -65,17 +62,13 @@ def predict_image(input_image, name, age, medical_record, sex):
|
|
65 |
# Draw the bounding box
|
66 |
cv2.rectangle(image_with_boxes, (xmin, ymin), (xmax, ymax), color, 2)
|
67 |
|
68 |
-
#
|
69 |
-
center_x = (xmin + xmax) // 2
|
70 |
-
center_y = (ymin + ymax) // 2
|
71 |
-
|
72 |
-
# Calculate the radius (1/12 of the average of the width and height of the bounding box)
|
73 |
box_width = xmax - xmin
|
74 |
box_height = ymax - ymin
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
cv2.circle(image_with_boxes, (center_x, center_y), radius, (255, 255, 255),
|
79 |
|
80 |
# Enlarge font scale and thickness
|
81 |
font_scale = 1.0
|
@@ -105,31 +98,6 @@ def predict_image(input_image, name, age, medical_record, sex):
|
|
105 |
|
106 |
return pil_image_with_boxes, raw_predictions_str
|
107 |
|
108 |
-
# Function to add watermark
|
109 |
-
def add_watermark(image):
|
110 |
-
try:
|
111 |
-
logo = Image.open('image-logo.png').convert("RGBA")
|
112 |
-
image = image.convert("RGBA")
|
113 |
-
|
114 |
-
# Resize logo
|
115 |
-
basewidth = 100
|
116 |
-
wpercent = (basewidth / float(logo.size[0]))
|
117 |
-
hsize = int((float(wpercent) * logo.size[1]))
|
118 |
-
logo = logo.resize((basewidth, hsize), Image.LANCZOS)
|
119 |
-
|
120 |
-
# Position logo
|
121 |
-
position = (image.width - logo.width - 10, image.height - logo.height - 10)
|
122 |
-
|
123 |
-
# Composite image
|
124 |
-
transparent = Image.new('RGBA', (image.width, image.height), (0, 0, 0, 0))
|
125 |
-
transparent.paste(image, (0, 0))
|
126 |
-
transparent.paste(logo, position, mask=logo)
|
127 |
-
|
128 |
-
return transparent.convert("RGB")
|
129 |
-
except Exception as e:
|
130 |
-
print(f"Error adding watermark: {e}")
|
131 |
-
return image
|
132 |
-
|
133 |
# Function to add text and watermark
|
134 |
def add_text_and_watermark(image, name, age, medical_record, sex, label):
|
135 |
draw = ImageDraw.Draw(image)
|
@@ -160,36 +128,29 @@ def add_text_and_watermark(image, name, age, medical_record, sex, label):
|
|
160 |
# Draw text on top of the rectangle
|
161 |
draw.text((text_x, text_y), text, fill=(255, 255, 255, 255), font=font)
|
162 |
|
163 |
-
|
164 |
-
image_with_watermark = add_watermark(image)
|
165 |
-
|
166 |
-
return image_with_watermark
|
167 |
|
168 |
# Function to save patient info in HTML and accumulate data
|
169 |
def save_patient_info_to_html(name, age, medical_record, sex, result):
|
|
|
|
|
|
|
|
|
170 |
html_content = f"""
|
171 |
<html>
|
172 |
<body>
|
173 |
<h1>Patient Information</h1>
|
174 |
-
|
175 |
-
<p><strong>Age:</strong> {age}</p>
|
176 |
-
<p><strong>Medical Record:</strong> {medical_record}</p>
|
177 |
-
<p><strong>Sex:</strong> {sex}</p>
|
178 |
-
<p><strong>Result:</strong> {result}</p>
|
179 |
-
<hr>
|
180 |
</body>
|
181 |
</html>
|
182 |
"""
|
183 |
|
184 |
-
#
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
else:
|
189 |
-
with open(html_file_path, 'w') as f:
|
190 |
-
f.write(html_content)
|
191 |
|
192 |
-
return
|
193 |
|
194 |
# Function to download the folders
|
195 |
def download_folder(folder):
|
@@ -201,28 +162,6 @@ def download_folder(folder):
|
|
201 |
return zip_path
|
202 |
|
203 |
# Gradio Interface
|
204 |
-
def interface(name, age, medical_record, sex, input_image):
|
205 |
-
if input_image is None:
|
206 |
-
return None, "Please upload an image.", None
|
207 |
-
|
208 |
-
output_image, raw_result = predict_image(input_image, name, age, medical_record, sex)
|
209 |
-
|
210 |
-
if output_image is None:
|
211 |
-
return None, raw_result, None
|
212 |
-
|
213 |
-
# Save patient info to HTML
|
214 |
-
html_file_path = save_patient_info_to_html(name, age, medical_record, sex, raw_result)
|
215 |
-
|
216 |
-
# Encode the image to display in Gradio
|
217 |
-
buffered = BytesIO()
|
218 |
-
output_image.save(buffered, format="PNG")
|
219 |
-
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
220 |
-
|
221 |
-
# Provide the zip file path for download
|
222 |
-
zip_file = download_folder(predicted_folder)
|
223 |
-
|
224 |
-
return f'<img src="data:image/png;base64,{img_str}" alt="Processed Image"/>', raw_result, zip_file
|
225 |
-
|
226 |
with gr.Blocks() as demo:
|
227 |
with gr.Column():
|
228 |
gr.Markdown("# Cataract Detection System")
|
@@ -253,10 +192,10 @@ with gr.Blocks() as demo:
|
|
253 |
uploaded_folder_file = gr.File(label="Uploaded Images Zip File")
|
254 |
predicted_folder_file = gr.File(label="Predicted Images Zip File")
|
255 |
|
256 |
-
submit_btn.click(fn=
|
257 |
download_html_btn.click(fn=save_patient_info_to_html, inputs=[name, age, medical_record, sex, raw_result], outputs=patient_info_file)
|
258 |
-
download_uploaded_btn.click(fn=
|
259 |
-
download_predicted_btn.click(fn=
|
260 |
|
261 |
# Launch Gradio app
|
262 |
demo.launch()
|
|
|
3 |
import cv2
|
4 |
import numpy as np
|
5 |
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
|
|
|
6 |
import os
|
7 |
from pathlib import Path
|
8 |
import shutil
|
9 |
+
import tempfile
|
10 |
|
11 |
# Load YOLOv8 model
|
12 |
model = YOLO("best.pt")
|
|
|
17 |
uploaded_folder.mkdir(parents=True, exist_ok=True)
|
18 |
predicted_folder.mkdir(parents=True, exist_ok=True)
|
19 |
|
20 |
+
# Global patient data list to accumulate HTML data
|
21 |
+
patient_data = []
|
22 |
|
|
|
23 |
def predict_image(input_image, name, age, medical_record, sex):
|
24 |
if input_image is None:
|
25 |
return None, "Please Input The Image"
|
|
|
36 |
# Perform prediction
|
37 |
results = model(image_np)
|
38 |
|
39 |
+
# Draw bounding boxes and white circle on the image
|
40 |
image_with_boxes = image_np.copy()
|
41 |
raw_predictions = []
|
42 |
|
|
|
62 |
# Draw the bounding box
|
63 |
cv2.rectangle(image_with_boxes, (xmin, ymin), (xmax, ymax), color, 2)
|
64 |
|
65 |
+
# Draw the white circle in the center of the bounding box
|
|
|
|
|
|
|
|
|
66 |
box_width = xmax - xmin
|
67 |
box_height = ymax - ymin
|
68 |
+
center_x = xmin + box_width // 2
|
69 |
+
center_y = ymin + box_height // 2
|
70 |
+
radius = int((box_width + box_height) / 2 / 12)
|
71 |
+
cv2.circle(image_with_boxes, (center_x, center_y), radius, (255, 255, 255), 2)
|
72 |
|
73 |
# Enlarge font scale and thickness
|
74 |
font_scale = 1.0
|
|
|
98 |
|
99 |
return pil_image_with_boxes, raw_predictions_str
|
100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
# Function to add text and watermark
|
102 |
def add_text_and_watermark(image, name, age, medical_record, sex, label):
|
103 |
draw = ImageDraw.Draw(image)
|
|
|
128 |
# Draw text on top of the rectangle
|
129 |
draw.text((text_x, text_y), text, fill=(255, 255, 255, 255), font=font)
|
130 |
|
131 |
+
return image
|
|
|
|
|
|
|
132 |
|
133 |
# Function to save patient info in HTML and accumulate data
|
134 |
def save_patient_info_to_html(name, age, medical_record, sex, result):
|
135 |
+
global patient_data
|
136 |
+
new_data = f"<p><strong>Name:</strong> {name}, <strong>Age:</strong> {age}, <strong>Medical Record:</strong> {medical_record}, <strong>Sex:</strong> {sex}, <strong>Result:</strong> {result}</p>"
|
137 |
+
patient_data.append(new_data)
|
138 |
+
|
139 |
html_content = f"""
|
140 |
<html>
|
141 |
<body>
|
142 |
<h1>Patient Information</h1>
|
143 |
+
{''.join(patient_data)}
|
|
|
|
|
|
|
|
|
|
|
144 |
</body>
|
145 |
</html>
|
146 |
"""
|
147 |
|
148 |
+
# Save HTML content to file
|
149 |
+
html_file_path = os.path.join(tempfile.gettempdir(), 'patient_info.html')
|
150 |
+
with open(html_file_path, 'w') as f:
|
151 |
+
f.write(html_content)
|
|
|
|
|
|
|
152 |
|
153 |
+
return html_file_path
|
154 |
|
155 |
# Function to download the folders
|
156 |
def download_folder(folder):
|
|
|
162 |
return zip_path
|
163 |
|
164 |
# Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
with gr.Blocks() as demo:
|
166 |
with gr.Column():
|
167 |
gr.Markdown("# Cataract Detection System")
|
|
|
192 |
uploaded_folder_file = gr.File(label="Uploaded Images Zip File")
|
193 |
predicted_folder_file = gr.File(label="Predicted Images Zip File")
|
194 |
|
195 |
+
submit_btn.click(fn=predict_image, inputs=[name, age, medical_record, sex, input_image], outputs=[output_image, raw_result])
|
196 |
download_html_btn.click(fn=save_patient_info_to_html, inputs=[name, age, medical_record, sex, raw_result], outputs=patient_info_file)
|
197 |
+
download_uploaded_btn.click(fn=download_folder, inputs=[uploaded_folder], outputs=uploaded_folder_file)
|
198 |
+
download_predicted_btn.click(fn=download_folder, inputs=[predicted_folder], outputs=predicted_folder_file)
|
199 |
|
200 |
# Launch Gradio app
|
201 |
demo.launch()
|