DawnC commited on
Commit
8f63c2e
1 Parent(s): 48169b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -19
app.py CHANGED
@@ -196,21 +196,6 @@ async def predict_single_dog(image):
196
  # return dogs
197
 
198
 
199
- def filter_detections(dogs, image_size):
200
- filtered_dogs = []
201
- image_area = image_size[0] * image_size[1]
202
-
203
- for dog in dogs:
204
- _, _, box = dog
205
- dog_area = (box[2] - box[0]) * (box[3] - box[1])
206
- area_ratio = dog_area / image_area
207
-
208
- if 0.01 < area_ratio < 0.9: # 過濾掉太小或太大的檢測框
209
- filtered_dogs.append(dog)
210
-
211
- return filtered_dogs
212
-
213
-
214
  async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.4):
215
  results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
216
  dogs = []
@@ -230,14 +215,27 @@ async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.4):
230
  for box, confidence in nms_boxes:
231
  x1, y1, x2, y2 = [int(coord) for coord in box]
232
  cropped_image = image.crop((x1, y1, x2, y2))
233
-
234
- if is_valid_dog(cropped_image):
235
- dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
236
-
237
  dogs = filter_detections(dogs, (image.width, image.height))
238
 
239
  return dogs
240
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
241
 
242
  def merge_overlapping_boxes(boxes, overlap_threshold):
243
  merged = []
 
196
  # return dogs
197
 
198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
  async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.4):
200
  results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
201
  dogs = []
 
215
  for box, confidence in nms_boxes:
216
  x1, y1, x2, y2 = [int(coord) for coord in box]
217
  cropped_image = image.crop((x1, y1, x2, y2))
218
+ dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
219
+
220
+ # 應用過濾器來移除可能的錯誤檢測
 
221
  dogs = filter_detections(dogs, (image.width, image.height))
222
 
223
  return dogs
224
 
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
238
+
239
 
240
  def merge_overlapping_boxes(boxes, overlap_threshold):
241
  merged = []