DawnC commited on
Commit
909939e
1 Parent(s): 614e5ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -6
app.py CHANGED
@@ -244,12 +244,12 @@ def _predict_single_dog(image):
244
  # return error_msg, None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), None
245
 
246
 
247
- async def detect_multiple_dogs(image, conf_threshold=0.1, iou_threshold=0.3, merge_threshold=0.5):
248
  results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
249
  dogs = []
250
 
251
  image_area = image.width * image.height
252
- min_area_ratio = 0.005 # 降低最小面積比例以捕獲更多小型狗狗
253
 
254
  for box in results.boxes:
255
  if box.cls == 16: # COCO 數據集中狗的類別是 16
@@ -259,17 +259,57 @@ async def detect_multiple_dogs(image, conf_threshold=0.1, iou_threshold=0.3, mer
259
  confidence = box.conf.item()
260
  dogs.append((xyxy, confidence))
261
 
262
- # 使用 NMS 進行後處理
263
  if dogs:
264
  boxes = torch.tensor([dog[0] for dog in dogs])
265
  scores = torch.tensor([dog[1] for dog in dogs])
 
 
266
  keep = nms(boxes, scores, iou_threshold)
267
 
268
  merged_dogs = []
269
  for i in keep:
270
  xyxy = boxes[i].tolist()
271
  confidence = scores[i].item()
272
- # 擴大邊界框以包含更多上下文
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273
  expanded_xyxy = [
274
  max(0, xyxy[0] - 20),
275
  max(0, xyxy[1] - 20),
@@ -277,9 +317,9 @@ async def detect_multiple_dogs(image, conf_threshold=0.1, iou_threshold=0.3, mer
277
  min(image.height, xyxy[3] + 20)
278
  ]
279
  cropped_image = image.crop(expanded_xyxy)
280
- merged_dogs.append((cropped_image, confidence, expanded_xyxy))
281
 
282
- return merged_dogs
283
 
284
  # 如果沒有檢測到狗狗,返回整張圖片
285
  return [(image, 1.0, [0, 0, image.width, image.height])]
 
244
  # return error_msg, None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), None
245
 
246
 
247
+ async def detect_multiple_dogs(image, conf_threshold=0.1, iou_threshold=0.4, merge_threshold=0.7):
248
  results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
249
  dogs = []
250
 
251
  image_area = image.width * image.height
252
+ min_area_ratio = 0.005 # 最小檢測面積佔整個圖像的比例
253
 
254
  for box in results.boxes:
255
  if box.cls == 16: # COCO 數據集中狗的類別是 16
 
259
  confidence = box.conf.item()
260
  dogs.append((xyxy, confidence))
261
 
 
262
  if dogs:
263
  boxes = torch.tensor([dog[0] for dog in dogs])
264
  scores = torch.tensor([dog[1] for dog in dogs])
265
+
266
+ # 應用 NMS
267
  keep = nms(boxes, scores, iou_threshold)
268
 
269
  merged_dogs = []
270
  for i in keep:
271
  xyxy = boxes[i].tolist()
272
  confidence = scores[i].item()
273
+ merged_dogs.append((xyxy, confidence))
274
+
275
+ # 後處理:分離過於接近的檢測框
276
+ final_dogs = []
277
+ while merged_dogs:
278
+ base_dog = merged_dogs.pop(0)
279
+ to_merge = [base_dog]
280
+
281
+ i = 0
282
+ while i < len(merged_dogs):
283
+ iou = box_iou(torch.tensor([base_dog[0]]), torch.tensor([merged_dogs[i][0]]))[0][0].item()
284
+ if iou > merge_threshold:
285
+ to_merge.append(merged_dogs.pop(i))
286
+ else:
287
+ i += 1
288
+
289
+ if len(to_merge) == 1:
290
+ final_dogs.append(base_dog)
291
+ else:
292
+ # 如果檢測到多個重疊框,嘗試分離它們
293
+ centers = torch.tensor([[((box[0] + box[2]) / 2, (box[1] + box[3]) / 2)] for box, _ in to_merge])
294
+ distances = torch.cdist(centers, centers)
295
+
296
+ if torch.any(distances > 0): # 確保不是完全重疊
297
+ max_distance = distances.max()
298
+ if max_distance > (base_dog[0][2] - base_dog[0][0]) * 0.5: # 如果最大距離大於框寬度的一半
299
+ final_dogs.extend(to_merge)
300
+ else:
301
+ # 合併為一個框
302
+ merged_box = torch.tensor([box for box, _ in to_merge]).mean(dim=0)
303
+ merged_confidence = max(conf for _, conf in to_merge)
304
+ final_dogs.append((merged_box.tolist(), merged_confidence))
305
+ else:
306
+ # 完全重疊的情況,保留置信度最高的
307
+ best_dog = max(to_merge, key=lambda x: x[1])
308
+ final_dogs.append(best_dog)
309
+
310
+ # 擴展邊界框並創建剪裁的圖像
311
+ expanded_dogs = []
312
+ for xyxy, confidence in final_dogs:
313
  expanded_xyxy = [
314
  max(0, xyxy[0] - 20),
315
  max(0, xyxy[1] - 20),
 
317
  min(image.height, xyxy[3] + 20)
318
  ]
319
  cropped_image = image.crop(expanded_xyxy)
320
+ expanded_dogs.append((cropped_image, confidence, expanded_xyxy))
321
 
322
+ return expanded_dogs
323
 
324
  # 如果沒有檢測到狗狗,返回整張圖片
325
  return [(image, 1.0, [0, 0, image.width, image.height])]