Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -193,7 +193,7 @@ async def predict_single_dog(image):
|
|
| 193 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
| 194 |
return top1_prob, topk_breeds, topk_probs_percent
|
| 195 |
|
| 196 |
-
async def detect_multiple_dogs(image, conf_threshold=0.
|
| 197 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
| 198 |
dogs = []
|
| 199 |
boxes = []
|
|
@@ -201,17 +201,25 @@ async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.5):
|
|
| 201 |
if box.cls == 16: # COCO dataset class for dog is 16
|
| 202 |
xyxy = box.xyxy[0].tolist()
|
| 203 |
confidence = box.conf.item()
|
| 204 |
-
boxes.append(xyxy)
|
| 205 |
|
| 206 |
# 如果沒有檢測到狗,使用整張圖片
|
| 207 |
if not boxes:
|
| 208 |
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
| 209 |
else:
|
| 210 |
-
#
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
return dogs
|
| 217 |
|
|
@@ -221,7 +229,7 @@ def merge_boxes(boxes, iou_threshold=0.5):
|
|
| 221 |
base_box = boxes.pop(0)
|
| 222 |
i = 0
|
| 223 |
while i < len(boxes):
|
| 224 |
-
if calculate_iou(base_box, boxes[i]) > iou_threshold:
|
| 225 |
base_box = merge_two_boxes(base_box, boxes.pop(i))
|
| 226 |
else:
|
| 227 |
i += 1
|
|
@@ -242,12 +250,13 @@ def calculate_iou(box1, box2):
|
|
| 242 |
return iou
|
| 243 |
|
| 244 |
def merge_two_boxes(box1, box2):
|
| 245 |
-
return
|
| 246 |
-
min(box1[0], box2[0]),
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
|
|
|
| 251 |
|
| 252 |
|
| 253 |
async def process_single_dog(image):
|
|
@@ -494,7 +503,7 @@ async def predict(image):
|
|
| 494 |
"is_multi_dog": len(dogs) > 1,
|
| 495 |
"dogs_info": explanations
|
| 496 |
}
|
| 497 |
-
return final_explanation, annotated_image, gr.update(visible=
|
| 498 |
else:
|
| 499 |
initial_state = {
|
| 500 |
"explanation": final_explanation,
|
|
|
|
| 193 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
| 194 |
return top1_prob, topk_breeds, topk_probs_percent
|
| 195 |
|
| 196 |
+
async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.45):
|
| 197 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
| 198 |
dogs = []
|
| 199 |
boxes = []
|
|
|
|
| 201 |
if box.cls == 16: # COCO dataset class for dog is 16
|
| 202 |
xyxy = box.xyxy[0].tolist()
|
| 203 |
confidence = box.conf.item()
|
| 204 |
+
boxes.append((xyxy, confidence))
|
| 205 |
|
| 206 |
# 如果沒有檢測到狗,使用整張圖片
|
| 207 |
if not boxes:
|
| 208 |
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
| 209 |
else:
|
| 210 |
+
# 按置信度排序並選擇前4個框(如果有的話)
|
| 211 |
+
sorted_boxes = sorted(boxes, key=lambda x: x[1], reverse=True)[:4]
|
| 212 |
+
|
| 213 |
+
for box, confidence in sorted_boxes:
|
| 214 |
+
x1, y1, x2, y2 = box
|
| 215 |
+
# 擴大框的大小
|
| 216 |
+
w, h = x2 - x1, y2 - y1
|
| 217 |
+
x1 = max(0, x1 - w * 0.1)
|
| 218 |
+
y1 = max(0, y1 - h * 0.1)
|
| 219 |
+
x2 = min(image.width, x2 + w * 0.1)
|
| 220 |
+
y2 = min(image.height, y2 + h * 0.1)
|
| 221 |
+
cropped_image = image.crop((x1, y1, x2, y2))
|
| 222 |
+
dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
|
| 223 |
|
| 224 |
return dogs
|
| 225 |
|
|
|
|
| 229 |
base_box = boxes.pop(0)
|
| 230 |
i = 0
|
| 231 |
while i < len(boxes):
|
| 232 |
+
if calculate_iou(base_box[0], boxes[i][0]) > iou_threshold:
|
| 233 |
base_box = merge_two_boxes(base_box, boxes.pop(i))
|
| 234 |
else:
|
| 235 |
i += 1
|
|
|
|
| 250 |
return iou
|
| 251 |
|
| 252 |
def merge_two_boxes(box1, box2):
|
| 253 |
+
return (
|
| 254 |
+
[min(box1[0][0], box2[0][0]),
|
| 255 |
+
min(box1[0][1], box2[0][1]),
|
| 256 |
+
max(box1[0][2], box2[0][2]),
|
| 257 |
+
max(box1[0][3], box2[0][3])],
|
| 258 |
+
max(box1[1], box2[1]) # 取較高的置信度
|
| 259 |
+
)
|
| 260 |
|
| 261 |
|
| 262 |
async def process_single_dog(image):
|
|
|
|
| 503 |
"is_multi_dog": len(dogs) > 1,
|
| 504 |
"dogs_info": explanations
|
| 505 |
}
|
| 506 |
+
return final_explanation, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
| 507 |
else:
|
| 508 |
initial_state = {
|
| 509 |
"explanation": final_explanation,
|