-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_generate_syn_kitti_multi.py
200 lines (164 loc) · 7.54 KB
/
batch_generate_syn_kitti_multi.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
import os
import sys
import cv2
import numpy as np
import glob
from plyfile import PlyData, PlyElement
from util_pcds import *
import pickle
from utils_notf import *
from multiprocessing import Pool, TimeoutError
DEBUG = False
VIS = True
GENERATE_PLY = True
POOL_NUM = 8
MODE = 'validation'
if MODE == 'training':
from config import cfg
else:
from config_val import cfg
sample_ratios = [2, 4, 8]
KITTI_DATA_DIR = os.path.join('../../data/KITTI', MODE)
DATA_DIR = os.path.join('../../data/KITTI_syn_multi', MODE)
OBJ_DIR = '/home/ziyanw1/PCDs/02958343'
SCENE_DIR = os.path.join(KITTI_DATA_DIR, 'velodyne')
OBJ_OUT_DIR = os.path.join(DATA_DIR, 'template')
SCENE_OUT_DIR = os.path.join(DATA_DIR, 'syn_scene')
LABEL_OUT_DIR = os.path.join(DATA_DIR, 'syn_label')
VIS_OUT_DIR = os.path.join(DATA_DIR, 'vis')
META_OUT_DIR = os.path.join(DATA_DIR, 'meta')
if not os.path.exists(OBJ_OUT_DIR):
os.mkdir(OBJ_OUT_DIR)
if not os.path.exists(SCENE_OUT_DIR):
os.mkdir(SCENE_OUT_DIR)
if not os.path.exists(LABEL_OUT_DIR):
os.mkdir(LABEL_OUT_DIR)
if not os.path.exists(VIS_OUT_DIR):
os.mkdir(VIS_OUT_DIR)
if not os.path.exists(META_OUT_DIR):
os.mkdir(META_OUT_DIR)
def create_syn_scene_obj(scene_file_name, obj_file_name, generate_ply=GENERATE_PLY, vis=VIS):
## parse file
s_set = scene_file_name.split('/')
scene_name = s_set[-1][:-4]
s_set = obj_file_name.split('/')
obj_name = s_set[-1][:-4]
## load scene here
#scene_name = os.path.join(kitti_data_dir, scene_file)
scene_pc = np.fromfile(scene_file_name, dtype=np.float32).reshape(-1, 4)
scene_pc_pcd = np.concatenate((scene_pc[:, :3], np.zeros_like(scene_pc[:, :3])), axis=1)
scene_pc_pcd[:, 5] = 255 ## assign green for background
## create car point clouds
## sample azim here
azim = np.random.choice([0, 90, 180, 270])
azim += np.random.uniform(-5, 5)
R = azim2rot_deg(azim)
## sample translation here
scene_x_var = np.sqrt(np.var(scene_pc[:, 0]))
scene_y_var = np.sqrt(np.var(scene_pc[:, 1]))
t_obj = np.zeros((1,3), dtype=np.float32)
t_obj[0, 0] = np.random.uniform(scene_x_var, scene_x_var)
t_obj[0, 1] = np.random.uniform(scene_y_var, scene_y_var)
#t_obj[0, 2] -= 1 ## make the car on the ground
## load car here
ply_data = PlyData.read(obj_file_name)
pc_coord = []
pc_coord.append(ply_data.elements[0].data['x'])
pc_coord.append(ply_data.elements[0].data['z'])
pc_coord.append(ply_data.elements[0].data['y'])
pc_coord = np.squeeze(np.dstack(pc_coord))
template_coord = pc_coord.copy()
template_coord_pcd = np.concatenate((template_coord, np.zeros_like(template_coord)), axis=1)
template_coord = np.concatenate((template_coord, np.zeros((pc_coord.shape[0], 1))), axis=1)
pc_coord *= 4 ## scale to real car
## for 3D bbox
h = np.max(pc_coord[:, 0]) - np.min(pc_coord[:, 0])
w = np.max(pc_coord[:, 1]) - np.min(pc_coord[:, 1])
l = np.max(pc_coord[:, 2]) - np.min(pc_coord[:, 2])
## car in plane rotation happens here
## coordinates in order of [x, y, z] ??
new_pc_coord = np.transpose(np.matmul(R, np.transpose(pc_coord)))
pc_coord = new_pc_coord
## translation happens here
pc_coord = pc_coord + np.tile(t_obj, [pc_coord.shape[0], 1])
## down sampleing happens here
sample_ratio = np.random.choice(sample_ratios)
pc_coord = pc_coord[::sample_ratio, ...]
pc_coord_pcd = np.concatenate((pc_coord, np.zeros_like(pc_coord)), axis=1)
pc_coord_pcd[:, 4] = 255 ## assign red for car
pc_coord = np.concatenate((pc_coord, np.zeros((pc_coord.shape[0], 1))), axis=1)
## dump template model both .ply and .bin
obj_outfile_pcd = os.path.join(OBJ_OUT_DIR, scene_name+'.pcd')
pcd_from_array(obj_outfile_pcd, template_coord_pcd)
obj_outfile_bin = os.path.join(OBJ_OUT_DIR, scene_name+'.bin')
template_coord.astype(np.float32).tofile(obj_outfile_bin)
if generate_ply:
command = './pcl_pcd2ply {} {}'.format(obj_outfile_pcd, obj_outfile_pcd[:-4]+'.ply')
os.system(command)
## dump annotations: label
## x y z -> z y x in label
gt_bbox = [(np.min(pc_coord[:, 0])+np.max(pc_coord[:, 0]))/2.0, (np.min(pc_coord[:, 1])+np.max(pc_coord[:, 1]))/2.0 \
, (np.min(pc_coord[:, 2])+np.max(pc_coord[:, 2]))/2.0, h, w, l, np.deg2rad(azim)]
gt_bboxes = np.asarray([gt_bbox])
label_line = 'Car -1 -1 -10 -1 -1 -1 -1 {:.02f} {:.02f} {:.02f} {:.02f} {:.02f} {:.02f} {:.02f}\n'.format(
l, w, h, -gt_bbox[1], gt_bbox[2], gt_bbox[0], np.deg2rad(90-azim))
label_file_name = os.path.join(LABEL_OUT_DIR, scene_name+'.txt')
with open(label_file_name, 'w') as f:
f.writelines([label_line])
## create new pc file
vertex = np.concatenate((pc_coord_pcd, scene_pc_pcd), axis=0)
## dump scene with template both p.ly and .bin
scene_outfile_pcd = os.path.join(SCENE_OUT_DIR, scene_name+'.pcd')
pcd_from_array(scene_outfile_pcd, vertex)
scene_outfile_bin = os.path.join(SCENE_OUT_DIR, scene_name+'.bin')
scene_gen_pc = np.concatenate((pc_coord, scene_pc), axis=0)
scene_gen_pc.astype(np.float32).tofile(scene_outfile_bin)
if generate_ply:
command = './pcl_pcd2ply {} {}'.format(scene_outfile_pcd, scene_outfile_pcd[:-4]+'.ply')
os.system(command)
### for debug
#load_scene = np.fromfile(scene_outfile_bin, dtype=np.float32).reshape(-1, 4)
#err = np.abs(load_scene - scene_gen_pc)
#print(np.max(err))
#sys.exit()
## dumpe meta data: obj name, scene, postion rotation scale
meta_info = {}
meta_info['obj_name'] = obj_name
meta_info['scene_name'] = scene_name
meta_info['sample_ratio'] = sample_ratio
meta_info['azimuth'] = azim
meta_info['translation'] = t_obj
meta_file_name = os.path.join(META_OUT_DIR, scene_name+'.pkl')
with open(meta_file_name, 'wb') as f:
pickle.dump(meta_info, f)
if vis:
batch_gt_boxes3d = label_to_gt_box3d(
[[label_line]], cls='Car', coordinate='lidar')
P, Tr, R = load_calib( os.path.join( cfg.CALIB_DIR, scene_name + '.txt' ) )
bird_view = lidar_to_bird_view_img(scene_gen_pc, factor=cfg.BV_LOG_FACTOR)
bird_view = draw_lidar_box3d_on_birdview(bird_view, batch_gt_boxes3d[0], 1, batch_gt_boxes3d[0], factor=cfg.BV_LOG_FACTOR, P2=P, T_VELO_2_CAM=Tr, R_RECT_0=R)
bv_file_name = os.path.join(VIS_OUT_DIR, scene_name+'_bv.png')
cv2.imwrite(bv_file_name, bird_view)
if __name__ == "__main__":
if DEBUG:
## for debug
scene_list = ['001201.bin', '001830.bin', '002453.bin']
obj_list = ['5801f9eb726b56448b9c28e7b121fdbc_4096.ply']
scene_list = [os.path.join(DATA_DIR, 'velodyne', s) for s in scene_list]
obj_list = [os.path.join(OBJ_DIR, o) for o in obj_list]
for s in scene_list:
o = np.random.choice(obj_list)
create_syn_scene_obj(s, o, True, True)
else:
#pool = Pool(POOL_NUM)
f_scene = glob.glob(os.path.join(SCENE_DIR, '*.bin'))
f_obj = glob.glob(os.path.join(OBJ_DIR, '*_4096.ply'))
if MODE is 'training':
f_obj_use = np.random.choice(f_obj[:int(0.7*len(f_obj))], size=len(f_scene))
else:
f_obj_use = np.random.choice(f_obj[int(0.7*len(f_obj)):], size=len(f_scene))
#pool.imap(create_syn_scene_obj, f_scene, f_obj_use)
#pool.close()
for idx, (s_path, o_path) in enumerate(zip(f_scene, f_obj_use)):
create_syn_scene_obj(s_path, o_path)
print('------- Scene {} is generated -------'.format(idx))