-
Notifications
You must be signed in to change notification settings - Fork 187
/
generate_sequential.py
executable file
·213 lines (164 loc) · 5.95 KB
/
generate_sequential.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
#!/usr/bin/env python3
# This file is covered by the LICENSE file in the root of this project.
import argparse
import os
import yaml
import numpy as np
from collections import deque
import shutil
from numpy.linalg import inv
import struct
import time
def parse_calibration(filename):
""" read calibration file with given filename
Returns
-------
dict
Calibration matrices as 4x4 numpy arrays.
"""
calib = {}
calib_file = open(filename)
for line in calib_file:
key, content = line.strip().split(":")
values = [float(v) for v in content.strip().split()]
pose = np.zeros((4, 4))
pose[0, 0:4] = values[0:4]
pose[1, 0:4] = values[4:8]
pose[2, 0:4] = values[8:12]
pose[3, 3] = 1.0
calib[key] = pose
calib_file.close()
return calib
def parse_poses(filename, calibration):
""" read poses file with per-scan poses from given filename
Returns
-------
list
list of poses as 4x4 numpy arrays.
"""
file = open(filename)
poses = []
Tr = calibration["Tr"]
Tr_inv = inv(Tr)
for line in file:
values = [float(v) for v in line.strip().split()]
pose = np.zeros((4, 4))
pose[0, 0:4] = values[0:4]
pose[1, 0:4] = values[4:8]
pose[2, 0:4] = values[8:12]
pose[3, 3] = 1.0
poses.append(np.matmul(Tr_inv, np.matmul(pose, Tr)))
return poses
if __name__ == '__main__':
start_time = time.time()
parser = argparse.ArgumentParser("./generate_sequential.py")
parser.add_argument(
'--dataset',
'-d',
type=str,
required=True,
help='dataset folder containing all sequences in a folder called "sequences".',
)
parser.add_argument(
'--output',
'-o',
type=str,
required=True,
help='output folder for generated sequence scans.',
)
parser.add_argument(
'--sequence_length',
'-s',
type=int,
required=True,
help='length of sequence, i.e., how many scans are concatenated.',
)
FLAGS, unparsed = parser.parse_known_args()
# print summary of what we will do
print("*" * 80)
print(" dataset folder: ", FLAGS.dataset)
print(" output folder: ", FLAGS.output)
print("sequence length: ", FLAGS.sequence_length)
print("*" * 80)
sequences_dir = os.path.join(FLAGS.dataset, "sequences")
sequence_folders = [
f for f in sorted(os.listdir(sequences_dir))
if os.path.isdir(os.path.join(sequences_dir, f))
]
for folder in sequence_folders:
input_folder = os.path.join(sequences_dir, folder)
output_folder = os.path.join(FLAGS.output, "sequences", folder)
velodyne_folder = os.path.join(output_folder, "velodyne")
labels_folder = os.path.join(output_folder, "labels")
if os.path.exists(output_folder) or os.path.exists(
velodyne_folder) or os.path.exists(labels_folder):
print("Output folder '{}' already exists!".format(output_folder))
answer = input("Overwrite? [y/N] ")
if answer != "y":
print("Aborted.")
exit(1)
if not os.path.exists(velodyne_folder):
os.makedirs(velodyne_folder)
if not os.path.exists(labels_folder):
os.makedirs(labels_folder)
else:
os.makedirs(velodyne_folder)
os.makedirs(labels_folder)
shutil.copy(os.path.join(input_folder, "poses.txt"), output_folder)
shutil.copy(os.path.join(input_folder, "calib.txt"), output_folder)
scan_files = [
f for f in sorted(os.listdir(os.path.join(input_folder, "velodyne")))
if f.endswith(".bin")
]
history = deque()
calibration = parse_calibration(os.path.join(input_folder, "calib.txt"))
poses = parse_poses(os.path.join(input_folder, "poses.txt"), calibration)
progress = 10
print("Processing {} ".format(folder), end="", flush=True)
for i, f in enumerate(scan_files):
# read scan and labels, get pose
scan_filename = os.path.join(input_folder, "velodyne", f)
scan = np.fromfile(scan_filename, dtype=np.float32)
scan = scan.reshape((-1, 4))
label_filename = os.path.join(input_folder, "labels", os.path.splitext(f)[0] + ".label")
labels = np.fromfile(label_filename, dtype=np.uint32)
labels = labels.reshape((-1))
# convert points to homogenous coordinates (x, y, z, 1)
points = np.ones((scan.shape))
points[:, 0:3] = scan[:, 0:3]
remissions = scan[:, 3]
pose = poses[i]
# prepare single numpy array for all points that can be written at once.
num_concat_points = points.shape[0]
num_concat_points += sum([past["points"].shape[0] for past in history])
concated_points = np.zeros((num_concat_points * 4), dtype = np.float32)
concated_labels = np.zeros((num_concat_points), dtype = np.uint32)
start = 0
concated_points[4 * start:4 * (start + points.shape[0])] = scan.reshape((-1))
concated_labels[start:start + points.shape[0]] = labels
start += points.shape[0]
for past in history:
diff = np.matmul(inv(pose), past["pose"])
tpoints = np.matmul(diff, past["points"].T).T
tpoints[:, 3] = past["remissions"]
tpoints = tpoints.reshape((-1))
concated_points[4 * start:4 * (start + past["points"].shape[0])] = tpoints
concated_labels[start:start + past["labels"].shape[0]] = past["labels"]
start += past["points"].shape[0]
# write scan and labels in one pass.
concated_points.tofile(os.path.join(velodyne_folder, f))
concated_labels.tofile(os.path.join(labels_folder, os.path.splitext(f)[0] + ".label"))
# append current data to history queue.
history.appendleft({
"points": points,
"labels": labels,
"remissions": remissions,
"pose": pose.copy()
})
if len(history) >= FLAGS.sequence_length:
history.pop()
if 100.0 * i / len(scan_files) >= progress:
print(".", end="", flush=True)
progress = progress + 10
print("finished.")
print("execution time: {}".format(time.time() - start_time))