Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -266,40 +266,86 @@ async def predict(image):
|
|
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 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
|
|
292 |
for breed in topk_breeds[:3]:
|
293 |
button_id = f"Dog {i+1}: More about {breed}"
|
294 |
-
|
295 |
buttons.append(button_id)
|
296 |
-
|
|
|
|
|
|
|
|
|
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 同步顯示
|
|
|
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 |
+
# buttons_html = '<div class="breed-buttons">' #new
|
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 |
for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
306 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
307 |
color = color_list[i % len(color_list)]
|
308 |
draw.rectangle(box, outline=color, width=3)
|
309 |
draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
|
310 |
+
|
311 |
combined_confidence = detection_confidence * top1_prob
|
312 |
dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
|
313 |
dogs_info += f'<h2>Dog {i+1}</h2>'
|
314 |
+
|
315 |
if top1_prob >= 0.45:
|
316 |
+
# 當信心 >= 0.45 顯示單一品種
|
317 |
breed = topk_breeds[0]
|
318 |
description = get_dog_description(breed)
|
319 |
dogs_info += format_description_html(description, breed)
|
320 |
+
|
321 |
elif combined_confidence >= 0.15:
|
322 |
+
# 顯示 top3 品種
|
323 |
dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
324 |
for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
325 |
+
prob = float(prob.replace('%', '')) # 去掉百分比符號並轉為浮點數
|
326 |
+
dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
|
327 |
+
dogs_info += "</ul>"
|
328 |
+
|
329 |
+
# 單獨生成按鈕邏輯
|
330 |
+
temp_buttons = '<div class="breed-buttons">' # 按鈕生成區塊
|
331 |
for breed in topk_breeds[:3]:
|
332 |
button_id = f"Dog {i+1}: More about {breed}"
|
333 |
+
temp_buttons += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
|
334 |
buttons.append(button_id)
|
335 |
+
temp_buttons += '</div>'
|
336 |
+
|
337 |
+
# 將 buttons_html 只生成按鈕
|
338 |
+
buttons_html += temp_buttons
|
339 |
+
|
340 |
else:
|
341 |
dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
342 |
+
|
343 |
+
dogs_info += '</div>' # 結束該狗的描述區塊
|
344 |
+
|
345 |
+
# 確保按鈕獨立於 dogs_info 區塊內,避免重複品種名
|
346 |
+
if buttons_html:
|
347 |
+
dogs_info += buttons_html # 確��按鈕獨立並附加到最後輸出
|
348 |
|
|
|
349 |
|
350 |
|
351 |
# 避免將按鈕與 dogs_info 同步顯示
|