DawnC commited on
Commit
1f29365
1 Parent(s): 54bef46

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -126
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
- # 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):
613
  if image is None:
614
  return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
@@ -617,17 +510,16 @@ async def predict(image):
617
  if isinstance(image, np.ndarray):
618
  image = Image.fromarray(image)
619
 
620
- # 創建一個新的圖像副本用於註釋
 
 
 
621
  annotated_image = image.copy()
622
  draw = ImageDraw.Draw(annotated_image)
623
  font = ImageFont.load_default()
624
 
625
- dogs = await detect_multiple_dogs(image)
626
-
627
- color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
628
  dogs_info = ""
629
- all_breeds = set() # 使用集合來避免重複
630
- buttons = [] # 初始化 buttons 列表
631
 
632
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
633
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
@@ -636,33 +528,37 @@ async def predict(image):
636
  draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
637
 
638
  combined_confidence = detection_confidence * top1_prob
639
- dogs_info += f'''
640
- <div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px; border: 1px solid #ddd; border-radius: 5px;">
641
- <h2 style="background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0;">Dog {i+1}</h2>
642
- '''
643
 
644
  if top1_prob >= 0.45:
645
  breed = topk_breeds[0]
646
- all_breeds.add(breed)
647
  description = get_dog_description(breed)
648
  dogs_info += format_description_html(description, breed)
 
649
  elif combined_confidence >= 0.15:
650
- dogs_info += "<p>Top 3 possible breeds:</p><ul>"
651
  for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
652
  dogs_info += f"<li><strong>{breed}</strong> ({prob} confidence)</li>"
653
- all_breeds.add(breed)
654
  dogs_info += "</ul>"
655
- dogs_info += '<div class="breed-buttons">'
656
  for breed in topk_breeds[:3]:
657
  button_id = f"Dog {i+1}: More about {breed}"
658
- dogs_info += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
659
  buttons.append(button_id)
660
- dogs_info += '</div>'
 
 
 
661
  else:
662
  dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
663
 
664
  dogs_info += '</div>'
665
 
 
 
 
666
  html_output = f"""
667
  <style>
668
  .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); }}
@@ -671,6 +567,7 @@ async def predict(image):
671
  .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
672
  </style>
673
  {dogs_info}
 
674
  """
675
 
676
  if buttons:
@@ -692,7 +589,7 @@ async def predict(image):
692
  "show_back": True,
693
  "image": annotated_image,
694
  "is_multi_dog": len(dogs) > 1,
695
- "html_output": html_output
696
  }
697
  return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
698
  else:
@@ -702,7 +599,7 @@ async def predict(image):
702
  "show_back": False,
703
  "image": annotated_image,
704
  "is_multi_dog": len(dogs) > 1,
705
- "html_output": html_output
706
  }
707
  return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
708
 
@@ -710,7 +607,6 @@ async def predict(image):
710
  error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
711
  print(error_msg)
712
  return error_msg, None, gr.update(visible=False, choices=[]), None
713
-
714
 
715
 
716
  def show_details_html(choice, previous_output, initial_state):
 
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
 
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)
 
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); }}
 
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:
 
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:
 
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
 
 
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
  def show_details_html(choice, previous_output, initial_state):