Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -194,7 +194,7 @@ async def predict_single_dog(image):
|
|
| 194 |
return top1_prob, topk_breeds, topk_probs_percent
|
| 195 |
|
| 196 |
|
| 197 |
-
async def detect_multiple_dogs(image, conf_threshold=0.
|
| 198 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
| 199 |
dogs = []
|
| 200 |
boxes = []
|
|
@@ -208,22 +208,47 @@ async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.45):
|
|
| 208 |
if not boxes:
|
| 209 |
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
| 210 |
else:
|
| 211 |
-
#
|
| 212 |
sorted_boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
|
| 213 |
|
| 214 |
-
|
|
|
|
|
|
|
|
|
|
| 215 |
x1, y1, x2, y2 = box
|
| 216 |
-
#
|
| 217 |
w, h = x2 - x1, y2 - y1
|
| 218 |
-
x1 = max(0, x1 - w * 0.
|
| 219 |
-
y1 = max(0, y1 - h * 0.
|
| 220 |
-
x2 = min(image.width, x2 + w * 0.
|
| 221 |
-
y2 = min(image.height, y2 + h * 0.
|
| 222 |
cropped_image = image.crop((x1, y1, x2, y2))
|
| 223 |
dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
|
| 224 |
|
| 225 |
return dogs
|
| 226 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
async def process_single_dog(image):
|
| 229 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
|
|
|
| 194 |
return top1_prob, topk_breeds, topk_probs_percent
|
| 195 |
|
| 196 |
|
| 197 |
+
async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.3):
|
| 198 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
| 199 |
dogs = []
|
| 200 |
boxes = []
|
|
|
|
| 208 |
if not boxes:
|
| 209 |
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
| 210 |
else:
|
| 211 |
+
# 按置信度排序
|
| 212 |
sorted_boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
|
| 213 |
|
| 214 |
+
# 使用非極大值抑制(NMS)來合併重疊的框
|
| 215 |
+
nms_boxes = non_max_suppression(sorted_boxes, iou_threshold)
|
| 216 |
+
|
| 217 |
+
for box, confidence in nms_boxes:
|
| 218 |
x1, y1, x2, y2 = box
|
| 219 |
+
# 擴大框的大小以包含更多上下文
|
| 220 |
w, h = x2 - x1, y2 - y1
|
| 221 |
+
x1 = max(0, x1 - w * 0.15)
|
| 222 |
+
y1 = max(0, y1 - h * 0.15)
|
| 223 |
+
x2 = min(image.width, x2 + w * 0.15)
|
| 224 |
+
y2 = min(image.height, y2 + h * 0.15)
|
| 225 |
cropped_image = image.crop((x1, y1, x2, y2))
|
| 226 |
dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
|
| 227 |
|
| 228 |
return dogs
|
| 229 |
|
| 230 |
+
def non_max_suppression(boxes, iou_threshold):
|
| 231 |
+
keep = []
|
| 232 |
+
boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
|
| 233 |
+
while boxes:
|
| 234 |
+
current = boxes.pop(0)
|
| 235 |
+
keep.append(current)
|
| 236 |
+
boxes = [box for box in boxes if calculate_iou(current[0], box[0]) < iou_threshold]
|
| 237 |
+
return keep
|
| 238 |
+
|
| 239 |
+
def calculate_iou(box1, box2):
|
| 240 |
+
x1 = max(box1[0], box2[0])
|
| 241 |
+
y1 = max(box1[1], box2[1])
|
| 242 |
+
x2 = min(box1[2], box2[2])
|
| 243 |
+
y2 = min(box1[3], box2[3])
|
| 244 |
+
|
| 245 |
+
intersection = max(0, x2 - x1) * max(0, y2 - y1)
|
| 246 |
+
area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
|
| 247 |
+
area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
|
| 248 |
+
|
| 249 |
+
iou = intersection / float(area1 + area2 - intersection)
|
| 250 |
+
return iou
|
| 251 |
+
|
| 252 |
|
| 253 |
async def process_single_dog(image):
|
| 254 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|