DawnC commited on
Commit
6e5026c
·
verified ·
1 Parent(s): 5ca0cd1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -45
app.py CHANGED
@@ -518,69 +518,132 @@ async def predict(image):
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
  dogs_info = ""
562
- all_breeds = [] # 用於收集所有的品種
563
-
 
 
 
564
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
565
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
566
  color = color_list[i % len(color_list)]
567
  draw.rectangle(box, outline=color, width=3)
568
  draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
569
-
570
  combined_confidence = detection_confidence * top1_prob
571
  dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px; border: 1px solid #ddd; border-radius: 5px;">'
572
  dogs_info += f'<h2 style="background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0;">Dog {i+1}</h2>'
573
-
574
  if top1_prob >= 0.45:
575
  breed = topk_breeds[0]
576
- all_breeds.append(breed) # 添加到所有品種列表
577
  description = get_dog_description(breed)
578
  dogs_info += format_description_html(description, breed)
579
  elif combined_confidence >= 0.15:
580
  dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
581
  for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
582
  dogs_info += f"<li><strong>{breed}</strong> ({prob} confidence)</li>"
583
- all_breeds.append(breed) # 添加到所有品種列表
584
  dogs_info += "</ul>"
585
  dogs_info += '<div class="breed-buttons">'
586
  for breed in topk_breeds[:3]:
@@ -592,9 +655,7 @@ async def predict(image):
592
  dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
593
 
594
  dogs_info += '</div>'
595
-
596
 
597
-
598
  html_output = f"""
599
  <style>
600
  .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); }}
@@ -603,7 +664,6 @@ async def predict(image):
603
  .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
604
  </style>
605
  {dogs_info}
606
- {buttons_html}
607
  """
608
 
609
  if buttons:
@@ -625,7 +685,7 @@ async def predict(image):
625
  "show_back": True,
626
  "image": annotated_image,
627
  "is_multi_dog": len(dogs) > 1,
628
- "html_output": html_output # 儲存完整的 HTML 輸出
629
  }
630
  return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
631
  else:
@@ -635,7 +695,7 @@ async def predict(image):
635
  "show_back": False,
636
  "image": annotated_image,
637
  "is_multi_dog": len(dogs) > 1,
638
- "html_output": html_output # 儲存完整的 HTML 輸出
639
  }
640
  return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
641
 
@@ -644,6 +704,7 @@ async def predict(image):
644
  print(error_msg)
645
  return error_msg, None, gr.update(visible=False, choices=[]), None
646
 
 
647
 
648
 
649
  def show_details_html(choice, previous_output, initial_state):
 
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
615
+
616
+ try:
617
+ if isinstance(image, np.ndarray):
618
+ image = Image.fromarray(image)
619
+
620
+ dogs = await detect_multiple_dogs(image)
621
+
622
+ color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
623
  dogs_info = ""
624
+ buttons = []
625
+ annotated_image = image.copy()
626
+ draw = ImageDraw.Draw(annotated_image)
627
+ font = ImageFont.load_default()
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)]
632
  draw.rectangle(box, outline=color, width=3)
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 += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px; border: 1px solid #ddd; border-radius: 5px;">'
637
  dogs_info += f'<h2 style="background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0;">Dog {i+1}</h2>'
638
+
639
  if top1_prob >= 0.45:
640
  breed = topk_breeds[0]
 
641
  description = get_dog_description(breed)
642
  dogs_info += format_description_html(description, breed)
643
  elif combined_confidence >= 0.15:
644
  dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
645
  for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
646
  dogs_info += f"<li><strong>{breed}</strong> ({prob} confidence)</li>"
 
647
  dogs_info += "</ul>"
648
  dogs_info += '<div class="breed-buttons">'
649
  for breed in topk_breeds[:3]:
 
655
  dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
656
 
657
  dogs_info += '</div>'
 
658
 
 
659
  html_output = f"""
660
  <style>
661
  .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); }}
 
664
  .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
665
  </style>
666
  {dogs_info}
 
667
  """
668
 
669
  if buttons:
 
685
  "show_back": True,
686
  "image": annotated_image,
687
  "is_multi_dog": len(dogs) > 1,
688
+ "html_output": html_output
689
  }
690
  return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
691
  else:
 
695
  "show_back": False,
696
  "image": annotated_image,
697
  "is_multi_dog": len(dogs) > 1,
698
+ "html_output": html_output
699
  }
700
  return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
701
 
 
704
  print(error_msg)
705
  return error_msg, None, gr.update(visible=False, choices=[]), None
706
 
707
+
708
 
709
 
710
  def show_details_html(choice, previous_output, initial_state):