-
Notifications
You must be signed in to change notification settings - Fork 1
/
export_kitti.py
368 lines (313 loc) · 14.7 KB
/
export_kitti.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# based on
# https://github.com/nutonomy/nuscenes-devkit/blob/master/python-sdk/nuscenes/scripts/export_kitti.py
import os
from typing import List, Optional
import fire
import matplotlib.pyplot as plt
import numpy as np
from nuscenes.utils.data_classes import LidarPointCloud
from nuscenes.utils.geometry_utils import BoxVisibility, transform_matrix
from nuscenes.utils.kitti import KittiDB
from PIL import Image
from pyquaternion import Quaternion
from tqdm import tqdm
from custom_nuscenes import CustomNuScenes
from joblib import Parallel, parallel_backend, delayed
class KittiConverter:
def __init__(self,
lyft_dataroot: str = 'D:/__LYFT/v1.01-train/',
table_folder: str = 'v1.01-train',
nusc_kitti_dir: str = '~/nusc_kitti/training/',
lidar_name: str = 'LIDAR_TOP',
get_all_detections = False,
parallel_n_jobs = 4,
samples_count: Optional[int] = None,
):
"""
:param lyft_dataroot: Where lyft dataset stored (root dir).
:param table_folder: folder with tables (json files).
:param nusc_kitti_dir: Where to write the KITTI-style annotations.
:param lidar_name: Name of the lidar sensor.
Only one lidar allowed at this moment.
:param get_all_detections: If True, will write all
bboxes in PointCloud and use only FrontCamera.
:param parallel_n_jobs: Number of threads to parralel processing.
:param samples_count: Number of samples to convert.
"""
self.lyft_dataroot = lyft_dataroot
self.table_folder = table_folder
self.nusc_kitti_dir = os.path.expanduser(nusc_kitti_dir)
self.lidar_name = lidar_name
self.get_all_detections = get_all_detections
self.samples_count = samples_count
self.parallel_n_jobs = parallel_n_jobs
# Create nusc_kitti_dir.
if not os.path.isdir(self.nusc_kitti_dir):
os.makedirs(self.nusc_kitti_dir)
# Select subset of the data to look at.
self.nusc = CustomNuScenes(self.lyft_dataroot, self.table_folder)
def nuscenes_gt_to_kitti(self) -> None:
"""
Converts nuScenes GT annotations to KITTI format.
"""
self.kitti_to_nu_lidar = Quaternion(axis=(0, 0, 1), angle=np.pi / 2)
self.kitti_to_nu_lidar_inv = self.kitti_to_nu_lidar.inverse
# Get assignment of scenes to splits.
split_logs = [
self.nusc.get('log', scene['log_token'])['logfile']
for scene
in self.nusc.scene
]
if self.get_all_detections:
self.cams_to_see = ['CAM_FRONT']
else:
self.cams_to_see = [
'CAM_FRONT',
'CAM_FRONT_LEFT',
'CAM_FRONT_RIGHT',
'CAM_BACK',
'CAM_BACK_LEFT',
'CAM_BACK_RIGHT'
]
# Create output folders.
self.label_folder = os.path.join(self.nusc_kitti_dir, 'label_2')
self.calib_folder = os.path.join(self.nusc_kitti_dir, 'calib')
self.image_folder = os.path.join(self.nusc_kitti_dir, 'image_2')
self.lidar_folder = os.path.join(self.nusc_kitti_dir, 'velodyne')
for folder in [self.label_folder, self.calib_folder, self.image_folder, self.lidar_folder]:
if not os.path.isdir(folder):
os.makedirs(folder)
# Use only the samples from the current split.
sample_tokens = self._split_to_samples(split_logs)
if self.samples_count is not None:
sample_tokens = sample_tokens[:self.samples_count]
with parallel_backend('threading', n_jobs=self.parallel_n_jobs):
Parallel()(
delayed(self.process_token_to_kitti)(sample_token)
for sample_token
in tqdm(sample_tokens)
)
def process_token_to_kitti(self, sample_token: str) -> None:
# Get sample data.
sample = self.nusc.get('sample', sample_token)
sample_annotation_tokens = sample['anns']
lidar_token = sample['data'][self.lidar_name]
sd_record_lid = self.nusc.get('sample_data', lidar_token)
cs_record_lid = self.nusc.get(
'calibrated_sensor', sd_record_lid['calibrated_sensor_token']
)
for cam_name in self.cams_to_see:
cam_front_token = sample['data'][cam_name]
token_to_write = cam_front_token
# Retrieve sensor records.
sd_record_cam = self.nusc.get('sample_data', cam_front_token)
cs_record_cam = self.nusc.get(
'calibrated_sensor', sd_record_cam['calibrated_sensor_token'])
cam_height = sd_record_cam['height']
cam_width = sd_record_cam['width']
imsize = (cam_width, cam_height)
# Combine transformations and convert to KITTI format.
# Note: cam uses same conventions in KITTI and nuScenes.
lid_to_ego = transform_matrix(
cs_record_lid['translation'],
Quaternion(cs_record_lid['rotation']),
inverse=False
)
ego_to_cam = transform_matrix(
cs_record_cam['translation'],
Quaternion(cs_record_cam['rotation']),
inverse=True
)
velo_to_cam = np.dot(ego_to_cam, lid_to_ego)
# Convert from KITTI to nuScenes LIDAR coordinates, where we apply velo_to_cam.
velo_to_cam_kitti = np.dot(
velo_to_cam, self.kitti_to_nu_lidar.transformation_matrix)
# Currently not used.
imu_to_velo_kitti = np.zeros((3, 4)) # Dummy values.
r0_rect = Quaternion(axis=[1, 0, 0], angle=0) # Dummy values.
# Projection matrix.
p_left_kitti = np.zeros((3, 4))
# Cameras are always rectified.
p_left_kitti[:3, :3] = cs_record_cam['camera_intrinsic']
# Create KITTI style transforms.
velo_to_cam_rot = velo_to_cam_kitti[:3, :3]
velo_to_cam_trans = velo_to_cam_kitti[:3, 3]
# Check that the rotation has the same format as in KITTI.
assert (velo_to_cam_trans[1:3] < 0).all()
# Retrieve the token from the lidar.
# Note that this may be confusing as the filename of the camera will
# include the timestamp of the lidar,
# not the camera.
filename_cam_full = sd_record_cam['filename']
filename_lid_full = sd_record_lid['filename']
# Convert image (jpg to png).
src_im_path = os.path.join(
self.nusc.dataroot, filename_cam_full)
dst_im_path = os.path.join(
self.image_folder, token_to_write + '.png')
if not os.path.exists(dst_im_path):
im = Image.open(src_im_path)
im.save(dst_im_path, "PNG")
# Convert lidar.
# Note that we are only using a single sweep, instead of the commonly used n sweeps.
src_lid_path = os.path.join(
self.nusc.dataroot, filename_lid_full)
dst_lid_path = os.path.join(
self.lidar_folder, token_to_write + '.bin')
pcl = LidarPointCloud.from_file(src_lid_path)
# In KITTI lidar frame.
pcl.rotate(self.kitti_to_nu_lidar_inv.rotation_matrix)
with open(dst_lid_path, "w") as lid_file:
pcl.points.T.tofile(lid_file)
# Add to tokens.
# tokens.append(token_to_write)
# Create calibration file.
kitti_transforms = dict()
kitti_transforms['P0'] = np.zeros((3, 4)) # Dummy values.
kitti_transforms['P1'] = np.zeros((3, 4)) # Dummy values.
kitti_transforms['P2'] = p_left_kitti # Left camera transform.
kitti_transforms['P3'] = np.zeros((3, 4)) # Dummy values.
# Cameras are already rectified.
kitti_transforms['R0_rect'] = r0_rect.rotation_matrix
kitti_transforms['Tr_velo_to_cam'] = np.hstack(
(velo_to_cam_rot, velo_to_cam_trans.reshape(3, 1)))
kitti_transforms['Tr_imu_to_velo'] = imu_to_velo_kitti
calib_path = os.path.join(
self.calib_folder, token_to_write + '.txt')
with open(calib_path, "w") as calib_file:
for (key, val) in kitti_transforms.items():
val = val.flatten()
val_str = '%.12e' % val[0]
for v in val[1:]:
val_str += ' %.12e' % v
calib_file.write('%s: %s\n' % (key, val_str))
# Write label file.
label_path = os.path.join(
self.label_folder, token_to_write + '.txt')
if os.path.exists(label_path):
print('Skipping existing file: %s' % label_path)
continue
# else:
# print('Writing file: %s' % label_path)
with open(label_path, "w") as label_file:
for sample_annotation_token in sample_annotation_tokens:
sample_annotation = self.nusc.get(
'sample_annotation', sample_annotation_token)
# Get box in LIDAR frame.
_, box_lidar_nusc, _ = self.nusc.get_sample_data(
lidar_token,
box_vis_level=BoxVisibility.NONE,
selected_anntokens=[sample_annotation_token]
)
box_lidar_nusc = box_lidar_nusc[0]
# Truncated: Set all objects to 0 which means untruncated.
truncated = 0.0
# Occluded: Set all objects to full visibility as this information is
# not available in nuScenes.
occluded = 0
detection_name = sample_annotation['category_name']
# category_to_detection_name(sample_annotation['category_name'])
# Skip categories that are not part of the nuScenes detection challenge. - disabled
if detection_name is None:
continue
# Convert from nuScenes to KITTI box format.
box_cam_kitti = KittiDB.box_nuscenes_to_kitti(
box_lidar_nusc,
Quaternion(matrix=velo_to_cam_rot),
velo_to_cam_trans, r0_rect
)
# Project 3d box to 2d box in image, ignore box if it does not fall inside.
bbox_2d = KittiDB.project_kitti_box_to_image(
box_cam_kitti, p_left_kitti, imsize=imsize)
if bbox_2d is None and not self.get_all_detections:
continue
elif bbox_2d is None and self.get_all_detections:
bbox_2d = (-1.0, -1.0, -1.0, -1.0) # default KITTI bbox
# Set dummy score so we can use this file as result.
box_cam_kitti.score = 0
# Convert box to output string format.
output = KittiDB.box_to_string(
name=detection_name,
box=box_cam_kitti,
bbox_2d=bbox_2d,
truncation=truncated,
occlusion=occluded
)
# Write to disk.
label_file.write(output + '\n')
def render_kitti(self, render_2d: bool = False) -> None:
"""
Renders the annotations in the KITTI dataset from a lidar and a camera view.
:param render_2d: Whether to render 2d boxes (only works for camera data).
"""
if render_2d:
print('Rendering 2d boxes from KITTI format')
else:
print('Rendering 3d boxes projected from 3d KITTI format')
# Load the KITTI dataset.
kitti = KittiDB(root=self.nusc_kitti_dir, splits=[''])
def get_transforms(token: str, root: str) -> dict:
calib_filename = KittiDB.get_filepath(token, 'calib', root=root)
lines = [line.rstrip() for line in open(calib_filename)]
velo_to_cam = np.array(
lines[5].strip().split(' ')[1:],
dtype=np.float64
)
velo_to_cam.resize((3, 4))
r0_rect = np.array(
lines[4].strip().split(' ')[1:],
dtype=np.float64
)
r0_rect.resize((3, 3))
p_left = np.array(
lines[2].strip().split(' ')[1:],
dtype=np.float64
)
p_left.resize((3, 4))
# Merge rectification and projection into one matrix.
p_combined = np.eye(4)
p_combined[:3, :3] = r0_rect
p_combined = np.dot(p_left, p_combined)
return {
'velo_to_cam': {
'R': velo_to_cam[:, :3],
'T': velo_to_cam[:, 3]
},
'r0_rect': r0_rect,
'p_left': p_left,
'p_combined': p_combined,
}
kitti.get_transforms = get_transforms # monkeypatching np.float32 -> 64
# Create output folder.
render_dir = os.path.join(self.nusc_kitti_dir, 'render')
if not os.path.isdir(render_dir):
os.mkdir(render_dir)
# Render each image.
tokens = kitti.tokens
if self.samples_count is not None:
tokens = tokens[:self.samples_count]
# currently supports only single thread processing
for token in tqdm(tokens):
for sensor in ['lidar', 'camera']:
out_path = os.path.join(
render_dir, '%s_%s.png' % (token, sensor))
kitti.render_sample_data(
token, sensor_modality=sensor, out_path=out_path, render_2d=render_2d)
# Close the windows to avoid a warning of too many open windows.
plt.close()
def _split_to_samples(self, split_logs: List[str]) -> List[str]:
"""
Convenience function to get the samples in a particular split.
:param split_logs: A list of the log names in this split.
:return: The list of samples.
"""
samples = []
for sample in self.nusc.sample:
scene = self.nusc.get('scene', sample['scene_token'])
log = self.nusc.get('log', scene['log_token'])
logfile = log['logfile']
if logfile in split_logs:
samples.append(sample['token'])
return samples
if __name__ == '__main__':
fire.Fire(KittiConverter)