DawnC commited on
Commit
aa82089
·
1 Parent(s): 0d69c9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -617,29 +617,30 @@ async def predict(image):
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
-
624
  dogs_info = ""
625
  all_breeds = set() # 使用集合來避免重複
626
- # 創建一個新的圖像副本用於註釋
627
- annotated_image = image.copy()
628
- draw = ImageDraw.Draw(annotated_image)
629
- font = ImageFont.load_default()
630
-
631
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
632
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
633
  color = color_list[i % len(color_list)]
634
  draw.rectangle(box, outline=color, width=3)
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'''
639
  <div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px; border: 1px solid #ddd; border-radius: 5px;">
640
  <h2 style="background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0;">Dog {i+1}</h2>
641
  '''
642
-
643
  if top1_prob >= 0.45:
644
  breed = topk_breeds[0]
645
  all_breeds.add(breed)
@@ -655,6 +656,7 @@ async def predict(image):
655
  for breed in topk_breeds[:3]:
656
  button_id = f"Dog {i+1}: More about {breed}"
657
  dogs_info += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
 
658
  dogs_info += '</div>'
659
  else:
660
  dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
@@ -709,7 +711,6 @@ async def predict(image):
709
  print(error_msg)
710
  return error_msg, None, gr.update(visible=False, choices=[]), None
711
 
712
-
713
 
714
 
715
  def show_details_html(choice, previous_output, initial_state):
 
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)
634
  color = color_list[i % len(color_list)]
635
  draw.rectangle(box, outline=color, width=3)
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)
 
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>"
 
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):