DawnC commited on
Commit
44e8e07
·
1 Parent(s): 30cf82f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -26
app.py CHANGED
@@ -518,57 +518,83 @@ 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
- dogs_info += '<div class="breed-buttons">' # new
547
  for breed in topk_breeds[:3]:
548
  button_id = f"Dog {i+1}: More about {breed}"
549
- buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
550
  buttons.append(button_id)
551
- #buttons_html += '</div>' 原本有
552
- dogs_info += '</div>' # new
553
-
554
-
555
  else:
556
  dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
557
 
558
  dogs_info += '</div>'
559
-
560
- #dogs_info += buttons_html 原本有
561
 
562
 
563
- # html_output = f"""
564
- # <style>
565
- # .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); }}
566
- # .dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
567
- # .breed-buttons {{ margin-top: 10px; }}
568
- # .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
569
- # </style>
570
- # {dogs_info}
571
- # """
572
  html_output = f"""
573
  <style>
574
  .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); }}
 
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]:
587
  button_id = f"Dog {i+1}: More about {breed}"
588
+ dogs_info += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
589
  buttons.append(button_id)
590
+ dogs_info += '</div>'
 
 
 
591
  else:
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); }}