DawnC commited on
Commit
a0c885f
1 Parent(s): d19b943

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -3
app.py CHANGED
@@ -225,13 +225,22 @@ async def detect_multiple_dogs(image, conf_threshold=0.35, iou_threshold=0.5):
225
  def filter_detections(dogs, image_size):
226
  filtered_dogs = []
227
  image_area = image_size[0] * image_size[1]
228
-
 
 
 
 
 
 
 
 
 
229
  for dog in dogs:
230
- _, _, box = dog
231
  dog_area = (box[2] - box[0]) * (box[3] - box[1])
232
  area_ratio = dog_area / image_area
233
 
234
- if 0.01 < area_ratio < 0.9: # 過濾掉太小或太大的檢測框
235
  filtered_dogs.append(dog)
236
 
237
  return filtered_dogs
 
225
  def filter_detections(dogs, image_size):
226
  filtered_dogs = []
227
  image_area = image_size[0] * image_size[1]
228
+ num_dogs = len(dogs)
229
+
230
+ # 根據檢測到的狗的數量動態調整閾值
231
+ if num_dogs > 5:
232
+ min_ratio, max_ratio = 0.003, 0.5
233
+ elif num_dogs > 2:
234
+ min_ratio, max_ratio = 0.005, 0.6
235
+ else:
236
+ min_ratio, max_ratio = 0.01, 0.7
237
+
238
  for dog in dogs:
239
+ _, confidence, box = dog
240
  dog_area = (box[2] - box[0]) * (box[3] - box[1])
241
  area_ratio = dog_area / image_area
242
 
243
+ if min_ratio < area_ratio < max_ratio:
244
  filtered_dogs.append(dog)
245
 
246
  return filtered_dogs