-
Notifications
You must be signed in to change notification settings - Fork 18
/
bbox.py
150 lines (132 loc) · 4.89 KB
/
bbox.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
""" The defination and basic methods of bbox
"""
import numpy as np
from copy import deepcopy
class BBox:
def __init__(self, x=None, y=None, z=None, h=None, w=None, l=None, o=None):
self.x = x # center x
self.y = y # center y
self.z = z # center z
self.h = h # height
self.w = w # width
self.l = l # length
self.o = o # orientation
self.s = None # detection score
def __str__(self):
return 'x: {}, y: {}, z: {}, heading: {}, length: {}, width: {}, height: {}, score: {}'.format(
self.x, self.y, self.z, self.o, self.l, self.w, self.h, self.s)
@classmethod
def bbox2dict(cls, bbox):
return {
'center_x': bbox.x, 'center_y': bbox.y, 'center_z': bbox.z,
'height': bbox.h, 'width': bbox.w, 'length': bbox.l, 'heading': bbox.o}
@classmethod
def bbox2array(cls, bbox):
if bbox.s is None:
return np.array([bbox.x, bbox.y, bbox.z, bbox.o, bbox.l, bbox.w, bbox.h])
else:
return np.array([bbox.x, bbox.y, bbox.z, bbox.o, bbox.l, bbox.w, bbox.h, bbox.s])
@classmethod
def array2bbox(cls, data):
bbox = BBox()
bbox.x, bbox.y, bbox.z, bbox.o, bbox.l, bbox.w, bbox.h = data[:7]
if len(data) == 8:
bbox.s = data[-1]
return bbox
@classmethod
def dict2bbox(cls, data):
bbox = BBox()
bbox.x = data['center_x']
bbox.y = data['center_y']
bbox.z = data['center_z']
bbox.h = data['height']
bbox.w = data['width']
bbox.l = data['length']
bbox.o = data['heading']
if 'score' in data.keys():
bbox.s = data['score']
return bbox
@classmethod
def copy_bbox(cls, bboxa, bboxb):
bboxa.x = bboxb.x
bboxa.y = bboxb.y
bboxa.z = bboxb.z
bboxa.l = bboxb.l
bboxa.w = bboxb.w
bboxa.h = bboxb.h
bboxa.o = bboxb.o
bboxa.s = bboxb.s
return
@classmethod
def box2corners2d(cls, bbox):
""" the coordinates for bottom corners
"""
bottom_center = np.array([bbox.x, bbox.y, bbox.z - bbox.h / 2])
cos, sin = np.cos(bbox.o), np.sin(bbox.o)
pc0 = np.array([bbox.x + cos * bbox.l / 2 + sin * bbox.w / 2,
bbox.y + sin * bbox.l / 2 - cos * bbox.w / 2,
bbox.z - bbox.h / 2])
pc1 = np.array([bbox.x + cos * bbox.l / 2 - sin * bbox.w / 2,
bbox.y + sin * bbox.l / 2 + cos * bbox.w / 2,
bbox.z - bbox.h / 2])
pc2 = 2 * bottom_center - pc0
pc3 = 2 * bottom_center - pc1
return [pc0.tolist(), pc1.tolist(), pc2.tolist(), pc3.tolist()]
@classmethod
def box2corners3d(cls, bbox):
""" the coordinates for bottom corners
"""
center = np.array([bbox.x, bbox.y, bbox.z])
bottom_corners = np.array(BBox.box2corners2d(bbox))
up_corners = 2 * center - bottom_corners
corners = np.concatenate([up_corners, bottom_corners], axis=0)
return corners.tolist()
@classmethod
def motion2bbox(cls, bbox, motion):
result = deepcopy(bbox)
result.x += motion[0]
result.y += motion[1]
result.z += motion[2]
result.o += motion[3]
return result
@classmethod
def set_bbox_size(cls, bbox, size_array):
result = deepcopy(bbox)
result.l, result.w, result.h = size_array
return result
@classmethod
def set_bbox_with_states(cls, prev_bbox, state_array):
prev_array = BBox.bbox2array(prev_bbox)
prev_array[:4] += state_array[:4]
prev_array[4:] = state_array[4:]
bbox = BBox.array2bbox(prev_array)
return bbox
@classmethod
def box_pts2world(cls, ego_matrix, pcs):
new_pcs = np.concatenate((pcs,
np.ones(pcs.shape[0])[:, np.newaxis]),
axis=1)
new_pcs = ego_matrix @ new_pcs.T
new_pcs = new_pcs.T[:, :3]
return new_pcs
@classmethod
def edge2yaw(cls, center, edge):
vec = edge - center
yaw = np.arccos(vec[0] / np.linalg.norm(vec))
if vec[1] < 0:
yaw = -yaw
return yaw
@classmethod
def bbox2world(cls, ego_matrix, box):
# center and corners
corners = np.array(BBox.box2corners2d(box))
center = BBox.bbox2array(box)[:3][np.newaxis, :]
center = BBox.box_pts2world(ego_matrix, center)[0]
corners = BBox.box_pts2world(ego_matrix, corners)
# heading
edge_mid_point = (corners[0] + corners[1]) / 2
yaw = BBox.edge2yaw(center[:2], edge_mid_point[:2])
result = deepcopy(box)
result.x, result.y, result.z = center
result.o = yaw
return result