-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
103 lines (81 loc) · 2.63 KB
/
utils.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import cv2
def xyxy_to_xywh(xyxy):
center_x = (xyxy[0] + xyxy[2]) / 2
center_y = (xyxy[1] + xyxy[3]) / 2
w = xyxy[2] - xyxy[0]
h = xyxy[3] - xyxy[1]
return (center_x, center_y, w, h)
def plot_one_box(xyxy, img, color=(0, 200, 0), target=False):
xy1 = (int(xyxy[0]), int(xyxy[1]))
xy2 = (int(xyxy[2]), int(xyxy[3]))
if target:
color = (0, 0, 255)
cv2.rectangle(img, xy1, xy2, color, 1, cv2.LINE_AA) # filled
def updata_trace_list(box_center, trace_list, max_list_len=50):
if len(trace_list) <= max_list_len:
trace_list.append(box_center)
else:
trace_list.pop(0)
trace_list.append(box_center)
return trace_list
def draw_trace(img, trace_list):
"""
更新trace_list,绘制trace
:param trace_list:
:param max_list_len:
:return:
"""
for i, item in enumerate(trace_list):
if i < 1:
continue
cv2.line(img,
(trace_list[i][0], trace_list[i][1]), (trace_list[i - 1][0], trace_list[i - 1][1]),
(255, 255, 0), 3)
def cal_iou(box1, box2):
"""
:param box1: xyxy 左上右下
:param box2: xyxy
:return:
"""
x1min, y1min, x1max, y1max = box1[0], box1[1], box1[2], box1[3]
x2min, y2min, x2max, y2max = box2[0], box2[1], box2[2], box2[3]
# 计算两个框的面积
s1 = (y1max - y1min + 1.) * (x1max - x1min + 1.)
s2 = (y2max - y2min + 1.) * (x2max - x2min + 1.)
# 计算相交部分的坐标
xmin = max(x1min, x2min)
ymin = max(y1min, y2min)
xmax = min(x1max, x2max)
ymax = min(y1max, y2max)
inter_h = max(ymax - ymin + 1, 0)
inter_w = max(xmax - xmin + 1, 0)
intersection = inter_h * inter_w
union = s1 + s2 - intersection
# 计算iou
iou = intersection / union
return iou
def cal_distance(box1, box2):
"""
计算两个box中心点的距离
:param box1: xyxy 左上右下
:param box2: xyxy
:return:
"""
center1 = ((box1[0] + box1[2]) // 2, (box1[1] + box1[3]) // 2)
center2 = ((box2[0] + box2[2]) // 2, (box2[1] + box2[3]) // 2)
dis = ((center1[0] - center2[0]) ** 2 + (center1[1] - center2[1]) ** 2) ** 0.5
return dis
def xywh_to_xyxy(xywh):
x1 = xywh[0] - xywh[2]//2
y1 = xywh[1] - xywh[3]//2
x2 = xywh[0] + xywh[2] // 2
y2 = xywh[1] + xywh[3] // 2
return [x1, y1, x2, y2]
if __name__ == "__main__":
box1 = [100, 100, 200, 200]
box2 = [100, 100, 200, 300]
iou = cal_iou(box1, box2)
print(iou)
box1.pop(0)
box1.append(555)
print(box1)