Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -502,6 +502,7 @@ 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 +527,10 @@ async def process_single_dog(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 +538,35 @@ async def process_single_dog(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 +581,26 @@ async def process_single_dog(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 |
-
# "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":
|
592 |
# }
|
593 |
# return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
|
594 |
|
@@ -597,7 +609,6 @@ async def process_single_dog(image):
|
|
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
|
@@ -609,20 +620,11 @@ async def predict(image):
|
|
609 |
dogs = await detect_multiple_dogs(image)
|
610 |
|
611 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
612 |
-
html_output = """
|
613 |
-
<style>
|
614 |
-
.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); }
|
615 |
-
.dog-info h2 { background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }
|
616 |
-
.breed-buttons { margin-top: 10px; }
|
617 |
-
.breed-button { margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }
|
618 |
-
</style>
|
619 |
-
"""
|
620 |
buttons = []
|
621 |
annotated_image = image.copy()
|
622 |
draw = ImageDraw.Draw(annotated_image)
|
623 |
font = ImageFont.load_default()
|
624 |
|
625 |
-
# 修改: 新增變數來分開存儲狗狗信息和按鈕信息
|
626 |
dogs_info = ""
|
627 |
buttons_html = ""
|
628 |
|
@@ -633,7 +635,6 @@ async def predict(image):
|
|
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 |
|
@@ -647,7 +648,6 @@ async def predict(image):
|
|
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}"
|
@@ -660,8 +660,17 @@ async def predict(image):
|
|
660 |
|
661 |
dogs_info += '</div>'
|
662 |
|
663 |
-
|
664 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
665 |
|
666 |
if buttons:
|
667 |
html_output += """
|
@@ -676,26 +685,23 @@ async def predict(image):
|
|
676 |
}
|
677 |
</script>
|
678 |
"""
|
679 |
-
# 修改: 更新 initial_state 的內容
|
680 |
initial_state = {
|
681 |
-
"
|
682 |
"buttons": buttons,
|
683 |
"show_back": True,
|
684 |
"image": annotated_image,
|
685 |
"is_multi_dog": len(dogs) > 1,
|
686 |
-
"
|
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 |
-
"
|
694 |
"buttons": [],
|
695 |
"show_back": False,
|
696 |
"image": annotated_image,
|
697 |
"is_multi_dog": len(dogs) > 1,
|
698 |
-
"
|
699 |
}
|
700 |
return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
|
701 |
|
@@ -750,8 +756,8 @@ def format_description_html(description, breed):
|
|
750 |
def go_back(state):
|
751 |
buttons = state.get("buttons", [])
|
752 |
return (
|
753 |
-
state["
|
754 |
-
#state["
|
755 |
state["image"],
|
756 |
gr.update(visible=True, choices=buttons),
|
757 |
gr.update(visible=False),
|
|
|
502 |
# return error_msg, None, gr.update(visible=False, choices=[]), None
|
503 |
|
504 |
|
505 |
+
|
506 |
# async def predict(image):
|
507 |
# if image is None:
|
508 |
# return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
|
|
527 |
# draw = ImageDraw.Draw(annotated_image)
|
528 |
# font = ImageFont.load_default()
|
529 |
|
530 |
+
# # 修改: 新增變數來分開存儲狗狗信息和按鈕信息
|
531 |
+
# dogs_info = ""
|
532 |
+
# buttons_html = ""
|
533 |
+
|
534 |
# for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
535 |
# top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
536 |
# color = color_list[i % len(color_list)]
|
|
|
538 |
# draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
|
539 |
|
540 |
# combined_confidence = detection_confidence * top1_prob
|
541 |
+
# # 修改: 使用 dogs_info 而不是 html_output
|
542 |
+
# dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color};">'
|
543 |
+
# dogs_info += f'<h2>Dog {i+1}</h2>'
|
544 |
|
545 |
# if top1_prob >= 0.45:
|
546 |
# breed = topk_breeds[0]
|
547 |
# description = get_dog_description(breed)
|
548 |
+
# dogs_info += format_description_html(description, breed)
|
549 |
|
550 |
# elif combined_confidence >= 0.15:
|
551 |
+
# dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
552 |
# for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
553 |
+
# dogs_info += f"<li><strong>{breed}</strong> ({prob} confidence)</li>"
|
554 |
+
# dogs_info += "</ul>"
|
555 |
+
# # 修改: 將按鈕信息添加到 buttons_html
|
556 |
+
# buttons_html += '<div class="breed-buttons">'
|
557 |
# for breed in topk_breeds[:3]:
|
558 |
# button_id = f"Dog {i+1}: More about {breed}"
|
559 |
+
# buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
|
560 |
# buttons.append(button_id)
|
561 |
+
# buttons_html += '</div>'
|
562 |
|
563 |
# else:
|
564 |
+
# dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
565 |
|
566 |
+
# dogs_info += '</div>'
|
567 |
+
|
568 |
+
# # 修改: 合併 dogs_info 和 buttons_html
|
569 |
+
# html_output += dogs_info + buttons_html
|
570 |
|
571 |
# if buttons:
|
572 |
# html_output += """
|
|
|
581 |
# }
|
582 |
# </script>
|
583 |
# """
|
584 |
+
# # 修改: 更新 initial_state 的內容
|
585 |
# initial_state = {
|
586 |
# "explanation": html_output,
|
587 |
# "buttons": buttons,
|
588 |
# "show_back": True,
|
589 |
# "image": annotated_image,
|
590 |
# "is_multi_dog": len(dogs) > 1,
|
591 |
+
# "dogs_info": dogs_info, # 修改: 只存儲狗狗信息,不包括按鈕
|
592 |
+
# "original_html": html_output # 保留完整的 HTML 輸出
|
593 |
# }
|
594 |
# return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
595 |
# else:
|
596 |
+
# # 修改: 更新 initial_state 的內容
|
597 |
# initial_state = {
|
598 |
# "explanation": html_output,
|
599 |
# "buttons": [],
|
600 |
# "show_back": False,
|
601 |
# "image": annotated_image,
|
602 |
# "is_multi_dog": len(dogs) > 1,
|
603 |
+
# "dogs_info": dogs_info # 修改: 只存儲狗狗信息,不包括按鈕
|
604 |
# }
|
605 |
# return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
|
606 |
|
|
|
609 |
# print(error_msg)
|
610 |
# return error_msg, None, gr.update(visible=False, choices=[]), None
|
611 |
|
|
|
612 |
async def predict(image):
|
613 |
if image is None:
|
614 |
return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
|
|
620 |
dogs = await detect_multiple_dogs(image)
|
621 |
|
622 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
623 |
buttons = []
|
624 |
annotated_image = image.copy()
|
625 |
draw = ImageDraw.Draw(annotated_image)
|
626 |
font = ImageFont.load_default()
|
627 |
|
|
|
628 |
dogs_info = ""
|
629 |
buttons_html = ""
|
630 |
|
|
|
635 |
draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
|
636 |
|
637 |
combined_confidence = detection_confidence * top1_prob
|
|
|
638 |
dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color};">'
|
639 |
dogs_info += f'<h2>Dog {i+1}</h2>'
|
640 |
|
|
|
648 |
for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
649 |
dogs_info += f"<li><strong>{breed}</strong> ({prob} confidence)</li>"
|
650 |
dogs_info += "</ul>"
|
|
|
651 |
buttons_html += '<div class="breed-buttons">'
|
652 |
for breed in topk_breeds[:3]:
|
653 |
button_id = f"Dog {i+1}: More about {breed}"
|
|
|
660 |
|
661 |
dogs_info += '</div>'
|
662 |
|
663 |
+
dogs_info += buttons_html
|
664 |
+
|
665 |
+
html_output = f"""
|
666 |
+
<style>
|
667 |
+
.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); }}
|
668 |
+
.dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
|
669 |
+
.breed-buttons {{ margin-top: 10px; }}
|
670 |
+
.breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
|
671 |
+
</style>
|
672 |
+
{dogs_info}
|
673 |
+
"""
|
674 |
|
675 |
if buttons:
|
676 |
html_output += """
|
|
|
685 |
}
|
686 |
</script>
|
687 |
"""
|
|
|
688 |
initial_state = {
|
689 |
+
"dogs_info": dogs_info,
|
690 |
"buttons": buttons,
|
691 |
"show_back": True,
|
692 |
"image": annotated_image,
|
693 |
"is_multi_dog": len(dogs) > 1,
|
694 |
+
"html_output": html_output # 儲存完整的 HTML 輸出
|
|
|
695 |
}
|
696 |
return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
697 |
else:
|
|
|
698 |
initial_state = {
|
699 |
+
"dogs_info": dogs_info,
|
700 |
"buttons": [],
|
701 |
"show_back": False,
|
702 |
"image": annotated_image,
|
703 |
"is_multi_dog": len(dogs) > 1,
|
704 |
+
"html_output": html_output # 儲存完整的 HTML 輸出
|
705 |
}
|
706 |
return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
|
707 |
|
|
|
756 |
def go_back(state):
|
757 |
buttons = state.get("buttons", [])
|
758 |
return (
|
759 |
+
state["html_output"],
|
760 |
+
#state["dogs_info"], # 這裡應該是完整的 HTML 內容
|
761 |
state["image"],
|
762 |
gr.update(visible=True, choices=buttons),
|
763 |
gr.update(visible=False),
|