DawnC commited on
Commit
ce4e4fc
1 Parent(s): 7ffe513

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -2
app.py CHANGED
@@ -167,22 +167,63 @@ async def predict_single_dog(image):
167
  return top1_prob, topk_breeds, topk_probs_percent
168
 
169
 
170
- async def detect_multiple_dogs(image, conf_threshold=0.35, iou_threshold=0.55):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
172
  dogs = []
173
  boxes = []
 
 
174
  for box in results.boxes:
175
  if box.cls == 16: # COCO dataset class for dog is 16
176
  xyxy = box.xyxy[0].tolist()
177
  confidence = box.conf.item()
 
 
 
 
 
 
 
 
178
  boxes.append((xyxy, confidence))
179
 
180
  if not boxes:
181
  dogs.append((image, 1.0, [0, 0, image.width, image.height]))
182
  else:
 
183
  nms_boxes = non_max_suppression(boxes, iou_threshold)
184
 
185
- for box, confidence in nms_boxes:
 
 
 
186
  x1, y1, x2, y2 = box
187
  w, h = x2 - x1, y2 - y1
188
  x1 = max(0, x1 - w * 0.05)
@@ -194,6 +235,30 @@ async def detect_multiple_dogs(image, conf_threshold=0.35, iou_threshold=0.55):
194
 
195
  return dogs
196
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
197
 
198
 
199
  def non_max_suppression(boxes, iou_threshold):
 
167
  return top1_prob, topk_breeds, topk_probs_percent
168
 
169
 
170
+ # async def detect_multiple_dogs(image, conf_threshold=0.35, iou_threshold=0.55):
171
+ # results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
172
+ # dogs = []
173
+ # boxes = []
174
+ # for box in results.boxes:
175
+ # if box.cls == 16: # COCO dataset class for dog is 16
176
+ # xyxy = box.xyxy[0].tolist()
177
+ # confidence = box.conf.item()
178
+ # boxes.append((xyxy, confidence))
179
+
180
+ # if not boxes:
181
+ # dogs.append((image, 1.0, [0, 0, image.width, image.height]))
182
+ # else:
183
+ # nms_boxes = non_max_suppression(boxes, iou_threshold)
184
+
185
+ # for box, confidence in nms_boxes:
186
+ # x1, y1, x2, y2 = box
187
+ # w, h = x2 - x1, y2 - y1
188
+ # x1 = max(0, x1 - w * 0.05)
189
+ # y1 = max(0, y1 - h * 0.05)
190
+ # x2 = min(image.width, x2 + w * 0.05)
191
+ # y2 = min(image.height, y2 + h * 0.05)
192
+ # cropped_image = image.crop((x1, y1, x2, y2))
193
+ # dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
194
+
195
+ # return dogs
196
+
197
+ async def detect_multiple_dogs(image, conf_threshold=0.35, iou_threshold=0.55, min_area_ratio=0.01, overlap_threshold=0.8):
198
  results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
199
  dogs = []
200
  boxes = []
201
+ image_area = image.width * image.height
202
+
203
  for box in results.boxes:
204
  if box.cls == 16: # COCO dataset class for dog is 16
205
  xyxy = box.xyxy[0].tolist()
206
  confidence = box.conf.item()
207
+
208
+ # 計算檢測框面積
209
+ box_area = (xyxy[2] - xyxy[0]) * (xyxy[3] - xyxy[1])
210
+
211
+ # 如果檢測框面積小於圖片面積的一定比例,則忽略
212
+ if box_area / image_area < min_area_ratio:
213
+ continue
214
+
215
  boxes.append((xyxy, confidence))
216
 
217
  if not boxes:
218
  dogs.append((image, 1.0, [0, 0, image.width, image.height]))
219
  else:
220
+ # 使用非最大抑制
221
  nms_boxes = non_max_suppression(boxes, iou_threshold)
222
 
223
+ # 進一步合併重疊嚴重的框
224
+ merged_boxes = merge_overlapping_boxes(nms_boxes, overlap_threshold)
225
+
226
+ for box, confidence in merged_boxes:
227
  x1, y1, x2, y2 = box
228
  w, h = x2 - x1, y2 - y1
229
  x1 = max(0, x1 - w * 0.05)
 
235
 
236
  return dogs
237
 
238
+ def merge_overlapping_boxes(boxes, overlap_threshold):
239
+ merged = []
240
+ while boxes:
241
+ base_box = boxes.pop(0)
242
+ i = 0
243
+ while i < len(boxes):
244
+ if calculate_iou(base_box[0], boxes[i][0]) > overlap_threshold:
245
+ # 合併框,取較大的置信度
246
+ merged_box = merge_boxes(base_box[0], boxes[i][0])
247
+ merged_conf = max(base_box[1], boxes[i][1])
248
+ base_box = (merged_box, merged_conf)
249
+ boxes.pop(i)
250
+ else:
251
+ i += 1
252
+ merged.append(base_box)
253
+ return merged
254
+
255
+ def merge_boxes(box1, box2):
256
+ x1 = min(box1[0], box2[0])
257
+ y1 = min(box1[1], box2[1])
258
+ x2 = max(box1[2], box2[2])
259
+ y2 = max(box1[3], box2[3])
260
+ return [x1, y1, x2, y2]
261
+
262
 
263
 
264
  def non_max_suppression(boxes, iou_threshold):