Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -502,113 +502,6 @@ async def process_single_dog(image):
|
|
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
|
509 |
-
|
510 |
-
# try:
|
511 |
-
# if isinstance(image, np.ndarray):
|
512 |
-
# image = Image.fromarray(image)
|
513 |
-
|
514 |
-
# dogs = await detect_multiple_dogs(image)
|
515 |
-
|
516 |
-
# color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
517 |
-
# html_output = """
|
518 |
-
# <style>
|
519 |
-
# .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); }
|
520 |
-
# .dog-info h2 { background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }
|
521 |
-
# .breed-buttons { margin-top: 10px; }
|
522 |
-
# .breed-button { margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }
|
523 |
-
# </style>
|
524 |
-
# """
|
525 |
-
# buttons = []
|
526 |
-
# annotated_image = image.copy()
|
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)]
|
537 |
-
# draw.rectangle(box, outline=color, width=3)
|
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 += """
|
573 |
-
# <script>
|
574 |
-
# function handle_button_click(button_id) {
|
575 |
-
# const radio = document.querySelector('input[type=radio][value="' + button_id + '"]');
|
576 |
-
# if (radio) {
|
577 |
-
# radio.click();
|
578 |
-
# } else {
|
579 |
-
# console.error("Radio button not found:", button_id);
|
580 |
-
# }
|
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 |
-
|
607 |
-
# except Exception as e:
|
608 |
-
# error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
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
|
@@ -660,8 +553,17 @@ async def predict(image):
|
|
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); }}
|
@@ -670,8 +572,11 @@ async def predict(image):
|
|
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 += """
|
677 |
<script>
|
|
|
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
|
|
|
553 |
|
554 |
dogs_info += '</div>'
|
555 |
|
556 |
+
#dogs_info += buttons_html
|
557 |
+
|
558 |
+
# html_output = f"""
|
559 |
+
# <style>
|
560 |
+
# .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); }}
|
561 |
+
# .dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
|
562 |
+
# .breed-buttons {{ margin-top: 10px; }}
|
563 |
+
# .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
|
564 |
+
# </style>
|
565 |
+
# {dogs_info}
|
566 |
+
# """
|
567 |
html_output = f"""
|
568 |
<style>
|
569 |
.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); }}
|
|
|
572 |
.breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
|
573 |
</style>
|
574 |
{dogs_info}
|
575 |
+
{buttons_html}
|
576 |
"""
|
577 |
|
578 |
+
|
579 |
+
|
580 |
if buttons:
|
581 |
html_output += """
|
582 |
<script>
|