Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -502,6 +502,102 @@ async def process_single_dog(image):
|
|
502 |
# return error_msg, None, gr.update(visible=False, choices=[]), None
|
503 |
|
504 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
505 |
async def predict(image):
|
506 |
if image is None:
|
507 |
return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
@@ -526,6 +622,10 @@ async def predict(image):
|
|
526 |
draw = ImageDraw.Draw(annotated_image)
|
527 |
font = ImageFont.load_default()
|
528 |
|
|
|
|
|
|
|
|
|
529 |
for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
530 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
531 |
color = color_list[i % len(color_list)]
|
@@ -533,30 +633,35 @@ async def predict(image):
|
|
533 |
draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
|
534 |
|
535 |
combined_confidence = detection_confidence * top1_prob
|
536 |
-
|
537 |
-
|
|
|
538 |
|
539 |
if top1_prob >= 0.45:
|
540 |
breed = topk_breeds[0]
|
541 |
description = get_dog_description(breed)
|
542 |
-
|
543 |
|
544 |
elif combined_confidence >= 0.15:
|
545 |
-
|
546 |
for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
547 |
-
|
548 |
-
|
549 |
-
|
|
|
550 |
for breed in topk_breeds[:3]:
|
551 |
button_id = f"Dog {i+1}: More about {breed}"
|
552 |
-
|
553 |
buttons.append(button_id)
|
554 |
-
|
555 |
|
556 |
else:
|
557 |
-
|
558 |
|
559 |
-
|
|
|
|
|
|
|
560 |
|
561 |
if buttons:
|
562 |
html_output += """
|
@@ -571,24 +676,26 @@ async def predict(image):
|
|
571 |
}
|
572 |
</script>
|
573 |
"""
|
|
|
574 |
initial_state = {
|
575 |
"explanation": html_output,
|
576 |
"buttons": buttons,
|
577 |
"show_back": True,
|
578 |
"image": annotated_image,
|
579 |
"is_multi_dog": len(dogs) > 1,
|
580 |
-
"dogs_info":
|
581 |
-
|
582 |
}
|
583 |
return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
584 |
else:
|
|
|
585 |
initial_state = {
|
586 |
"explanation": html_output,
|
587 |
"buttons": [],
|
588 |
"show_back": False,
|
589 |
"image": annotated_image,
|
590 |
"is_multi_dog": len(dogs) > 1,
|
591 |
-
"dogs_info":
|
592 |
}
|
593 |
return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
|
594 |
|
@@ -616,7 +723,7 @@ def show_details_html(choice, previous_output, initial_state):
|
|
616 |
"""
|
617 |
|
618 |
initial_state["current_description"] = html_output
|
619 |
-
|
620 |
|
621 |
return html_output, gr.update(visible=True), initial_state
|
622 |
except Exception as e:
|
|
|
502 |
# return error_msg, None, gr.update(visible=False, choices=[]), None
|
503 |
|
504 |
|
505 |
+
# async def predict(image):
|
506 |
+
# if image is None:
|
507 |
+
# return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
508 |
+
|
509 |
+
# try:
|
510 |
+
# if isinstance(image, np.ndarray):
|
511 |
+
# image = Image.fromarray(image)
|
512 |
+
|
513 |
+
# dogs = await detect_multiple_dogs(image)
|
514 |
+
|
515 |
+
# color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
516 |
+
# html_output = """
|
517 |
+
# <style>
|
518 |
+
# .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); }
|
519 |
+
# .dog-info h2 { background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }
|
520 |
+
# .breed-buttons { margin-top: 10px; }
|
521 |
+
# .breed-button { margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }
|
522 |
+
# </style>
|
523 |
+
# """
|
524 |
+
# buttons = []
|
525 |
+
# annotated_image = image.copy()
|
526 |
+
# draw = ImageDraw.Draw(annotated_image)
|
527 |
+
# font = ImageFont.load_default()
|
528 |
+
|
529 |
+
# for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
530 |
+
# top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
531 |
+
# color = color_list[i % len(color_list)]
|
532 |
+
# draw.rectangle(box, outline=color, width=3)
|
533 |
+
# draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
|
534 |
+
|
535 |
+
# combined_confidence = detection_confidence * top1_prob
|
536 |
+
# html_output += f'<div class="dog-info" style="border-left: 5px solid {color};">'
|
537 |
+
# html_output += f'<h2>Dog {i+1}</h2>'
|
538 |
+
|
539 |
+
# if top1_prob >= 0.45:
|
540 |
+
# breed = topk_breeds[0]
|
541 |
+
# description = get_dog_description(breed)
|
542 |
+
# html_output += format_description_html(description, breed)
|
543 |
+
|
544 |
+
# elif combined_confidence >= 0.15:
|
545 |
+
# html_output += f"<p>Top 3 possible breeds:</p><ul>"
|
546 |
+
# for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
547 |
+
# html_output += f"<li><strong>{breed}</strong> ({prob} confidence)</li>"
|
548 |
+
# html_output += "</ul>"
|
549 |
+
# html_output += '<div class="breed-buttons">'
|
550 |
+
# for breed in topk_breeds[:3]:
|
551 |
+
# button_id = f"Dog {i+1}: More about {breed}"
|
552 |
+
# html_output += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
|
553 |
+
# buttons.append(button_id)
|
554 |
+
# html_output += '</div>'
|
555 |
+
|
556 |
+
# else:
|
557 |
+
# html_output += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
558 |
+
|
559 |
+
# html_output += '</div>'
|
560 |
+
|
561 |
+
# if buttons:
|
562 |
+
# html_output += """
|
563 |
+
# <script>
|
564 |
+
# function handle_button_click(button_id) {
|
565 |
+
# const radio = document.querySelector('input[type=radio][value="' + button_id + '"]');
|
566 |
+
# if (radio) {
|
567 |
+
# radio.click();
|
568 |
+
# } else {
|
569 |
+
# console.error("Radio button not found:", button_id);
|
570 |
+
# }
|
571 |
+
# }
|
572 |
+
# </script>
|
573 |
+
# """
|
574 |
+
# initial_state = {
|
575 |
+
# "explanation": html_output,
|
576 |
+
# "buttons": buttons,
|
577 |
+
# "show_back": True,
|
578 |
+
# "image": annotated_image,
|
579 |
+
# "is_multi_dog": len(dogs) > 1,
|
580 |
+
# "dogs_info": html_output,
|
581 |
+
# "original_html": html_output # 新增這行
|
582 |
+
# }
|
583 |
+
# return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
584 |
+
# else:
|
585 |
+
# initial_state = {
|
586 |
+
# "explanation": html_output,
|
587 |
+
# "buttons": [],
|
588 |
+
# "show_back": False,
|
589 |
+
# "image": annotated_image,
|
590 |
+
# "is_multi_dog": len(dogs) > 1,
|
591 |
+
# "dogs_info": html_output
|
592 |
+
# }
|
593 |
+
# return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
|
594 |
+
|
595 |
+
# except Exception as e:
|
596 |
+
# error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
597 |
+
# print(error_msg)
|
598 |
+
# return error_msg, None, gr.update(visible=False, choices=[]), None
|
599 |
+
|
600 |
+
|
601 |
async def predict(image):
|
602 |
if image is None:
|
603 |
return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
|
|
622 |
draw = ImageDraw.Draw(annotated_image)
|
623 |
font = ImageFont.load_default()
|
624 |
|
625 |
+
# 修改: 新增變數來分開存儲狗狗��息和按鈕信息
|
626 |
+
dogs_info = ""
|
627 |
+
buttons_html = ""
|
628 |
+
|
629 |
for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
630 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
631 |
color = color_list[i % len(color_list)]
|
|
|
633 |
draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
|
634 |
|
635 |
combined_confidence = detection_confidence * top1_prob
|
636 |
+
# 修改: 使用 dogs_info 而不是 html_output
|
637 |
+
dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color};">'
|
638 |
+
dogs_info += f'<h2>Dog {i+1}</h2>'
|
639 |
|
640 |
if top1_prob >= 0.45:
|
641 |
breed = topk_breeds[0]
|
642 |
description = get_dog_description(breed)
|
643 |
+
dogs_info += format_description_html(description, breed)
|
644 |
|
645 |
elif combined_confidence >= 0.15:
|
646 |
+
dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
647 |
for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
648 |
+
dogs_info += f"<li><strong>{breed}</strong> ({prob} confidence)</li>"
|
649 |
+
dogs_info += "</ul>"
|
650 |
+
# 修改: 將按鈕信息添加到 buttons_html
|
651 |
+
buttons_html += '<div class="breed-buttons">'
|
652 |
for breed in topk_breeds[:3]:
|
653 |
button_id = f"Dog {i+1}: More about {breed}"
|
654 |
+
buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
|
655 |
buttons.append(button_id)
|
656 |
+
buttons_html += '</div>'
|
657 |
|
658 |
else:
|
659 |
+
dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
660 |
|
661 |
+
dogs_info += '</div>'
|
662 |
+
|
663 |
+
# 修改: 合併 dogs_info 和 buttons_html
|
664 |
+
html_output += dogs_info + buttons_html
|
665 |
|
666 |
if buttons:
|
667 |
html_output += """
|
|
|
676 |
}
|
677 |
</script>
|
678 |
"""
|
679 |
+
# 修改: 更新 initial_state 的內容
|
680 |
initial_state = {
|
681 |
"explanation": html_output,
|
682 |
"buttons": buttons,
|
683 |
"show_back": True,
|
684 |
"image": annotated_image,
|
685 |
"is_multi_dog": len(dogs) > 1,
|
686 |
+
"dogs_info": dogs_info, # 修改: 只存儲狗狗信息,不包括按鈕
|
687 |
+
"original_html": html_output # 保留完整的 HTML 輸出
|
688 |
}
|
689 |
return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
690 |
else:
|
691 |
+
# 修改: 更新 initial_state 的內容
|
692 |
initial_state = {
|
693 |
"explanation": html_output,
|
694 |
"buttons": [],
|
695 |
"show_back": False,
|
696 |
"image": annotated_image,
|
697 |
"is_multi_dog": len(dogs) > 1,
|
698 |
+
"dogs_info": dogs_info # 修改: 只存儲狗狗信息,不包括按鈕
|
699 |
}
|
700 |
return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
|
701 |
|
|
|
723 |
"""
|
724 |
|
725 |
initial_state["current_description"] = html_output
|
726 |
+
initial_state["original_buttons"] = initial_state.get("buttons", [])
|
727 |
|
728 |
return html_output, gr.update(visible=True), initial_state
|
729 |
except Exception as e:
|