Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -261,8 +261,43 @@ async def predict(image):
|
|
261 |
|
262 |
dogs_info = ""
|
263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
265 |
-
buttons_html = ""
|
266 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
267 |
color = color_list[i % len(color_list)]
|
268 |
draw.rectangle(box, outline=color, width=3)
|
@@ -276,7 +311,6 @@ async def predict(image):
|
|
276 |
breed = topk_breeds[0]
|
277 |
description = get_dog_description(breed)
|
278 |
dogs_info += format_description_html(description, breed)
|
279 |
-
|
280 |
elif combined_confidence >= 0.15:
|
281 |
dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
282 |
for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
@@ -284,17 +318,22 @@ async def predict(image):
|
|
284 |
dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
|
285 |
dogs_info += "</ul>"
|
286 |
|
|
|
|
|
287 |
for breed in topk_breeds[:3]:
|
288 |
button_id = f"Dog {i+1}: More about {breed}"
|
289 |
buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
|
290 |
buttons.append(button_id)
|
291 |
-
|
292 |
-
|
|
|
293 |
|
294 |
else:
|
295 |
dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
296 |
|
297 |
-
dogs_info += '</div>'
|
|
|
|
|
298 |
|
299 |
buttons_html = ""
|
300 |
|
|
|
261 |
|
262 |
dogs_info = ""
|
263 |
|
264 |
+
# for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
265 |
+
# buttons_html = ""
|
266 |
+
# top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
267 |
+
# color = color_list[i % len(color_list)]
|
268 |
+
# draw.rectangle(box, outline=color, width=3)
|
269 |
+
# draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
|
270 |
+
|
271 |
+
# combined_confidence = detection_confidence * top1_prob
|
272 |
+
# dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
|
273 |
+
# dogs_info += f'<h2>Dog {i+1}</h2>'
|
274 |
+
|
275 |
+
# if top1_prob >= 0.45:
|
276 |
+
# breed = topk_breeds[0]
|
277 |
+
# description = get_dog_description(breed)
|
278 |
+
# dogs_info += format_description_html(description, breed)
|
279 |
+
|
280 |
+
# elif combined_confidence >= 0.15:
|
281 |
+
# dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
282 |
+
# for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
283 |
+
# prob = float(prob.replace('%', ''))
|
284 |
+
# dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
|
285 |
+
# dogs_info += "</ul>"
|
286 |
+
|
287 |
+
# for breed in topk_breeds[:3]:
|
288 |
+
# button_id = f"Dog {i+1}: More about {breed}"
|
289 |
+
# buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
|
290 |
+
# buttons.append(button_id)
|
291 |
+
|
292 |
+
# else:
|
293 |
+
# dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
294 |
+
|
295 |
+
# dogs_info += '</div>'
|
296 |
+
|
297 |
+
|
298 |
+
|
299 |
for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
300 |
+
buttons_html = "" # 每次迴圈重置按鈕 HTML
|
301 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
302 |
color = color_list[i % len(color_list)]
|
303 |
draw.rectangle(box, outline=color, width=3)
|
|
|
311 |
breed = topk_breeds[0]
|
312 |
description = get_dog_description(breed)
|
313 |
dogs_info += format_description_html(description, breed)
|
|
|
314 |
elif combined_confidence >= 0.15:
|
315 |
dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
316 |
for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
|
|
318 |
dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
|
319 |
dogs_info += "</ul>"
|
320 |
|
321 |
+
# 生成與品種對應的按鈕
|
322 |
+
buttons_html = "" # 確保每次生成按鈕前清空
|
323 |
for breed in topk_breeds[:3]:
|
324 |
button_id = f"Dog {i+1}: More about {breed}"
|
325 |
buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
|
326 |
buttons.append(button_id)
|
327 |
+
|
328 |
+
# 確保按鈕插入在品種列表內部,而不是最後添加
|
329 |
+
dogs_info += f'<div class="breed-buttons">{buttons_html}</div>'
|
330 |
|
331 |
else:
|
332 |
dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
333 |
|
334 |
+
dogs_info += '</div>' # 結束當前狗的資訊區塊
|
335 |
+
|
336 |
+
|
337 |
|
338 |
buttons_html = ""
|
339 |
|