DawnC commited on
Commit
362066d
·
1 Parent(s): 6c3d0bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -5
app.py CHANGED
@@ -435,7 +435,6 @@ async def predict(image):
435
  image = Image.fromarray(image)
436
 
437
  dogs = await detect_multiple_dogs(image)
438
- # 更柔和的顏色組合
439
  single_dog_color = '#4A90E2' # 柔和的藍色
440
  color_list = ['#4A90E2', '#34C759', '#007AFF', '#5856D6', '#5AC8FA', '#FFB246', '#9C6ADE']
441
  annotated_image = image.copy()
@@ -449,11 +448,30 @@ async def predict(image):
449
  dogs_info = ""
450
 
451
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
452
- # 單狗時使用柔和的藍色
453
  color = single_dog_color if len(dogs) == 1 else color_list[i % len(color_list)]
454
 
455
- # ... (標記框和標籤的代碼保持不變) ...
456
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
457
  if combined_confidence < 0.15:
458
  dogs_info += f'''
459
  <div class="dog-info-header" style="background-color: {color}10;">
@@ -512,7 +530,31 @@ async def predict(image):
512
  </div>
513
  '''
514
  else:
515
- # ... (中等信心度的代碼保持類似,但使用新的樣式) ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
516
 
517
  html_output = f"""
518
  <style>
 
435
  image = Image.fromarray(image)
436
 
437
  dogs = await detect_multiple_dogs(image)
 
438
  single_dog_color = '#4A90E2' # 柔和的藍色
439
  color_list = ['#4A90E2', '#34C759', '#007AFF', '#5856D6', '#5AC8FA', '#FFB246', '#9C6ADE']
440
  annotated_image = image.copy()
 
448
  dogs_info = ""
449
 
450
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
 
451
  color = single_dog_color if len(dogs) == 1 else color_list[i % len(color_list)]
452
 
453
+ # 優化圖片上的標記
454
+ draw.rectangle(box, outline=color, width=4)
455
+ label = f"Dog {i+1}"
456
+ label_bbox = draw.textbbox((0, 0), label, font=font)
457
+ label_width = label_bbox[2] - label_bbox[0]
458
+ label_height = label_bbox[3] - label_bbox[1]
459
+
460
+ label_x = box[0] + 5
461
+ label_y = box[1] + 5
462
+ draw.rectangle(
463
+ [label_x - 2, label_y - 2, label_x + label_width + 4, label_y + label_height + 4],
464
+ fill='white',
465
+ outline=color,
466
+ width=2
467
+ )
468
+ draw.text((label_x, label_y), label, fill=color, font=font)
469
+
470
+ top1_prob, topk_breeds, relative_probs = await predict_single_dog(cropped_image)
471
+ combined_confidence = detection_confidence * top1_prob
472
+
473
+ dogs_info += f'<div class="dog-info-card" style="border-left: 6px solid {color};">'
474
+
475
  if combined_confidence < 0.15:
476
  dogs_info += f'''
477
  <div class="dog-info-header" style="background-color: {color}10;">
 
530
  </div>
531
  '''
532
  else:
533
+ dogs_info += f'''
534
+ <div class="dog-info-header" style="background-color: {color}10;">
535
+ <span class="dog-label" style="color: {color};">Dog {i+1}</span>
536
+ </div>
537
+ <div class="breed-info">
538
+ <div class="model-uncertainty-note">
539
+ Note: The model is showing some uncertainty in its predictions.
540
+ Here are the most likely breeds based on the available visual features.
541
+ </div>
542
+ <h3 class="breeds-title">Top 3 possible breeds:</h3>
543
+ '''
544
+
545
+ for j, (breed, prob) in enumerate(zip(topk_breeds, relative_probs)):
546
+ description = get_dog_description(breed)
547
+ dogs_info += f'''
548
+ <div class="breed-section">
549
+ <div class="confidence-score" style="color: {color};">{breed} (Confidence: {prob})</div>
550
+ <div class="breed-info-content">
551
+ {format_description_html(description, breed)}
552
+ </div>
553
+ </div>
554
+ '''
555
+ dogs_info += '</div>'
556
+
557
+ dogs_info += '</div>'
558
 
559
  html_output = f"""
560
  <style>