Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -263,131 +263,43 @@ async def predict(image):
|
|
263 |
draw = ImageDraw.Draw(annotated_image)
|
264 |
font = ImageFont.load_default()
|
265 |
|
266 |
-
|
267 |
-
|
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 |
-
# html_output = f"""
|
306 |
-
# <style>
|
307 |
-
# .dog-info {{ border: 1px solid #ddd; margin-bottom: 20px; padding: 15px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }}
|
308 |
-
# .dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
|
309 |
-
# .breed-buttons {{ margin-top: 10px; }}
|
310 |
-
# .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
|
311 |
-
# </style>
|
312 |
-
# {dogs_info}
|
313 |
-
|
314 |
-
# """
|
315 |
-
|
316 |
-
# if buttons:
|
317 |
-
# html_output += """
|
318 |
-
# <script>
|
319 |
-
# function handle_button_click(button_id) {
|
320 |
-
# const radio = document.querySelector('input[type=radio][value="' + button_id + '"]');
|
321 |
-
# if (radio) {
|
322 |
-
# radio.click();
|
323 |
-
# } else {
|
324 |
-
# console.error("Radio button not found:", button_id);
|
325 |
-
# }
|
326 |
-
# }
|
327 |
-
# </script>
|
328 |
-
# """
|
329 |
-
# initial_state = {
|
330 |
-
# "dogs_info": dogs_info,
|
331 |
-
# "buttons": buttons,
|
332 |
-
# "show_back": True,
|
333 |
-
# "image": annotated_image,
|
334 |
-
# "is_multi_dog": len(dogs) > 1,
|
335 |
-
# "html_output": html_output # 儲存完整的 HTML 輸出
|
336 |
-
# }
|
337 |
-
# return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
338 |
-
# else:
|
339 |
-
# initial_state = {
|
340 |
-
# "dogs_info": dogs_info,
|
341 |
-
# "buttons": [],
|
342 |
-
# "show_back": False,
|
343 |
-
# "image": annotated_image,
|
344 |
-
# "is_multi_dog": len(dogs) > 1,
|
345 |
-
# "html_output": html_output # 儲存完整的 HTML 輸出
|
346 |
-
# }
|
347 |
-
# return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
|
348 |
-
|
349 |
-
|
350 |
-
dogs_info = "" # 初始化 dogs_info
|
351 |
-
buttons_html = "" # 初始化 buttons_html
|
352 |
-
|
353 |
for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
354 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
355 |
color = color_list[i % len(color_list)]
|
356 |
draw.rectangle(box, outline=color, width=3)
|
357 |
draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
|
358 |
-
|
359 |
combined_confidence = detection_confidence * top1_prob
|
360 |
dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
|
361 |
dogs_info += f'<h2>Dog {i+1}</h2>'
|
362 |
-
|
363 |
if top1_prob >= 0.45:
|
364 |
-
# 當信心 >= 0.45 顯示單一品種
|
365 |
breed = topk_breeds[0]
|
366 |
description = get_dog_description(breed)
|
367 |
dogs_info += format_description_html(description, breed)
|
368 |
-
|
369 |
elif combined_confidence >= 0.15:
|
370 |
-
# 顯示 top3 品種
|
371 |
dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
372 |
for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
# 單獨生成按鈕邏輯
|
378 |
-
temp_buttons = '<div class="breed-buttons">' # 按鈕生成區塊
|
379 |
for breed in topk_breeds[:3]:
|
380 |
button_id = f"Dog {i+1}: More about {breed}"
|
381 |
-
|
382 |
buttons.append(button_id)
|
383 |
-
buttons_html +=
|
384 |
-
|
385 |
else:
|
386 |
dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
|
|
|
|
|
|
|
|
|
|
387 |
|
388 |
-
dogs_info += '</div>' # 結束該狗的描述區塊
|
389 |
-
|
390 |
-
# 按鈕不再每個迴圈加入,而是放到整個迴圈結束後統一加入
|
391 |
html_output = f"""
|
392 |
<style>
|
393 |
.dog-info {{ border: 1px solid #ddd; margin-bottom: 20px; padding: 15px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }}
|
@@ -396,12 +308,9 @@ async def predict(image):
|
|
396 |
.breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
|
397 |
</style>
|
398 |
{dogs_info}
|
399 |
-
"""
|
400 |
-
|
401 |
-
# 確保按鈕獨立顯示,只加一次
|
402 |
-
if buttons_html:
|
403 |
-
html_output += buttons_html
|
404 |
|
|
|
|
|
405 |
if buttons:
|
406 |
html_output += """
|
407 |
<script>
|
|
|
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 |
+
dogs_info += "</ul>"
|
289 |
+
|
|
|
|
|
|
|
290 |
for breed in topk_breeds[:3]:
|
291 |
button_id = f"Dog {i+1}: More about {breed}"
|
292 |
+
buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
|
293 |
buttons.append(button_id)
|
294 |
+
buttons_html += '</div>'
|
|
|
295 |
else:
|
296 |
dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
297 |
+
|
298 |
+
#dogs_info += '</div>'
|
299 |
+
|
300 |
+
dogs_info += buttons_html
|
301 |
+
|
302 |
|
|
|
|
|
|
|
303 |
html_output = f"""
|
304 |
<style>
|
305 |
.dog-info {{ border: 1px solid #ddd; margin-bottom: 20px; padding: 15px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }}
|
|
|
308 |
.breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
|
309 |
</style>
|
310 |
{dogs_info}
|
|
|
|
|
|
|
|
|
|
|
311 |
|
312 |
+
"""
|
313 |
+
|
314 |
if buttons:
|
315 |
html_output += """
|
316 |
<script>
|