Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -20,7 +20,7 @@ logger = logging.getLogger(__name__)
|
|
20 |
|
21 |
|
22 |
# 下載YOLOv8預訓練模型
|
23 |
-
model_yolo = YOLO('
|
24 |
|
25 |
|
26 |
dog_breeds = ["Afghan_Hound", "African_Hunting_Dog", "Airedale", "American_Staffordshire_Terrier",
|
@@ -229,32 +229,32 @@ async def detect_multiple_dogs(image, conf_threshold=0.15, iou_threshold=0.3):
|
|
229 |
confidence = box.conf.item()
|
230 |
boxes.append((xyxy, confidence))
|
231 |
|
232 |
-
# 如果沒有檢測到任何狗
|
233 |
if not boxes:
|
234 |
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
235 |
else:
|
236 |
-
|
|
|
|
|
|
|
237 |
|
238 |
-
# 進一步優化處理重疊框邏輯
|
239 |
for box, confidence in nms_boxes:
|
240 |
x1, y1, x2, y2 = box
|
241 |
w, h = x2 - x1, y2 - y1
|
242 |
|
243 |
-
#
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
y2 = min(image.height, y2 + h * 0.05)
|
251 |
|
252 |
cropped_image = image.crop((x1, y1, x2, y2))
|
253 |
dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
|
254 |
|
255 |
return dogs
|
256 |
|
257 |
-
|
258 |
def non_max_suppression(boxes, iou_threshold=0.3):
|
259 |
keep = []
|
260 |
boxes = sorted(boxes, key=lambda x: x[1], reverse=True) # 按信心分數排序
|
@@ -265,23 +265,23 @@ def non_max_suppression(boxes, iou_threshold=0.3):
|
|
265 |
boxes = [box for box in boxes if calculate_iou(current[0], box[0]) < iou_threshold]
|
266 |
return keep
|
267 |
|
|
|
268 |
def calculate_iou(box1, box2):
|
269 |
x1 = max(box1[0], box2[0])
|
270 |
y1 = max(box1[1], box2[1])
|
271 |
x2 = min(box1[2], box2[2])
|
272 |
y2 = min(box1[3], box2[3])
|
273 |
|
274 |
-
# 計算交集面積
|
275 |
intersection = max(0, x2 - x1) * max(0, y2 - y1)
|
276 |
area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
|
277 |
area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
|
278 |
|
279 |
-
# 計算IOU
|
280 |
iou = intersection / float(area1 + area2 - intersection)
|
281 |
return iou
|
282 |
|
283 |
|
284 |
|
|
|
285 |
async def process_single_dog(image):
|
286 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
287 |
if top1_prob < 0.2:
|
|
|
20 |
|
21 |
|
22 |
# 下載YOLOv8預訓練模型
|
23 |
+
model_yolo = YOLO('yolov8s.pt') # 使用 YOLOv8 預訓練模型
|
24 |
|
25 |
|
26 |
dog_breeds = ["Afghan_Hound", "African_Hunting_Dog", "Airedale", "American_Staffordshire_Terrier",
|
|
|
229 |
confidence = box.conf.item()
|
230 |
boxes.append((xyxy, confidence))
|
231 |
|
|
|
232 |
if not boxes:
|
233 |
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
234 |
else:
|
235 |
+
# 新增框的面積過濾條件,避免太小的框
|
236 |
+
valid_boxes = [box for box in boxes if (box[0][2] - box[0][0]) * (box[0][3] - box[0][1]) > 0.1 * image.width * image.height]
|
237 |
+
|
238 |
+
nms_boxes = non_max_suppression(valid_boxes, iou_threshold)
|
239 |
|
|
|
240 |
for box, confidence in nms_boxes:
|
241 |
x1, y1, x2, y2 = box
|
242 |
w, h = x2 - x1, y2 - y1
|
243 |
|
244 |
+
# 調整框的位置,處理重疊框問題
|
245 |
+
if w * h < 0.2 * image.width * image.height and confidence < 0.2:
|
246 |
+
continue # 跳過信心分數過低的框
|
247 |
+
|
248 |
+
# 根據框的大小動態調整信心門檻
|
249 |
+
if w * h < 0.05 * image.width * image.height:
|
250 |
+
continue # 過小的框直接跳過
|
|
|
251 |
|
252 |
cropped_image = image.crop((x1, y1, x2, y2))
|
253 |
dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
|
254 |
|
255 |
return dogs
|
256 |
|
257 |
+
|
258 |
def non_max_suppression(boxes, iou_threshold=0.3):
|
259 |
keep = []
|
260 |
boxes = sorted(boxes, key=lambda x: x[1], reverse=True) # 按信心分數排序
|
|
|
265 |
boxes = [box for box in boxes if calculate_iou(current[0], box[0]) < iou_threshold]
|
266 |
return keep
|
267 |
|
268 |
+
|
269 |
def calculate_iou(box1, box2):
|
270 |
x1 = max(box1[0], box2[0])
|
271 |
y1 = max(box1[1], box2[1])
|
272 |
x2 = min(box1[2], box2[2])
|
273 |
y2 = min(box1[3], box2[3])
|
274 |
|
|
|
275 |
intersection = max(0, x2 - x1) * max(0, y2 - y1)
|
276 |
area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
|
277 |
area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
|
278 |
|
|
|
279 |
iou = intersection / float(area1 + area2 - intersection)
|
280 |
return iou
|
281 |
|
282 |
|
283 |
|
284 |
+
|
285 |
async def process_single_dog(image):
|
286 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
287 |
if top1_prob < 0.2:
|