DawnC commited on
Commit
18941a4
1 Parent(s): 9543b48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -0
app.py CHANGED
@@ -195,6 +195,31 @@ async def predict_single_dog(image):
195
  # return dogs
196
 
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  def non_max_suppression(boxes, iou_threshold):
199
  keep = []
200
  boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
 
195
  # return dogs
196
 
197
 
198
+ async def detect_multiple_dogs(image, conf_threshold=0.4, iou_threshold=0.5):
199
+ # 提高conf_threshold來減少無效框
200
+ results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
201
+ dogs = []
202
+ boxes = []
203
+ for box in results.boxes:
204
+ if box.cls == 16: # 確保是狗的類別
205
+ xyxy = box.xyxy[0].tolist()
206
+ confidence = box.conf.item()
207
+ # 只保存高信心的框,降低不必要框的數量
208
+ if confidence >= conf_threshold:
209
+ boxes.append((xyxy, confidence))
210
+
211
+ if not boxes:
212
+ # 沒有檢測到狗,使用整張圖
213
+ dogs.append((image, 1.0, [0, 0, image.width, image.height]))
214
+ else:
215
+ nms_boxes = non_max_suppression(boxes, iou_threshold)
216
+ for box, confidence in nms_boxes:
217
+ x1, y1, x2, y2 = box
218
+ cropped_image = image.crop((x1, y1, x2, y2))
219
+ dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
220
+
221
+ return dogs
222
+
223
  def non_max_suppression(boxes, iou_threshold):
224
  keep = []
225
  boxes = sorted(boxes, key=lambda x: x[1], reverse=True)