-
Notifications
You must be signed in to change notification settings - Fork 3
/
bbox3D_estimate.py
162 lines (127 loc) · 5.05 KB
/
bbox3D_estimate.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
151
152
153
154
155
156
157
158
159
160
161
162
import torch
import numpy as np
def dimensions_to_corners(dimensions):
"""
Note that the center of the 3D bounding box is the center of the bottom surface,
not the geometric center of the box, following KITTI's defination.
"""
h, w, l = dimensions[:, 0], dimensions[:, 1], dimensions[:, 2]
zeros = torch.zeros(dimensions.size(0)).to(dimensions.device)
corner_x = torch.stack([ l/2.0, -l/2.0, -l/2.0, l/2.0, l/2.0, -l/2.0, -l/2.0, l/2.0])
corner_y = torch.stack([ zeros, zeros, zeros, zeros, -h, -h, -h, -h])
corner_z = torch.stack([ w/2.0, w/2.0, -w/2.0, -w/2.0, w/2.0, w/2.0, -w/2.0, -w/2.0])
corners = torch.stack([corner_x, corner_y, corner_z]) # 3x8xN
return corners.permute(2, 1, 0).contiguous() # Nx8x3
def solve_3d_bbox_single(bbox2D, corners, theta_l, calib):
"""
Input:
bbox2D: Tensor(4), [x1, y1, x2, y2]
corners: Tensor(8, 3), aligned corners without rotation
theta_l: camera direction [-pi, pi]
calib: calibration metrices in KITTI
"""
x1, y1, x2, y2 = bbox2D
# useful calibrations
P2 = calib['P2']
R0_rect = torch.eye(4)
R0_rect[:3, :3] = calib['R0_rect']
K = torch.matmul(P2, R0_rect)
# use 2D bbox to estimate global rotation
theta_ray = torch.atan2(P2[0, 0], (x1 + x2) * 0.5 - P2[0, 2])
ry = np.pi - theta_ray - theta_l
Ry_T = torch.tensor([[ torch.cos(ry), 0.0, -torch.sin(ry)],
[ 0.0 , 1.0, 0.0 ],
[ torch.sin(ry), 0.0, torch.cos(ry)]])
corners = torch.matmul(corners, Ry_T) # rotated corners
# adjust front side
if theta_l >= np.pi / 2.0 and theta_l < np.pi:
corners = corners[[3, 0, 1, 2, 7, 4, 5, 6]].contiguous()
elif theta_l >= -np.pi and theta_l < -np.pi / 2.0:
corners = corners[[2, 3, 0, 1, 6, 7, 4, 5]].contiguous()
elif theta_l >= -np.pi / 2.0 and theta_l < 0.0:
corners = corners[[1, 2, 3, 0, 5, 6, 7, 4]].contiguous()
# start solve constrains
X = torch.eye(4)
A = torch.zeros(4, 3)
b = torch.zeros(4)
# prepare constrains
constrains = {}
# x1 -> 7, 6
constrains['x1'] = {}
for i in [7, 6]:
constrains['x1'][i] = {}
X[:3, 3] = corners[i]
K_X = torch.matmul(K, X)
constrains['x1'][i]['A'] = K_X[0, :3] - x1 * K_X[2, :3]
constrains['x1'][i]['b'] = x1 * K_X[2, 3] - K_X[0, 3]
# x2 -> 4, 7
constrains['x2'] = {}
for i in [4, 7]:
constrains['x2'][i] = {}
X[:3, 3] = corners[i]
K_X = torch.matmul(K, X)
constrains['x2'][i]['A'] = K_X[0, :3] - x2 * K_X[2, :3]
constrains['x2'][i]['b'] = x2 * K_X[2, 3] - K_X[0, 3]
# y1 -> 4, 5, 6, 7
constrains['y1'] = {}
for i in [4, 5, 6, 7]:
constrains['y1'][i] = {}
X[:3, 3] = corners[i]
K_X = torch.matmul(K, X)
constrains['y1'][i]['A'] = K_X[1, :3] - y1 * K_X[2, :3]
constrains['y1'][i]['b'] = y1 * K_X[2, 3] - K_X[1, 3]
# y2 -> 2, 3, 0
constrains['y2'] = {}
for i in [2, 3, 0]:
constrains['y2'][i] = {}
X[:3, 3] = corners[i]
K_X = torch.matmul(K, X)
constrains['y2'][i]['A'] = K_X[1, :3] - y2 * K_X[2, :3]
constrains['y2'][i]['b'] = y2 * K_X[2, 3] - K_X[1, 3]
# solving linear functions
error = float('inf')
# case 1: only see front side
A[0] = constrains['x1'][7]['A']
b[0] = constrains['x1'][7]['b']
A[1] = constrains['x2'][4]['A']
b[1] = constrains['x2'][4]['b']
for i in [3, 0]:
A[2] = constrains['y2'][i]['A']
b[2] = constrains['y2'][i]['b']
for j in [4, 5, 6, 7]:
A[3] = constrains['y1'][j]['A']
b[3] = constrains['y1'][j]['b']
trans_t = torch.matmul(torch.pinverse(A), b)
error_t = torch.norm(torch.matmul(A, trans_t) - b)
if error_t < error:
trans = trans_t
error = error_t
# case 2: see both front side and lateral side
A[0] = constrains['x1'][6]['A']
b[0] = constrains['x1'][6]['b']
for i in [2, 3, 0]:
A[2] = constrains['y2'][i]['A']
b[2] = constrains['y2'][i]['b']
for j in [4, 5, 6, 7]:
A[3] = constrains['y1'][j]['A']
b[3] = constrains['y1'][j]['b']
trans_t = torch.matmul(torch.pinverse(A), b)
error_t = torch.norm(torch.matmul(A, trans_t) - b)
if error_t < error:
trans = trans_t
error = error_t
# case 2: only see lateral side
A[1] = constrains['x2'][7]['A']
b[1] = constrains['x2'][7]['b']
for i in [2, 3]:
A[2] = constrains['y2'][i]['A']
b[2] = constrains['y2'][i]['b']
for j in [4, 5, 6, 7]:
A[3] = constrains['y1'][j]['A']
b[3] = constrains['y1'][j]['b']
trans_t = torch.matmul(torch.pinverse(A), b)
error_t = torch.norm(torch.matmul(A, trans_t) - b)
if error_t < error:
trans = trans_t
error = error_t
return trans