DawnC commited on
Commit
d825a8b
·
verified ·
1 Parent(s): 2156e3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -15
app.py CHANGED
@@ -194,7 +194,7 @@ async def predict_single_dog(image):
194
  return top1_prob, topk_breeds, topk_probs_percent
195
 
196
 
197
- async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.3):
198
  results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
199
  dogs = []
200
  boxes = []
@@ -207,22 +207,17 @@ async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.3):
207
  if not boxes:
208
  dogs.append((image, 1.0, [0, 0, image.width, image.height]))
209
  else:
210
- sorted_boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
211
- nms_boxes = non_max_suppression(sorted_boxes, iou_threshold)
212
 
213
  for box, confidence in nms_boxes:
214
  x1, y1, x2, y2 = box
215
  w, h = x2 - x1, y2 - y1
216
- x1 = max(0, x1 - w * 0.1)
217
- y1 = max(0, y1 - h * 0.1)
218
- x2 = min(image.width, x2 + w * 0.1)
219
- y2 = min(image.height, y2 + h * 0.1)
220
  cropped_image = image.crop((x1, y1, x2, y2))
221
  dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
222
-
223
- # 如果只檢測到一隻狗,但置信度較低,添加整張圖片作為備選
224
- if len(dogs) == 1 and dogs[0][1] < 0.5:
225
- dogs.append((image, 1.0, [0, 0, image.width, image.height]))
226
 
227
  return dogs
228
 
@@ -470,21 +465,20 @@ async def predict(image):
470
  draw.rectangle(box, outline=color, width=3)
471
  draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
472
 
473
- # 結合 YOLO 檢測置信度和品種識別置信度
474
  combined_confidence = detection_confidence * top1_prob
475
 
476
- if combined_confidence >= 0.4:
477
  breed = topk_breeds[0]
478
  description = get_dog_description(breed)
479
  formatted_description = format_description(description, breed)
480
- explanations.append(f"Dog {i+1}: {formatted_description}")
481
  elif combined_confidence >= 0.2:
482
  dog_explanation = f"Dog {i+1}: Top 3 possible breeds:\n"
483
  dog_explanation += "\n".join([f"{j+1}. **{breed}** ({prob} confidence)" for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3]))])
484
  explanations.append(dog_explanation)
485
  buttons.extend([f"Dog {i+1}: More about {breed}" for breed in topk_breeds[:3]])
486
  else:
487
- explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
488
 
489
  final_explanation = "\n\n".join(explanations)
490
  if buttons:
 
194
  return top1_prob, topk_breeds, topk_probs_percent
195
 
196
 
197
+ async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.5):
198
  results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
199
  dogs = []
200
  boxes = []
 
207
  if not boxes:
208
  dogs.append((image, 1.0, [0, 0, image.width, image.height]))
209
  else:
210
+ nms_boxes = non_max_suppression(boxes, iou_threshold)
 
211
 
212
  for box, confidence in nms_boxes:
213
  x1, y1, x2, y2 = box
214
  w, h = x2 - x1, y2 - y1
215
+ x1 = max(0, x1 - w * 0.05)
216
+ y1 = max(0, y1 - h * 0.05)
217
+ x2 = min(image.width, x2 + w * 0.05)
218
+ y2 = min(image.height, y2 + h * 0.05)
219
  cropped_image = image.crop((x1, y1, x2, y2))
220
  dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
 
 
 
 
221
 
222
  return dogs
223
 
 
465
  draw.rectangle(box, outline=color, width=3)
466
  draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
467
 
 
468
  combined_confidence = detection_confidence * top1_prob
469
 
470
+ if top1_prob >= 0.5:
471
  breed = topk_breeds[0]
472
  description = get_dog_description(breed)
473
  formatted_description = format_description(description, breed)
474
+ explanations.append(f"Dog {i+1}: Breed: {breed}\n{formatted_description}")
475
  elif combined_confidence >= 0.2:
476
  dog_explanation = f"Dog {i+1}: Top 3 possible breeds:\n"
477
  dog_explanation += "\n".join([f"{j+1}. **{breed}** ({prob} confidence)" for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3]))])
478
  explanations.append(dog_explanation)
479
  buttons.extend([f"Dog {i+1}: More about {breed}" for breed in topk_breeds[:3]])
480
  else:
481
+ explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset. Please upload a clearer image.")
482
 
483
  final_explanation = "\n\n".join(explanations)
484
  if buttons: