-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkitti_data_convert_numpy_to_panorama.py
194 lines (144 loc) · 5.35 KB
/
kitti_data_convert_numpy_to_panorama.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import numpy as np
import os
import time
from multiprocessing import Pool
from multiprocessing import Process
lidar_dir = './extract_kiti/'
gt_box_dir = './newt/'
def box_encoder(point, boxes):
'''
'''
box_num = in_which_box(point, boxes)
# print(box_num)
if box_num == 0:
return np.zeros(8)
box = boxes[box_num - 1]
# print(box.shape)
theta = np.arctan2(-point[1], point[0])
# print(theta*180/np.pi)
# phi = -np.arctan2(point[2], np.sqrt(point[0]**2 + point[1]**2) )
u0 = point[:3] - box[0]
ru0 = rotation(-theta, u0)
u6 = point[:3] - box[6]
ru6 = rotation(-theta, u6)
x = np.sqrt(np.sum(np.square(box[1, :2] - box[2, :2])))
z = np.sqrt(np.sum(np.square(box[0, :2] - box[2, :2])))
phi = np.arcsin(x / z)
return np.array([1, ru0[0], ru0[1], ru0[2], ru6[0], ru6[1], ru6[2], phi])
def rotation(theta, point):
v = np.sin(theta)
u = np.cos(theta)
out = np.copy(point)
out[0] = u * point[0] + v * point[1]
out[1] = -v * point[0] + u * point[1]
return out
def is_in_box(point, box):
'''
point: tuple (x,y,z) coordinate
box: numpy array of shape (8,3)
return: True or False
'''
low = np.min(box[:, 2])
high = np.max(box[:, 2])
if (point[2] >= high) or (point[2] <= low):
return False
v = point[:2] - box[0, :2]
v1 = box[1, :2] - box[0, :2]
v2 = box[3, :2] - box[0, :2]
det1 = v[0] * v2[1] - v[1] * v2[0]
if det1 == 0:
return False
det2 = v[0] * v1[1] - v[1] * v1[0]
if det2 == 0:
return False
t1 = (v1[0] * v2[1] - v1[1] * v2[0]) / det1
s1 = (v1[0] * v[1] - v1[1] * v[0]) / det1
if (t1 <= 1) or (s1 <= 0):
return False
t2 = (v2[0] * v1[1] - v2[1] * v1[0]) / det2
s2 = (v2[0] * v[1] - v2[1] * v[0]) / det2
if (t2 <= 1) or (s2 <= 0):
return False
return True
def in_which_box(point, boxes):
'''
return in which box the given point belongs to, return 0 if the point doesn't belong to any boxes
'''
for i in range(len(boxes)):
if is_in_box(point, boxes[i]):
return i + 1
return 0
def cylindrical_projection_for_training(lidar, gt_box3d, ver_fov=(-24.4, 2.), hor_fov=(-47., 47.), v_res=0.42,
h_res=0.33):
'''
lidar: a numpy array of shape N*D, D>=3
gt_box3d: Ground truth boxes of shape B*8*3 (B : number of boxes)
ver_fov : angle range of vertical projection in degree
hor_fov: angle range of horizantal projection in degree
v_res : vertical resolusion
h_res : horizontal resolution
return : cylindrical projection (or panorama view) of lidar
'''
x = lidar[:, 0]
y = lidar[:, 1]
z = lidar[:, 2]
d = np.sqrt(np.square(x) + np.square(y))
theta = np.arctan2(-y, x)
phi = -np.arctan2(z, d)
x_view = np.int16(np.ceil((theta * 180 / np.pi - hor_fov[0]) / h_res))
y_view = np.int16(np.ceil((phi * 180 / np.pi + ver_fov[1]) / v_res))
x_max = np.int16(np.ceil((hor_fov[1] - hor_fov[0]) / h_res))
y_max = 63
indices = np.logical_and(np.logical_and(x_view >= 0, x_view <= x_max),
np.logical_and(y_view >= 0, y_view <= y_max))
x_view = x_view[indices]
y_view = y_view[indices]
z = z[indices]
d = d[indices]
d_z = [[d[i], z[i]] for i in range(len(d))]
view = np.zeros([y_max + 1, x_max + 1, 10], dtype=np.float32)
view[y_view, x_view, :2] = d_z
encode_boxes = np.array([box_encoder(lidar[i], gt_box3d) for i in range(len(lidar))])
encode_boxes = encode_boxes[indices]
# box = np.zeros([y_max+1, x_max+1, 8],dtype=np.float32)
view[y_view, x_view, 2:] = encode_boxes
return view
def list_of_paths(lidar_dir, gt_box_dir):
list_of_lidar = []
list_of_gtbox = []
list_of_view = []
for f in os.listdir(lidar_dir):
lidar_path = os.path.join(lidar_dir, f, 'lidar')
gtbox_path = os.path.join(gt_box_dir, f, 'gt_boxes3d')
# lidar_path = os.path.join(path, 'lidar')
# gtbox_path = os.path.join(path, 'gt_boxes3d')
view_path = os.path.join(lidar_dir, f, 'view')
if not os.path.exists(view_path):
os.makedirs(view_path)
num_files = len(os.listdir(lidar_path))
lidar = [os.path.join(lidar_path, 'lidar_' + str(i) + '.npy') for i in range(num_files)]
gtbox = [os.path.join(gtbox_path, 'gt_boxes3d_' + str(i) + '.npy') for i in range(num_files)]
view = [os.path.join(view_path, 'view_' + str(i) + '.npy') for i in range(num_files)]
list_of_lidar += lidar
list_of_gtbox += gtbox
list_of_view += view
return list_of_lidar, list_of_gtbox, list_of_view
list_of_lidar, list_of_gtbox, list_of_view = list_of_paths(lidar_dir, gt_box_dir)
def convert(i):
lidar = np.load(list_of_lidar[i])
gt_box = np.load(list_of_gtbox[i])
view = cylindrical_projection_for_training(lidar, gt_box)
np.save(list_of_view[i], view)
return i
if __name__ == '__main__':
using_pool = True
start = time.time()
if using_pool:
p = Pool(4)
p.map(convert, np.arange(len(list_of_lidar)))
else:
for i in range(len(list_of_lidar)):
convert(i)
#if len(list_of_lidar) % 20 == 0:
print('process : {0} / {1}'.format(i, len(list_of_lidar)))
print('Finish convert - total time = {0}'.format(time.time() - start))