forked from huahuasousou/dair_v2x_i_dataset_vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
until.py
397 lines (325 loc) · 19.2 KB
/
until.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
import os, sys
import shutil
import scipy.io as scio
import sys
import yaml
import shutil
import numpy as np
import pandas as pd
from random import shuffle
import random
def read_config(config_path='./config/config.yaml'):
file = open(config_path, 'r', encoding="utf-8")
#读取文件中的所有数据
file_data = file.read()
file.close()
#指定Loader
config_data = yaml.load(file_data,Loader=yaml.FullLoader)
return config_data
class convert_datasets:
def __init__(self,config_data):
np.set_printoptions(linewidth=1000)#防止numpy转换str自动换行
self.config_data=config_data
self.output_path = os.path.join(config_data['output_path'],config_data['output_floder_name'])
#self.data_info_path=os.path.join(config_data['root_path'],config_data['data_info'])
#self.data_info=read_config(data_info_path)
self.source_image_path=os.path.join(config_data['root_path'],config_data['image_floder'])
self.source_velodyne_path=os.path.join(config_data['root_path'],config_data['velodyne_floder'])
self.source_calib_path=os.path.join(config_data['root_path'],config_data['calib_floder'])
self.source_camera_Tr_path=os.path.join(config_data['root_path'],config_data['lidar_to_camera_floder'])
self.source_camera_label=os.path.join(config_data['root_path'],config_data['camera_label_floder'])
self.source_velodyne_label=os.path.join(config_data['root_path'],config_data['virtuallidar_label_floder'])
self.calib_path=os.path.join(self.output_path,"calib")
self.image2_path=os.path.join(self.output_path,"image_2")
self.label2_path=os.path.join(self.output_path,"label_2")
self.label_vel_path=os.path.join(self.output_path,"label_velodyne")
self.velodyne_path=os.path.join(self.output_path,"velodyne")
self.splitting_path=os.path.join(self.output_path,"splitting")
self.train_set_proportion=config_data['train_set_proportion']
self.validation_set_proportion=config_data['validation_set_proportion']
self.test_set_proportion=config_data['test_set_proportion']
self.random_seed=config_data['random_seed']
self.onlyFileName=config_data['onlyFileName']
self.create_floders_list=[]
self.create_floders_list.append(self.calib_path)
#self.create_floders_list.append(self.image2_path)
self.create_floders_list.append(self.label2_path)
self.create_floders_list.append(self.label_vel_path)
self.create_floders_list.append(self.splitting_path)
#self.create_floders_list.append(self.velodyne_path)
#print("create_floders list:",self.create_floders_list)
#self.files_list = os.listdir(self.source_image_path)
#self.files_list.sort()
#self.files_len=len(self.files_list)
self.car_count=0
self.van_count=0
#创建目录,返回路径
def create_path(self):
for floder in self.create_floders_list:
if not os.path.exists(floder):
os.makedirs(floder)
print("created the path:",floder)
def check_files(self):
#check image, velodyne and label are correspond.
pass
def calib_convert(self):
output_path=self.calib_path
calib_input_path=self.source_calib_path
tr_input_path=self.source_camera_Tr_path
self.calib_list = os.listdir(self.source_calib_path)
self.calib_list.sort() #no neceressory, save time
for index in self.calib_list:
calib=read_config(os.path.join(calib_input_path,index))
tr=read_config(os.path.join(tr_input_path,index))
rotation_matrix=np.mat(tr["rotation"])
translation_matrix=np.mat(tr["translation"])
Tr_matrix=np.c_[rotation_matrix,translation_matrix]
Tr_matrix=Tr_matrix.flatten()
Tr_matrix=str(Tr_matrix)
Tr_matrix=Tr_matrix.strip("[]")#delete "[" and "]"
Tr_matrix=Tr_matrix.replace(" "," ")# replace the "," to " "
#matrix="P2: "+matrix
Tr_matrix="Tr_velo_to_cam: "+Tr_matrix
#print(index,Tr_matrix)
#P2_matrix=str(calib["P"])#not P matrix
P2_matrix=str(calib["cam_K"])# should use cam_K matrix
P2_matrix=P2_matrix.strip("[]")
P2_matrix=P2_matrix.replace(",","")# replace the "," to " "
P2_matrix="P2: "+P2_matrix
output_name = os.path.splitext(index)
with open(os.path.join(output_path,output_name[0]+".txt"), 'wt') as f:
print("write file:",os.path.join(output_path,output_name[0]+".txt"))
f.write("P0: \n")
f.write("P1: \n")
f.write(P2_matrix+"\n")
f.write("P3: \n")
f.write("R0_rect: \n")
f.write(Tr_matrix)
def label_convert(self,input_path,output_path):
delete_class=['Trafficcone','Tricyclists','Barrowlists']#Set the list you want to delete
merge_class={'Car':['Van']}#Set the list you want to merge,example 'a':[1, 2, 3],'b':[13, 22, 32],'c':[15, 'sdf']
files_list = os.listdir(input_path)
files_list.sort()
#print(files_list)
for index in files_list:
label=read_config(os.path.join(input_path,index))
output_name = os.path.splitext(index)
with open(os.path.join(output_path,output_name[0]+".txt"), 'wt') as f:
print("write file:",os.path.join(output_path,output_name[0]+".txt"))
for line in label:
#print("line: ",line,"\n")
f.write(line['type']+" ")
f.write(line['truncated_state']+" ")
f.write(line['occluded_state']+" ")
f.write(line['alpha']+" ")
f.write(line['2d_box']['xmin']+" ")
f.write(line['2d_box']['ymin']+" ")
f.write(line['2d_box']['xmax']+" ")
f.write(line['2d_box']['ymax']+" ")
f.write(line['3d_dimensions']['h']+" ")
f.write(line['3d_dimensions']['w']+" ")
f.write(line['3d_dimensions']['l']+" ")
f.write(line['3d_location']['x']+" ")
f.write(line['3d_location']['y']+" ")
f.write(line['3d_location']['z']+" ")
f.write(line['rotation']+" "+"\n")
def label_convert_fix_high(self,camera_label_path,lidar_label_path,output_path):
delete_class=['Trafficcone','Tricyclists','Barrowlists']#Set the list you want to delete
merge_class={'Car':['Van']}#Set the list you want to merge,example 'a':[1, 2, 3],'b':[13, 22, 32],'c':[15, 'sdf']
'''
for key,value in merge_class.items():
print(key,value)
'''
lidar_files_list = os.listdir(lidar_label_path)
lidar_files_list.sort()
camera_files_list = os.listdir(camera_label_path)
camera_files_list.sort()
#print(files_list)
self.car_count=0
self.van_count=0
for index in lidar_files_list:
lidar_label=read_config(os.path.join(lidar_label_path,index))
camera_label=read_config(os.path.join(camera_label_path,index))
output_name = os.path.splitext(index)
with open(os.path.join(output_path,output_name[0]+".txt"), 'wt') as f:
print("write file:",os.path.join(output_path,output_name[0]+".txt"))
write_buffer=""
for camera_line,lidar_line in zip(camera_label,lidar_label):
if camera_line['type'] in delete_class:
#print("Skip the class",camera_line['type'])
pass
else:
for key,value in merge_class.items():
if camera_line['type'] in value:
#print("replace the class",camera_line['type'], "to",key)
camera_line['type']=key
if camera_line['type']=="Car":
self.car_count=self.car_count+1
elif camera_line['type']=="Van":
self.van_count=self.van_count+1
write_buffer+=camera_line['type']+" "
write_buffer+=camera_line['truncated_state']+" "
write_buffer+=camera_line['occluded_state']+" "
write_buffer+=camera_line['alpha']+" "
write_buffer+=camera_line['2d_box']['xmin']+" "
write_buffer+=camera_line['2d_box']['ymin']+" "
write_buffer+=camera_line['2d_box']['xmax']+" "
write_buffer+=camera_line['2d_box']['ymax']+" "
write_buffer+=lidar_line['3d_dimensions']['h']+" "
write_buffer+=lidar_line['3d_dimensions']['w']+" "
write_buffer+=lidar_line['3d_dimensions']['l']+" "
write_buffer+=camera_line['3d_location']['x']+" "
write_buffer+=camera_line['3d_location']['y']+" "
write_buffer+=lidar_line['3d_location']['z']+" "
write_buffer+=camera_line['rotation']+" "+"\n"
#print("write_buffer:",write_buffer)
f.write(write_buffer)
print("car_count",self.car_count)
print("van_count",self.van_count)
def rename_floder(self,input_path,output_path):
print("copy file from ",input_path,"to the ",output_path)
shutil.copytree(input_path,output_path)
def creat_list(self):
random_seed=self.random_seed
onlyFileName=self.onlyFileName
random.seed(int(random_seed))
r=random.random()
output_path=self.splitting_path
print("datasets splitting output path: ",output_path)
camera_files_list = os.listdir(self.image2_path)
#random.shuffle(camera_files_list,random=r)#打乱list
shuffle(camera_files_list)#打乱list
#camera_files_list.sort()
#print("camera_list: ",camera_files_list)
datasets_size=len(camera_files_list)
print("datasets_size: ",len(camera_files_list))
train_sets=camera_files_list[0:int(self.train_set_proportion*datasets_size)]
val_sets=camera_files_list[int(self.train_set_proportion*datasets_size):int(self.train_set_proportion*datasets_size)+int(self.validation_set_proportion*datasets_size)]
test_sets=camera_files_list[int(self.train_set_proportion*datasets_size)+int(self.validation_set_proportion*datasets_size):int(self.train_set_proportion*datasets_size)+int(self.validation_set_proportion*datasets_size)+int(self.test_set_proportion*datasets_size)+1]
"""
print("train_sets:",train_sets)
print("val_sets:",val_sets)
print("test_sets:",test_sets)
"""
train_output_path=os.path.join(output_path,"train.txt")
val_output_path=os.path.join(output_path,"val.txt")
test_output_path=os.path.join(output_path,"test.txt")
with open(train_output_path, 'wt') as f:
print("write file:",train_output_path)
for index in train_sets:
if onlyFileName== True:
image_name=index
lidar_name=os.path.splitext(index)[0]+".pcd"
cam_label_name=os.path.splitext(index)[0]+".txt"
vel_label_name=os.path.splitext(index)[0]+".txt"
write_col=lidar_name+" "+vel_label_name+" "+image_name+" "+cam_label_name+"\n"
else:
image_name=os.path.join(self.image2_path,index)
lidar_name=os.path.join(self.velodyne_path,os.path.splitext(index)[0]+".pcd")
cam_label_name=os.path.join(self.label2_path,os.path.splitext(index)[0]+".txt")
vel_label_name=os.path.join(self.label_vel_path,os.path.splitext(index)[0]+".txt")
write_col=lidar_name+" "+vel_label_name+" "+image_name+" "+cam_label_name+"\n"
#print(write_col)
f.write(write_col)
with open(val_output_path, 'wt') as f:
print("write file:",val_output_path)
for index in val_sets:
if onlyFileName== True:
image_name=index
lidar_name=os.path.splitext(index)[0]+".pcd"
cam_label_name=os.path.splitext(index)[0]+".txt"
vel_label_name=os.path.splitext(index)[0]+".txt"
write_col=lidar_name+" "+vel_label_name+" "+image_name+" "+cam_label_name+"\n"
else:
image_name=os.path.join(self.image2_path,index)
lidar_name=os.path.join(self.velodyne_path,os.path.splitext(index)[0]+".pcd")
cam_label_name=os.path.join(self.label2_path,os.path.splitext(index)[0]+".txt")
vel_label_name=os.path.join(self.label_vel_path,os.path.splitext(index)[0]+".txt")
write_col=lidar_name+" "+vel_label_name+" "+image_name+" "+cam_label_name+"\n"
#print(write_col)
f.write(write_col)
with open(test_output_path, 'wt') as f:
print("write file:",test_output_path)
for index in test_sets:
if onlyFileName== True:
image_name=index
lidar_name=os.path.splitext(index)[0]+".pcd"
cam_label_name=os.path.splitext(index)[0]+".txt"
vel_label_name=os.path.splitext(index)[0]+".txt"
write_col=lidar_name+" "+vel_label_name+" "+image_name+" "+cam_label_name+"\n"
else:
image_name=os.path.join(self.image2_path,index)
lidar_name=os.path.join(self.velodyne_path,os.path.splitext(index)[0]+".pcd")
cam_label_name=os.path.join(self.label2_path,os.path.splitext(index)[0]+".txt")
vel_label_name=os.path.join(self.label_vel_path,os.path.splitext(index)[0]+".txt")
write_col=lidar_name+" "+vel_label_name+" "+image_name+" "+cam_label_name+"\n"
#print(write_col)
f.write(write_col)
def creat_image_list(self):
random_seed=self.random_seed
onlyFileName=self.onlyFileName
random.seed(int(random_seed))
r=random.random()
output_path=self.splitting_path
print("datasets splitting output path: ",output_path)
camera_files_list = os.listdir(self.image2_path)
#random.shuffle(camera_files_list,random=r)#打乱list
shuffle(camera_files_list)#打乱list
#camera_files_list.sort()
#print("camera_list: ",camera_files_list)
datasets_size=len(camera_files_list)
print("datasets_size: ",len(camera_files_list))
train_sets=camera_files_list[0:int(self.train_set_proportion*datasets_size)]
val_sets=camera_files_list[int(self.train_set_proportion*datasets_size):int(self.train_set_proportion*datasets_size)+int(self.validation_set_proportion*datasets_size)]
test_sets=camera_files_list[int(self.train_set_proportion*datasets_size)+int(self.validation_set_proportion*datasets_size):int(self.train_set_proportion*datasets_size)+int(self.validation_set_proportion*datasets_size)+int(self.test_set_proportion*datasets_size)+1]
"""
print("train_sets:",train_sets)
print("val_sets:",val_sets)
print("test_sets:",test_sets)
"""
train_output_path=os.path.join(output_path,"train.txt")
val_output_path=os.path.join(output_path,"val.txt")
test_output_path=os.path.join(output_path,"test.txt")
with open(train_output_path, 'wt') as f:
print("write file:",train_output_path)
for index in train_sets:
image_name=os.path.join(self.config_data['image_list_define_path'],index)
cam_label_name=os.path.join(self.config_data['image_label_list_path'],os.path.splitext(index)[0]+".txt")
write_col=image_name+" "+cam_label_name+"\n"
print(write_col)
f.write(write_col)
with open(val_output_path, 'wt') as f:
print("write file:",val_output_path)
for index in val_sets:
image_name=os.path.join(self.config_data['image_list_define_path'],index)
cam_label_name=os.path.join(self.config_data['image_label_list_path'],os.path.splitext(index)[0]+".txt")
write_col=image_name+" "+cam_label_name+"\n"
print(write_col)
f.write(write_col)
with open(test_output_path, 'wt') as f:
print("write file:",test_output_path)
for index in test_sets:
image_name=os.path.join(self.config_data['image_list_define_path'],index)
cam_label_name=os.path.join(self.config_data['image_label_list_path'],os.path.splitext(index)[0]+".txt")
write_col=image_name+" "+cam_label_name+"\n"
print(write_col)
f.write(write_col)
if __name__ == '__main__':
#You can select which folders you need to convert by commenting out parts of the code
config_data=read_config('./config/config.yaml') #read config file get the convert input and output path
dair2kitti=convert_datasets(config_data) #instantiate the object
dair2kitti.create_path() #create the subfloders
dair2kitti.calib_convert() #calib file convert to the kitti format
#camera coordinate label convert, but origin camera label have some issues, so don't use this.
#dair2kitti.label_convert(dair2kitti.source_camera_label,dair2kitti.label2_path)
#camera coordinate label convert, and fix the camera label issues
#base the camera label source, and replace the pos(z) and the size(l,w,h) by the lidar label source.
dair2kitti.label_convert_fix_high(dair2kitti.source_camera_label,dair2kitti.source_velodyne_label,dair2kitti.label2_path)
#velodyne coordinate label convert
dair2kitti.label_convert(dair2kitti.source_velodyne_label,dair2kitti.label_vel_path)
#Make sure the destination folder does not exist before converting
#copy and rename the camera image floder
dair2kitti.rename_floder(dair2kitti.source_image_path,dair2kitti.image2_path)
#copy and rename the velodyne pcd floder
dair2kitti.rename_floder(dair2kitti.source_velodyne_path,dair2kitti.velodyne_path)
#convert finished