Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -502,111 +502,111 @@ 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 |
-
|
507 |
-
|
508 |
|
509 |
-
|
510 |
-
|
511 |
-
|
512 |
|
513 |
-
|
514 |
|
515 |
-
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
520 |
|
521 |
-
|
522 |
-
|
523 |
-
|
524 |
-
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
# dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
|
533 |
-
# dogs_info += f'<h2>Dog {i+1}</h2>'
|
534 |
-
|
535 |
-
# if top1_prob >= 0.45:
|
536 |
-
# breed = topk_breeds[0]
|
537 |
-
# description = get_dog_description(breed)
|
538 |
-
# dogs_info += format_description_html(description, breed)
|
539 |
-
|
540 |
-
# elif combined_confidence >= 0.15:
|
541 |
-
# dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
542 |
-
# for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
543 |
-
# dogs_info += f"<li><strong>{breed}</strong> ({prob} confidence)</li>"
|
544 |
-
# dogs_info += "</ul>"
|
545 |
-
# buttons_html += '<div class="breed-buttons">'
|
546 |
-
# for breed in topk_breeds[:3]:
|
547 |
-
# button_id = f"Dog {i+1}: More about {breed}"
|
548 |
-
# buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
|
549 |
-
# buttons.append(button_id)
|
550 |
-
# buttons_html += '</div>'
|
551 |
|
552 |
|
553 |
|
554 |
-
|
555 |
-
|
556 |
|
557 |
-
|
558 |
|
559 |
-
|
560 |
|
561 |
|
562 |
-
|
563 |
-
|
564 |
-
|
565 |
-
|
566 |
-
|
567 |
-
|
568 |
-
|
569 |
-
|
570 |
-
|
571 |
-
|
572 |
-
|
573 |
-
|
574 |
-
|
575 |
-
|
576 |
-
|
577 |
-
|
578 |
-
|
579 |
-
|
580 |
-
|
581 |
-
|
582 |
-
|
583 |
-
|
584 |
-
|
585 |
-
|
586 |
-
|
587 |
-
|
588 |
-
|
589 |
-
|
590 |
-
|
591 |
-
|
592 |
-
|
593 |
-
|
594 |
-
|
595 |
-
|
596 |
-
|
597 |
-
|
598 |
-
|
599 |
-
|
600 |
-
|
601 |
-
|
602 |
-
|
603 |
-
|
604 |
-
|
605 |
-
|
606 |
-
|
607 |
-
|
608 |
-
|
609 |
-
|
610 |
|
611 |
|
612 |
async def predict(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
|
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 |
+
# buttons = []
|
517 |
+
# annotated_image = image.copy()
|
518 |
+
# draw = ImageDraw.Draw(annotated_image)
|
519 |
+
# font = ImageFont.load_default()
|
520 |
+
|
521 |
+
# dogs_info = ""
|
522 |
+
# buttons_html = ""
|
523 |
+
|
524 |
+
# for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
525 |
+
# top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
526 |
+
# color = color_list[i % len(color_list)]
|
527 |
+
# draw.rectangle(box, outline=color, width=3)
|
528 |
+
# draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
|
529 |
+
|
530 |
+
# combined_confidence = detection_confidence * top1_prob
|
531 |
+
# #dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color};">'
|
532 |
+
# dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
|
533 |
+
# dogs_info += f'<h2>Dog {i+1}</h2>'
|
534 |
+
|
535 |
+
# if top1_prob >= 0.45:
|
536 |
+
# breed = topk_breeds[0]
|
537 |
+
# description = get_dog_description(breed)
|
538 |
+
# dogs_info += format_description_html(description, breed)
|
539 |
|
540 |
+
# elif combined_confidence >= 0.15:
|
541 |
+
# dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
542 |
+
# for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
543 |
+
# dogs_info += f"<li><strong>{breed}</strong> ({prob} confidence)</li>"
|
544 |
+
# dogs_info += "</ul>"
|
545 |
+
# buttons_html += '<div class="breed-buttons">'
|
546 |
+
# for breed in topk_breeds[:3]:
|
547 |
+
# button_id = f"Dog {i+1}: More about {breed}"
|
548 |
+
# buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
|
549 |
+
# buttons.append(button_id)
|
550 |
+
# buttons_html += '</div>'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
551 |
|
552 |
|
553 |
|
554 |
+
# else:
|
555 |
+
# dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
556 |
|
557 |
+
# dogs_info += '</div>'
|
558 |
|
559 |
+
# dogs_info += buttons_html
|
560 |
|
561 |
|
562 |
+
# html_output = f"""
|
563 |
+
# <style>
|
564 |
+
# .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); }}
|
565 |
+
# .dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
|
566 |
+
# .breed-buttons {{ margin-top: 10px; }}
|
567 |
+
# .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
|
568 |
+
# </style>
|
569 |
+
# {dogs_info}
|
570 |
+
# {buttons_html}
|
571 |
+
# """
|
572 |
+
|
573 |
+
# if buttons:
|
574 |
+
# html_output += """
|
575 |
+
# <script>
|
576 |
+
# function handle_button_click(button_id) {
|
577 |
+
# const radio = document.querySelector('input[type=radio][value="' + button_id + '"]');
|
578 |
+
# if (radio) {
|
579 |
+
# radio.click();
|
580 |
+
# } else {
|
581 |
+
# console.error("Radio button not found:", button_id);
|
582 |
+
# }
|
583 |
+
# }
|
584 |
+
# </script>
|
585 |
+
# """
|
586 |
+
# initial_state = {
|
587 |
+
# "dogs_info": dogs_info,
|
588 |
+
# "buttons": buttons,
|
589 |
+
# "show_back": True,
|
590 |
+
# "image": annotated_image,
|
591 |
+
# "is_multi_dog": len(dogs) > 1,
|
592 |
+
# "html_output": html_output # 儲存完整的 HTML 輸出
|
593 |
+
# }
|
594 |
+
# return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
595 |
+
# else:
|
596 |
+
# initial_state = {
|
597 |
+
# "dogs_info": dogs_info,
|
598 |
+
# "buttons": [],
|
599 |
+
# "show_back": False,
|
600 |
+
# "image": annotated_image,
|
601 |
+
# "is_multi_dog": len(dogs) > 1,
|
602 |
+
# "html_output": html_output # 儲存完整的 HTML 輸出
|
603 |
+
# }
|
604 |
+
# return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
|
605 |
+
|
606 |
+
# except Exception as e:
|
607 |
+
# error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
608 |
+
# print(error_msg)
|
609 |
+
# return error_msg, None, gr.update(visible=False, choices=[]), None
|
610 |
|
611 |
|
612 |
async def predict(image):
|