forked from ArashJavan/veloparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlidar_manager.py
337 lines (276 loc) · 12.1 KB
/
lidar_manager.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
import os
from pathlib import Path
import dpkt
import datetime
import numpy as np
from tqdm import tqdm
from gps import GprmcMessage, utc_to_weekseconds
import lidar
class VelodyneManager():
def __init__(self, type, pcap_path, out_root, params):
self.pcap_path = Path(pcap_path)
self.params = params
self.lidar_type = type
self.lidar = None
self.out_root = out_root
self.pos_X = None
self.pos_Y = None
self.pos_Z = None
self.intensities = None
self.latitudes = None
self.timestamps = None
self.distances = None
self.indicies = None
self.longitudes = None
self.frame_nr = 0
self.cur_azimuth = None
self.last_azimuth = None
self.datetime = None
self.gps_fp = None
if "velodynevlp16" == type.lower():
self.lidar = lidar.VelodyneVLP16()
def get_pcap_length(self):
# open pcap file
try:
fpcap = open(self.pcap_path, 'rb')
lidar_reader = dpkt.pcap.Reader(fpcap)
except Exception as ex:
print(str(ex))
return 0
counter = 0
# itearte through each data packet and timestamps
for _, _ in enumerate(lidar_reader):
counter += 1
fpcap.close()
return counter
def run(self):
"""
Exteractis point clouds from pcap file
:return:
"""
pcap_len = self.get_pcap_length()
if pcap_len <= 0:
return
# open pcap file
try:
fpcap = open(self.pcap_path, 'rb')
self.lidar_reader = dpkt.pcap.Reader(fpcap)
except Exception as ex:
print(str(ex))
return
# create output folder hierarchy
if not self.create_folders():
return
# itearte through each data packet and timestamps
pbar = tqdm(total=pcap_len)
for idx, (ts, buf) in enumerate(self.lidar_reader):
if idx < self.params['from']:
continue
if 0 < self.params['to'] < idx:
break
self.datetime = datetime.datetime.utcfromtimestamp(ts)
eth = dpkt.ethernet.Ethernet(buf)
data = eth.data.data.data
# handle Position-Frame (GPS-Data)
if self.params['gps']:
if eth.data.data.sport == self.params['gps-port']:
self.process_gps_frame(data, ts, idx)
# Handle Data-Frame (Point clouds)
if eth.data.data.sport == self.params['data-port']:
self.process_data_frame(data, ts, idx)
pbar.update(1)
if self.gps_fp is not None:
self.gps_fp.close()
def process_data_frame(self, data, timestamp, index):
cur_X, cur_Y, cur_Z, cur_intensities, cur_latitudes, cur_timestamps, cur_distances = self.lidar.process_data_frame(data, index)
# number of sequences
n_seq = int(len(cur_X) / self.lidar.count_lasers)
cur_indicies = np.tile(np.arange(self.lidar.count_lasers), n_seq)
cur_longitudes = np.tile(self.lidar.omega, n_seq)
# initilaise states
if index == 0 or self.pos_X is None:
self.pos_X = cur_X
self.pos_Y = cur_Y
self.pos_Z = cur_Z
self.intensities = cur_distances
self.latitudes = cur_latitudes
self.timestamps = cur_timestamps
self.distances = cur_distances
self.indicies = cur_indicies
self.longitudes = cur_longitudes
if self.cur_azimuth is None:
self.cur_azimuth = cur_latitudes
self.last_azimuth = cur_latitudes
# update current azimuth before checking for roll over
self.cur_azimuth = cur_latitudes
# check if a frame is finished
idx_rollovr = self.is_roll_over()
# handle rollover (full 360° frame)
if idx_rollovr is not None:
if idx_rollovr > 0:
self.pos_X = np.hstack((self.pos_X, cur_X[0:idx_rollovr+1]))
self.pos_Y = np.hstack((self.pos_Y, cur_Y[0:idx_rollovr+1]))
self.pos_Z = np.hstack((self.pos_Z, cur_Z[0:idx_rollovr+1]))
self.intensities = np.hstack((self.intensities, cur_intensities[0:idx_rollovr+1]))
self.latitudes = np.hstack((self.latitudes, cur_latitudes[0:idx_rollovr+1]))
self.timestamps = np.hstack((self.timestamps, cur_timestamps[0:idx_rollovr+1]))
self.distances = np.hstack((self.distances, cur_distances[0:idx_rollovr+1]))
self.indicies = np.hstack((self.indicies, cur_indicies[0:idx_rollovr+1]))
self.longitudes = np.hstack((self.longitudes, cur_longitudes[0:idx_rollovr+1]))
min, sec, micro = self.time_from_lidar(self.timestamps[0])
self.datetime = self.datetime.replace(minute=min, second=sec, microsecond=int(micro))
gpsweek, gpsdays, gpsseconds, gpsmicrosec = utc_to_weekseconds(self.datetime, 0)
if self.params['text']:
fpath = "{}/{}_frame_{}.{:06d}.txt".format(self.txt_path, self.frame_nr, gpsseconds, gpsmicrosec)
write_pcl_txt(fpath, self.timestamps, self.pos_X, self.pos_Y, self.pos_Z, self.indicies,
self.intensities, self.latitudes, self.longitudes, self.distances)
if self.params['ply']:
fpath = "{}/{}_frame_{}.{:06d}.pcd".format(self.pcl_path, self.frame_nr, gpsseconds, gpsmicrosec)
write_pcd(fpath, self.pos_X, self.pos_Y, self.pos_Z, self.intensities)
# reset states
if idx_rollovr > 0:
self.pos_X = cur_X[idx_rollovr+1:]
self.pos_Y = cur_Y[idx_rollovr+1:]
self.pos_Z = cur_Z[idx_rollovr+1:]
self.intensities = cur_intensities[idx_rollovr+1:]
self.latitudes = cur_latitudes[idx_rollovr+1:]
self.timestamps = cur_timestamps[idx_rollovr+1:]
self.distances = cur_distances[idx_rollovr+1:]
self.indicies = cur_indicies[idx_rollovr+1:]
self.longitudes = cur_longitudes[idx_rollovr+1:]
else:
self.pos_X = cur_X
self.pos_Y = cur_Y
self.pos_Z = cur_Z
self.intensities = cur_intensities
self.latitudes = cur_latitudes
self.timestamps = cur_timestamps
self.distances = cur_distances
self.indicies = cur_indicies
self.longitudes = cur_longitudes
self.frame_nr += 1
# reset roll over check
self.cur_azimuth = None
return
self.pos_X = np.hstack((self.pos_X, cur_X))
self.pos_Y = np.hstack((self.pos_Y, cur_Y))
self.pos_Z = np.hstack((self.pos_Z, cur_Z))
self.intensities = np.hstack((self.intensities, cur_intensities))
self.latitudes = np.hstack((self.latitudes, cur_latitudes))
self.timestamps = np.hstack((self.timestamps, cur_timestamps))
self.distances = np.hstack((self.distances, cur_distances))
self.indicies = np.hstack((self.indicies, cur_indicies))
self.longitudes = np.hstack((self.longitudes, cur_longitudes))
self.last_azimuth = cur_latitudes
def process_gps_frame(self, data, timestamp, index):
gps_msg = self.lidar.process_position_frame(data, index)
# open gps file to write in
if self.gps_fp is None:
try:
gps_path = Path("{}/{}.txt".format(self.out_path, "data_gps"))
self.gps_fp = open(gps_path, 'w')
# write point cloud as a text-file
header = "UTC-Time, Week, Seconds [sow], Status, Latitude [Deg], Longitudes [Deg], Velocity [m/s]\n"
self.gps_fp.write(header)
except Exception as ex:
print(ex)
txt = "{}, {}, {}, {}, {} {}, {} {}, {}\n".format(gps_msg.datetime, gps_msg.weeks, gps_msg.seconds,
gps_msg.status, gps_msg.lat, gps_msg.lat_ori,
gps_msg.long, gps_msg.lat_ori, gps_msg.velocity)
self.gps_fp.write(txt)
def is_roll_over(self):
"""
Check if one frame is completed, therefore 360° rotation of the lidar
:return:
"""
diff_cur = self.cur_azimuth[0:-1] - self.cur_azimuth[1:]
diff_cur_last = self.cur_azimuth - self.last_azimuth
res_cur = np.where(diff_cur > 0.)[0]
res_cur_last = np.where(diff_cur_last < 0.)[0]
if res_cur.size > 0:
index = res_cur[0]
return index
elif res_cur_last.size > 0:
index = res_cur_last[0]
return index
else:
return None
def time_from_lidar(self, timestamp):
"""
convert the timestamp [top of the hour in microsec] of a firing into
minutes, seconds and microseconds
:param timestamp:
:return:
"""
try:
micro = timestamp % (1000*1000)
min_float = (timestamp / (1000*1000*60)) % 60
min = int(min_float)
sec = int((timestamp / (1000*1000)) % 60)
min = int(min)
except Exception as ex:
print(ex)
return min, sec, micro
def create_folders(self):
self.out_path = Path("{}/{}".format(self.out_root, self.lidar_type.lower()))
# creating output dir
try:
os.makedirs(self.out_path.absolute())
except Exception as ex:
print(str(ex))
return False
# create point cloud dirs
self.pcl_path = Path("{}/{}".format(self.out_path, "data_pcl"))
try:
os.makedirs(self.pcl_path.absolute())
except Exception as ex:
print(str(ex))
return False
# if text-files are desired, create text-file dir
if self.params['text']:
self.txt_path = Path("{}/{}".format(self.out_path, "data_ascii"))
try:
os.makedirs(self.txt_path.absolute())
except Exception as ex:
print(str(ex))
return False
return True
def write_pcl_txt(path, timestamps, X, Y, Z, laser_id, intensities=None, latitudes=None, longitudes=None, distances=None):
header = "time,X,Y,Z,id,intensity,latitude,longitudes,distance\n"
try:
fp = open(path, 'w')
fp.write(header)
except Exception as ex:
print(str(ex))
return
M = np.vstack((timestamps, X, Y, Z, laser_id))
if intensities is not None:
M = np.vstack((M, intensities))
if latitudes is not None:
M = np.vstack((M, latitudes))
if longitudes is not None:
M = np.vstack((M, longitudes))
if distances is not None:
M = np.vstack((M, distances))
np.savetxt(fp, M.T, fmt=('%d', '%.6f', '%.6f', '%.6f', '%d', '%d', '%.3f', '%.3f', '%.3f'), delimiter=',')
fp.close()
def write_pcd(path, X, Y, Z, intensities=None):
template = """VERSION {}\nFIELDS {}\nSIZE {}\nTYPE {}\nCOUNT {}\nWIDTH {}\nHEIGHT {}\nVIEWPOINT {}\nPOINTS {}\nDATA {}\n"""
X = X.astype(np.float32).reshape(1, -1)
Y = Y.astype(np.float32).reshape(1, -1)
Z = Z.astype(np.float32).reshape(1, -1)
if intensities is not None:
I = intensities.astype(np.float32).reshape(1, -1)
M = np.hstack((X.T, Y.T, Z.T, I.T))
pc_data = M.view(np.dtype([('x', np.float32), ('y', np.float32), ('z', np.float32), ('i', np.float32)]))
tmpl = template.format("0.7", "x y z intensity", "4 4 4 4", "F F F F", "1 1 1 1", pc_data.size, "1",
"0 0 0 1 0 0 0", pc_data.size, "binary")
else:
M = np.hstack((X.T, Y.T, Z.T))
pc_data = M.view(np.dtype([('x', np.float32), ('y', np.float32), ('z', np.float32)]))
tmpl = template.format("0.7", "x y z", "4 4 4", "F F F", "1 1 1", pc_data.size, "1", "0 0 0 1 0 0 0",
pc_data.size, "binary")
fp = open(path, 'wb')
fp.write(tmpl.encode())
fp.write(pc_data.tostring())
fp.close()