Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -312,7 +312,7 @@ def _predict_single_dog(image):
|
|
| 312 |
# return dogs
|
| 313 |
# 此為如果後面調不好 使用的版本
|
| 314 |
|
| 315 |
-
async def detect_multiple_dogs(image, conf_threshold=0.
|
| 316 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
| 317 |
dogs = []
|
| 318 |
for box in results.boxes:
|
|
@@ -320,42 +320,31 @@ async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.5):
|
|
| 320 |
xyxy = box.xyxy[0].tolist()
|
| 321 |
confidence = box.conf.item()
|
| 322 |
area = (xyxy[2] - xyxy[0]) * (xyxy[3] - xyxy[1])
|
| 323 |
-
|
|
|
|
| 324 |
cropped_image = image.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
|
| 325 |
dogs.append((cropped_image, confidence, xyxy))
|
| 326 |
|
| 327 |
-
#
|
| 328 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
|
| 330 |
return dogs
|
| 331 |
|
| 332 |
-
def
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
while i < len(dogs):
|
| 338 |
-
if calculate_iou(base[2], dogs[i][2]) > iou_threshold:
|
| 339 |
-
# 合併重疊的框
|
| 340 |
-
base = merge_boxes(base, dogs.pop(i))
|
| 341 |
-
else:
|
| 342 |
-
i += 1
|
| 343 |
-
merged_dogs.append(base)
|
| 344 |
-
return merged_dogs
|
| 345 |
-
|
| 346 |
-
def merge_boxes(box1, box2):
|
| 347 |
-
xyxy1, conf1, _ = box1
|
| 348 |
-
xyxy2, conf2, _ = box2
|
| 349 |
-
merged_xyxy = [
|
| 350 |
-
min(xyxy1[0], xyxy2[0]),
|
| 351 |
-
min(xyxy1[1], xyxy2[1]),
|
| 352 |
-
max(xyxy1[2], xyxy2[2]),
|
| 353 |
-
max(xyxy1[3], xyxy2[3])
|
| 354 |
-
]
|
| 355 |
-
merged_conf = max(conf1, conf2)
|
| 356 |
-
merged_image = Image.new('RGB', (int(merged_xyxy[2] - merged_xyxy[0]), int(merged_xyxy[3] - merged_xyxy[1])))
|
| 357 |
-
merged_image.paste(box1[0], (0, 0))
|
| 358 |
-
return (merged_image, merged_conf, merged_xyxy)
|
| 359 |
|
| 360 |
def calculate_iou(box1, box2):
|
| 361 |
# 計算兩個邊界框的交集面積
|
|
@@ -494,15 +483,15 @@ async def predict(image):
|
|
| 494 |
image = Image.fromarray(image)
|
| 495 |
|
| 496 |
dogs = await detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.5)
|
| 497 |
-
|
| 498 |
-
# 如果檢測到的狗的數量不合理,嘗試調整參數重新檢測
|
| 499 |
-
if len(dogs) > 5 or (len(dogs) == 0 and has_dog_features(image)):
|
| 500 |
-
dogs = await detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.4)
|
| 501 |
-
|
| 502 |
if len(dogs) == 0:
|
| 503 |
return await process_single_dog(image)
|
| 504 |
elif len(dogs) == 1:
|
| 505 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 506 |
else:
|
| 507 |
# 多狗情境
|
| 508 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
|
@@ -558,12 +547,13 @@ async def predict(image):
|
|
| 558 |
print(error_msg) # 添加日誌輸出
|
| 559 |
return error_msg, None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), None
|
| 560 |
|
| 561 |
-
def
|
| 562 |
-
#
|
| 563 |
# 這裡可以使用更複雜的方法,如特徵提取或輕量級模型
|
| 564 |
gray = image.convert('L')
|
| 565 |
edges = gray.filter(ImageFilter.FIND_EDGES)
|
| 566 |
-
|
|
|
|
| 567 |
|
| 568 |
async def process_single_dog(image):
|
| 569 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
|
|
|
| 312 |
# return dogs
|
| 313 |
# 此為如果後面調不好 使用的版本
|
| 314 |
|
| 315 |
+
async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.3):
|
| 316 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
| 317 |
dogs = []
|
| 318 |
for box in results.boxes:
|
|
|
|
| 320 |
xyxy = box.xyxy[0].tolist()
|
| 321 |
confidence = box.conf.item()
|
| 322 |
area = (xyxy[2] - xyxy[0]) * (xyxy[3] - xyxy[1])
|
| 323 |
+
image_area = image.width * image.height
|
| 324 |
+
if area > 0.01 * image_area: # 過濾掉太小的檢測框,但使用相對面積
|
| 325 |
cropped_image = image.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
|
| 326 |
dogs.append((cropped_image, confidence, xyxy))
|
| 327 |
|
| 328 |
+
# 如果檢測到的狗太少,嘗試降低閾值再次檢測
|
| 329 |
+
if len(dogs) < 2:
|
| 330 |
+
results = model_yolo(image, conf=conf_threshold/2, iou=iou_threshold)[0]
|
| 331 |
+
for box in results.boxes:
|
| 332 |
+
if box.cls == 16:
|
| 333 |
+
xyxy = box.xyxy[0].tolist()
|
| 334 |
+
confidence = box.conf.item()
|
| 335 |
+
area = (xyxy[2] - xyxy[0]) * (xyxy[3] - xyxy[1])
|
| 336 |
+
image_area = image.width * image.height
|
| 337 |
+
if area > 0.01 * image_area and not is_box_duplicate(xyxy, [d[2] for d in dogs]):
|
| 338 |
+
cropped_image = image.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
|
| 339 |
+
dogs.append((cropped_image, confidence, xyxy))
|
| 340 |
|
| 341 |
return dogs
|
| 342 |
|
| 343 |
+
def is_box_duplicate(new_box, existing_boxes, iou_threshold=0.5):
|
| 344 |
+
for box in existing_boxes:
|
| 345 |
+
if calculate_iou(new_box, box) > iou_threshold:
|
| 346 |
+
return True
|
| 347 |
+
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
|
| 349 |
def calculate_iou(box1, box2):
|
| 350 |
# 計算兩個邊界框的交集面積
|
|
|
|
| 483 |
image = Image.fromarray(image)
|
| 484 |
|
| 485 |
dogs = await detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.5)
|
| 486 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
if len(dogs) == 0:
|
| 488 |
return await process_single_dog(image)
|
| 489 |
elif len(dogs) == 1:
|
| 490 |
+
# 如果只檢測到一隻狗,但圖像可能包含多隻狗,再次嘗試檢測
|
| 491 |
+
if has_multiple_dogs(image):
|
| 492 |
+
dogs = await detect_multiple_dogs(image, conf_threshold=0.1, iou_threshold=0.2)
|
| 493 |
+
if len(dogs) == 1:
|
| 494 |
+
return await process_single_dog(dogs[0][0])
|
| 495 |
else:
|
| 496 |
# 多狗情境
|
| 497 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
|
|
|
| 547 |
print(error_msg) # 添加日誌輸出
|
| 548 |
return error_msg, None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), None
|
| 549 |
|
| 550 |
+
def has_multiple_dogs(image):
|
| 551 |
+
# 使用簡單的啟發式方法來檢查圖像是否可能包含多隻狗
|
| 552 |
# 這裡可以使用更複雜的方法,如特徵提取或輕量級模型
|
| 553 |
gray = image.convert('L')
|
| 554 |
edges = gray.filter(ImageFilter.FIND_EDGES)
|
| 555 |
+
edge_pixels = np.array(edges)
|
| 556 |
+
return np.sum(edge_pixels > 128) > image.width * image.height * 0.1 # 假設邊緣像素比例大於 10% 表示可能有多隻狗
|
| 557 |
|
| 558 |
async def process_single_dog(image):
|
| 559 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|