Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -263,32 +263,70 @@ async def predict(image):
|
|
263 |
draw = ImageDraw.Draw(annotated_image)
|
264 |
font = ImageFont.load_default()
|
265 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
266 |
dogs_info = ""
|
267 |
-
buttons_html = ""
|
268 |
|
269 |
for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
|
|
270 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
271 |
color = color_list[i % len(color_list)]
|
272 |
draw.rectangle(box, outline=color, width=3)
|
273 |
draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
|
274 |
-
|
275 |
combined_confidence = detection_confidence * top1_prob
|
276 |
dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
|
277 |
dogs_info += f'<h2>Dog {i+1}</h2>'
|
278 |
-
|
279 |
if top1_prob >= 0.45:
|
280 |
breed = topk_breeds[0]
|
281 |
description = get_dog_description(breed)
|
282 |
dogs_info += format_description_html(description, breed)
|
283 |
-
|
284 |
elif combined_confidence >= 0.15:
|
285 |
dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
286 |
for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
287 |
-
#dogs_info += f"<li><strong>{breed}</strong> ({prob} confidence)</li>"
|
288 |
prob = float(prob.replace('%', '')) # new
|
289 |
dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>" # new
|
290 |
dogs_info += "</ul>"
|
291 |
-
|
292 |
for breed in topk_breeds[:3]:
|
293 |
button_id = f"Dog {i+1}: More about {breed}"
|
294 |
buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
|
@@ -298,8 +336,10 @@ async def predict(image):
|
|
298 |
dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
299 |
|
300 |
dogs_info += '</div>'
|
|
|
|
|
|
|
301 |
|
302 |
-
dogs_info += buttons_html
|
303 |
|
304 |
|
305 |
html_output = f"""
|
|
|
263 |
draw = ImageDraw.Draw(annotated_image)
|
264 |
font = ImageFont.load_default()
|
265 |
|
266 |
+
# dogs_info = ""
|
267 |
+
# buttons_html = ""
|
268 |
+
|
269 |
+
# for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
270 |
+
# top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
271 |
+
# color = color_list[i % len(color_list)]
|
272 |
+
# draw.rectangle(box, outline=color, width=3)
|
273 |
+
# draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
|
274 |
+
|
275 |
+
# combined_confidence = detection_confidence * top1_prob
|
276 |
+
# dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
|
277 |
+
# dogs_info += f'<h2>Dog {i+1}</h2>'
|
278 |
+
|
279 |
+
# if top1_prob >= 0.45:
|
280 |
+
# breed = topk_breeds[0]
|
281 |
+
# description = get_dog_description(breed)
|
282 |
+
# dogs_info += format_description_html(description, breed)
|
283 |
+
|
284 |
+
# elif combined_confidence >= 0.15:
|
285 |
+
# dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
286 |
+
# for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
287 |
+
# #dogs_info += f"<li><strong>{breed}</strong> ({prob} confidence)</li>"
|
288 |
+
# prob = float(prob.replace('%', '')) # new
|
289 |
+
# dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>" # new
|
290 |
+
# dogs_info += "</ul>"
|
291 |
+
|
292 |
+
# for breed in topk_breeds[:3]:
|
293 |
+
# button_id = f"Dog {i+1}: More about {breed}"
|
294 |
+
# buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
|
295 |
+
# buttons.append(button_id)
|
296 |
+
# buttons_html += '</div>'
|
297 |
+
# else:
|
298 |
+
# dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
299 |
+
|
300 |
+
# dogs_info += '</div>'
|
301 |
+
|
302 |
+
# dogs_info += buttons_html
|
303 |
+
|
304 |
+
|
305 |
dogs_info = ""
|
|
|
306 |
|
307 |
for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
308 |
+
buttons_html = "" # 初始化,避免按鈕重複附加
|
309 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
310 |
color = color_list[i % len(color_list)]
|
311 |
draw.rectangle(box, outline=color, width=3)
|
312 |
draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
|
313 |
+
|
314 |
combined_confidence = detection_confidence * top1_prob
|
315 |
dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
|
316 |
dogs_info += f'<h2>Dog {i+1}</h2>'
|
317 |
+
|
318 |
if top1_prob >= 0.45:
|
319 |
breed = topk_breeds[0]
|
320 |
description = get_dog_description(breed)
|
321 |
dogs_info += format_description_html(description, breed)
|
322 |
+
|
323 |
elif combined_confidence >= 0.15:
|
324 |
dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
325 |
for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
|
|
326 |
prob = float(prob.replace('%', '')) # new
|
327 |
dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>" # new
|
328 |
dogs_info += "</ul>"
|
329 |
+
|
330 |
for breed in topk_breeds[:3]:
|
331 |
button_id = f"Dog {i+1}: More about {breed}"
|
332 |
buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
|
|
|
336 |
dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
337 |
|
338 |
dogs_info += '</div>'
|
339 |
+
|
340 |
+
# 將 buttons_html 在每輪迴圈後附加到 dogs_info
|
341 |
+
dogs_info += buttons_html
|
342 |
|
|
|
343 |
|
344 |
|
345 |
html_output = f"""
|