This repository has been archived by the owner on May 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
53 lines (36 loc) · 1.6 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from typing import List, Tuple
import numpy as np
# TODO: Add type hints
def iou(pred_box, target_box):
if len(target_box.shape) == 1:
target_box = target_box[np.newaxis, :]
xA = np.maximum(pred_box[0], target_box[:, 0])
yA = np.maximum(pred_box[1], target_box[:, 1])
xB = np.minimum(pred_box[2], target_box[:, 2])
yB = np.minimum(pred_box[3], target_box[:, 3])
intersection = np.maximum(0.0, xB - xA) * np.maximum(0.0, yB - yA)
boxAArea = (pred_box[2] - pred_box[0]) * (pred_box[3] - pred_box[1])
boxBArea = (target_box[:, 2] - target_box[:, 0]) * \
(target_box[:, 3] - target_box[:, 1])
scores = intersection / (boxAArea + boxBArea - intersection)
return scores
def nms(rect_list: List[Tuple[Tuple[int, int, int, int], float]],
threshhold: float) -> List[Tuple[Tuple[int, int, int, int], float]]:
nms_list: List[Tuple[Tuple[int, int, int, int], float]] = []
rect_array = np.array([rect[0] for rect in rect_list])
score_array = np.array([rect[1] for rect in rect_list])
idxs = np.argsort(score_array)[::-1]
rect_array = rect_array[idxs]
score_array = score_array[idxs]
while len(score_array) > 0:
nms_list.append((rect_array[0], score_array[0]))
rect_array = rect_array[1:]
score_array = score_array[1:]
length = len(score_array)
if length <= threshhold:
break
iou_scores = iou(np.array(nms_list[-1][0]), rect_array)
idxs = np.where(iou_scores < threshhold)[0]
rect_array = rect_array[idxs]
score_array = score_array[idxs]
return nms_list