-
Notifications
You must be signed in to change notification settings - Fork 13
/
match_kitti_imu.py
215 lines (187 loc) · 9.07 KB
/
match_kitti_imu.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
import os
import shutil
import PIL
from PIL import Image
import numpy as np
from tqdm import tqdm
def check_imu():
"""
check the timestamp match between raw_oxts and sync_oxts
-> also done in match_imu() where more filtering are performed
-> results are written to check_kitti_imu.log
"""
msgs = []
seqs = []
for mode in ["train", "val"]:
with open("kitti_extract/{}.txt".format(mode), 'r') as f:
for line in f.readlines():
if line.strip() not in seqs:
seqs.append(line.strip()) # e.g. "2011_09_30_drive_0028"
with open("test_scenes.txt", 'r') as f:
for line in f.readlines():
if line.strip() not in seqs:
seqs.append(line.strip()) # e.g. "2011_09_30_drive_0028"
for seq in seqs:
msgs.append('=========================')
msgs.append('processing seq {}'.format(seq))
print('=========================')
print('processing seq {}'.format(seq))
scene = "_".join(seq.split("_")[:3]) # "2011_09_30"
raw_dir = "kitti_extract/{}/{}_extract".format(scene, seq)
sync_dir = "kitti/kitti_raw/{}/{}_sync".format(scene, seq)
raw_time = []
sync_time = []
with open('{}/oxts/timestamps.txt'.format(raw_dir), mode='r') as f:
for _line in tqdm(f.readlines()):
raw_time.append(_line[:-1])
with open('{}/oxts/timestamps.txt'.format(sync_dir), mode='r') as f:
for _line in tqdm(f.readlines()):
sync_time.append(_line[:-1])
missing_num = 0
for _i, _sync in tqdm(enumerate(sync_time)):
if _sync not in raw_time:
msgs.append('Not found: [{}] {}'.format(_i, _sync))
missing_num += 1
print('--------------------------------')
print('total number of timestamps: {}'.format(len(sync_time)))
print('the number of not found: {}'.format(missing_num))
msgs.append('--------------------------------')
msgs.append('total number of timestamps: {}'.format(len(sync_time)))
msgs.append('the number of not found: {}'.format(missing_num))
with open('check_kitti_imu.log', mode='w') as f:
for _msg in msgs:
print(_msg)
f.write('{}\n'.format(_msg))
def match_imu():
"""
match imu from raw_oxts to sync_oxts in sync/00/matched_oxts
-> results are written to match_kitti_imu.log
"""
msgs = []
seqs = []
for mode in ["train", "val"]:
with open("kitti_extract/{}.txt".format(mode), 'r') as f:
for line in f.readlines():
if line.strip() not in seqs:
seqs.append(line.strip()) # e.g. "2011_09_30_drive_0028"
with open("test_scenes.txt", 'r') as f:
for line in f.readlines():
if line.strip() not in seqs:
seqs.append(line.strip()) # e.g. "2011_09_30_drive_0028"
for seq in seqs:
msgs.append('=========================')
msgs.append('processing seq {}'.format(seq))
print('=========================')
print('processing seq {}'.format(seq))
scene = "_".join(seq.split("_")[:3]) # "2011_09_30"
raw_dir = "kitti_extract/{}/{}_extract".format(scene, seq)
sync_dir = "kitti/kitti_raw/{}/{}_sync".format(scene, seq)
raw_oxt = []
sync_oxt = []
raw_num = len([x for x in os.listdir('{}/oxts/data/'.format(raw_dir)) if x[-4:] == '.txt'])
oxt_num = len([x for x in os.listdir('{}/oxts/data/'.format(sync_dir)) if x[-4:] == '.txt'])
for _i in tqdm(range(raw_num)):
with open('{}/oxts/data/{:010d}.txt'.format(raw_dir, _i), mode='r') as f:
tmp_list = []
for _line in f.readlines():
if _line[-1] == '\n':
_line = _line[:-1]
tmp_list.append(_line)
assert len(tmp_list) == 1
raw_oxt.append(tmp_list[0])
for _j in tqdm(range(oxt_num)):
with open('{}/oxts/data/{:010d}.txt'.format(sync_dir, _j), mode='r') as f:
tmp_list = []
for _line in f.readlines():
if _line[-1] == '\n':
_line = _line[:-1]
tmp_list.append(_line)
assert len(tmp_list) == 1
sync_oxt.append(tmp_list[0])
raw_time = []
sync_time = []
with open('{}/oxts/timestamps.txt'.format(raw_dir), mode='r') as f:
for _line in tqdm(f.readlines()):
if _line[-1] == '\n':
_line = _line[:-1]
raw_time.append(_line)
with open('{}/oxts/timestamps.txt'.format(sync_dir), mode='r') as f:
for _line in tqdm(f.readlines()):
if _line[-1] == '\n':
_line = _line[:-1]
sync_time.append(_line)
assert len(raw_oxt) == len(raw_time)
assert len(sync_oxt) == len(sync_time)
matched_oxt = []
matched_timestamp = []
imu_num = []
for _i in tqdm(range(len(sync_time) - 1)):
if sync_time[_i] not in raw_time or sync_time[_i + 1] not in raw_time:
matched_oxt.append('nan')
matched_timestamp.append('nan')
imu_num.append('nan')
else:
raw_ind_0 = raw_time.index(sync_time[_i])
raw_ind_1 = raw_time.index(sync_time[_i + 1])
no_outlier = True
for _k in range(raw_ind_0, raw_ind_1, 1):
if no_outlier:
# check whether there is a jump in timestamps in raw_imu
tmp_time_0 = raw_time[_k]
tmp_time_0 = [float(x) for x in tmp_time_0.split(' ')[1].split(':')]
tmp_time_0 = 3600 * tmp_time_0[0] + 60 * tmp_time_0[1] + tmp_time_0[2]
tmp_time_1 = raw_time[_k + 1]
tmp_time_1 = [float(x) for x in tmp_time_1.split(' ')[1].split(':')]
tmp_time_1 = 3600 * tmp_time_1[0] + 60 * tmp_time_1[1] + tmp_time_1[2]
tdiff = tmp_time_1 - tmp_time_0 # should be 0.01 (100Hz for raw_data)
if abs(tdiff - 0.01) > 0.005: # allow for a 5ms drift
no_outlier = False
matched_oxt.append('nan')
matched_timestamp.append('nan')
imu_num.append('nan')
msgs.append('warning: raw_time[{}] and [{}] failed for drift check (tdiff: {})'.format(_k, _k+1, tdiff))
if no_outlier:
tmp_gap = raw_ind_1 - raw_ind_0 + 1
if tmp_gap < 11:
matched_oxt.append('nan')
matched_timestamp.append('nan')
imu_num.append('nan')
msgs.append('warning: imu_gap: {} for raw_time[{}] and [{}]'.format(tmp_gap, raw_ind_0, raw_ind_1))
else:
matched_oxt.append(' | '.join(raw_oxt[raw_ind_0:raw_ind_1 + 1]))
matched_timestamp.append(' | '.join(raw_time[raw_ind_0:raw_ind_1 + 1]))
imu_num.append(tmp_gap)
msgs.append('-------------------------')
msgs.append('total number of items: {}'.format(len(matched_oxt)))
msgs.append('summarization of imu nums between two images:')
for _s in set(imu_num):
msgs.append('-> {}: {}'.format(_s, imu_num.count(_s)))
path_matched_oxt = '{}/matched_oxts'.format(sync_dir)
if os.path.isdir(path_matched_oxt):
shutil.rmtree(path_matched_oxt)
os.mkdir(path_matched_oxt)
os.mkdir('{}/data'.format(path_matched_oxt))
with open('{}/matched_timestamps.txt'.format(path_matched_oxt), mode='w') as f:
for _time in matched_timestamp:
f.write('{}\n'.format(_time))
with open('{}/matched_oxts.txt'.format(path_matched_oxt), mode='w') as f:
for _oxt in matched_oxt:
f.write('{}\n'.format(_oxt))
for _i, _oxt in enumerate(matched_oxt):
with open('{}/data/{:010d}.txt'.format(path_matched_oxt, _i), mode='w') as f:
f.write(_oxt)
with open('match_kitti_imu.log', mode='w') as f:
for _msg in msgs:
print(_msg)
f.write('{}\n'.format(_msg))
def prepare_kitti():
"""
match 100 hz imu data from raw_data to the dataset released in kitti odometry leaderboard
requires the download of
(1) sync dataset from the kitti raw data website -> need image_02/ (10 hz) and oxts/ (10 hz) for matching
(2) unsync dataset from the kitti raw data website -> need oxts/ (100 hz) for matching
"""
match_imu() # generate matched_oxts.txt and matched_timestamps for sync_data
if __name__ == "__main__":
# check_imu()
prepare_kitti()