-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathutils.py
More file actions
534 lines (418 loc) · 16.8 KB
/
utils.py
File metadata and controls
534 lines (418 loc) · 16.8 KB
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
from datetime import datetime
import time
import numpy as np
import os
import glob
import cv2
from PIL import Image
def load_images_KITTI(path_to_sequence):
"""Return the sequence of the images found in the path and the corrispondent timestamp
Args:
path_to_sequence : the sequence in witch we can found the image sequences
Returns :
two array : one contains the sequence of the image filename and the second the timestamp in whitch they are acquired
"""
timestamps = []
t0 = None
with open(os.path.join(path_to_sequence, "timestamps.txt")) as times_file:
for line in times_file:
if len(line) > 0:
line = line[:-4]
if t0 is None:
t0 = datetime.strptime(line, "%Y-%m-%d %H:%M:%S.%f")
t1 = datetime.strptime(line, "%Y-%m-%d %H:%M:%S.%f")
difference = t1 - t0
timestamps.append(
difference.seconds + difference.microseconds / 1000000
)
return [
os.path.join(path_to_sequence, "data", str(idx).zfill(10) + ".png")
for idx in range(len(timestamps))
], timestamps
def load_images_TUM(path_to_sequence, file_name):
"""Return the sequence of the images found in the path and the corrispondent timestamp
Args:
path_to_sequence : the sequence in witch we can found the image sequences
Returns:
two array : one contains the sequence of the image filename and the second the timestamp in whitch they are acquired
"""
timestamps = []
rgb_filenames = []
with open(os.path.join(path_to_sequence, file_name)) as times_file:
for line in times_file:
if len(line) > 0 and not line.startswith("#"):
t, rgb = line.rstrip().split(" ")[0:2]
rgb_filenames.append(rgb)
timestamps.append(float(t))
return [os.path.join(path_to_sequence, name) for name in rgb_filenames], timestamps
def compute_errors(gt, pred):
"""Computation of error metrics (abs rel,sq rel, rmse, rmse log) between predicted and ground truth depths
From https://github.com/mrharicot/monodepth
Args:
gt : an array with the ground truth values
pred : an array with the predicted values
Returns:
abs_rel,sq_rel,rmse,rmse_log : the error
"""
"""Computation of error metrics between predicted and ground truth depths
"""
thresh = np.maximum((gt / pred), (pred / gt))
a1 = (thresh < 1.25).mean()
a2 = (thresh < 1.25 ** 2).mean()
a3 = (thresh < 1.25 ** 3).mean()
rmse = (gt - pred) ** 2
rmse = np.sqrt(rmse.mean())
rmse_log = (np.log(gt) - np.log(pred)) ** 2
rmse_log = np.sqrt(rmse_log.mean())
abs_rel = np.mean(np.abs(gt - pred) / gt)
sq_rel = np.mean(((gt - pred) ** 2) / gt)
return abs_rel, sq_rel, rmse, rmse_log, a1, a2, a3
def convert_scale(points, gt_depth):
"""convert the scale of the predictions to the gt
Args:
points: the predictions depth
gt_filename: the gt references filename
Returns: the ratio between the predictions and the gt
"""
depth_SLAM = np.array(
[gt_depth[int(img_point[1]), int(img_point[0])] for (_, img_point) in points]
)
depth_SLAM_mask = depth_SLAM > 0
depth_SLAM = depth_SLAM[depth_SLAM_mask]
cp = np.array([cp[2] for (cp, _) in points])
cp = cp[depth_SLAM_mask]
return np.median(depth_SLAM) / np.median(cp)
def get_error_TUM(points, gt_filename):
"""Get the realtive gt from it's filename and convert the scale of the predictions in order to compute the error
Args:
points: the predictions depth
gt_filename: the gt references filename
Returns:
the error computed on this examples
"""
gt_depth = read_depth_TUM(gt_filename)
if points is not None:
ratio = convert_scale(points, gt_depth)
depth = []
gt = []
for (cp, img_point) in points:
depth_converted = cp[2] * ratio
if gt_depth[int(img_point[1]), int(img_point[0])] > 0:
depth.append(depth_converted)
gt.append(gt_depth[int(img_point[1]), int(img_point[0])])
depth = np.array(depth)
gt = np.array(gt)
return compute_errors(gt, depth)
def get_error_KITTI(points, gt_filename):
"""Get the realtive gt from it's filename and convert the scale of the predictions in order to compute the error
Args:
points: the predictions depth
gt_filename: the gt references filename
Returns:
the error computed on this examples
"""
gt_depth = read_depth_KITTI(gt_filename)
if points is not None:
ratio = convert_scale(points, gt_depth)
depth = []
gt = []
for (cp, img_point) in points:
depth_converted = cp[2] * ratio
if gt_depth[int(img_point[1]), int(img_point[0])] > 0:
depth.append(depth_converted)
gt.append(gt_depth[int(img_point[1]), int(img_point[0])])
depth = np.array(depth)
gt = np.array(gt)
return compute_errors(gt, depth)
def read_depth_KITTI(filename):
"""loads depth map D from png file and returns it as a numpy array,"""
depth_png = np.array(Image.open(filename), dtype=int)
# make sure we have a proper 16bit depth map here.. not 8bit!
assert np.max(depth_png) > 255
depth = depth_png.astype(np.float) / 256.0
depth[depth_png == 0] = -1.0
return depth
def read_depth_TUM(filename):
"""loads depth map D from png file and returns it as a numpy array,"""
depth_png = np.array(Image.open(filename), dtype=int)
# make sure we have a proper 16bit depth map here.. not 8bit!
assert np.max(depth_png) > 255
depth = (depth_png.astype(np.float) / 256) / 5000.0
return depth
def create_dir(directory):
"""Create a directory if not exists
Args:
directory: directory to create
Returns:
None, but it creates a new folder if not exists
"""
if not os.path.exists(directory):
os.makedirs(directory)
def save_depth(dest, depth):
"""Save depth as 16 bit png file
Args:
dest: path to new 16 bit png image wiht depth, w/o exension
depth: depth to save, as ndarray HxW
Returns:
None, but a new 16 bit png image will be saved at dest
"""
cv2.imwrite(f"{dest}.png", (depth * 256).astype(np.uint16))
def save_pose(dest, pose):
"""Save pose as npy file
Args:
dest: path to new npy file wiht pose, w/o exension
pose: ndarray with 4x4 pose matrix (as R|t in homogeneous notation)
Returns:
None, but it creates a new npy file with the pose
"""
np.save(f"{dest}.npy", pose)
def save_pose_txt(args, name, pose):
"""Save pose as txt file
Args:
dir: directory of pose.txt
name: frame name or id
pose: ndarray with 4x4 pose matrix (as R|t in homogeneous notation)
Returns:
None, but it creates a new npy file with the pose
"""
pose_file_path = os.path.join(args.dest, "pose.txt")
fp = open(pose_file_path, "a")
pose34 = pose[:3]
fp.write(name)
for row in pose34:
fp.write(" ")
fp.write(" ".join(str(round(i, 10)) for i in row))
# fp.write('\n')
fp.write("\n")
fp.close()
def save_pose_and_times_txt(args, name, pose):
"""Save pose and time in two different txt files."""
time_file_path = os.path.join(args.dest, "times.txt")
pose_file_path = os.path.join(args.dest, "pose.txt")
fd = open(time_file_path, "a")
fp = open(pose_file_path, "a")
pose34 = pose[:3]
fd.write(name)
fd.write("\n")
fd.close()
line = ""
for row in pose34:
line += " ".join(str(round(i, 10)) for i in row)
line += " "
line = line[:-1]
line += "\n"
fp.write(line)
# fp.write('\n')
fp.close()
def evaluate_pose(args):
"""Evaluate odometry on the KITTI dataset"""
orb_pose_dir = os.path.join(args.dest, "pose")
pred_poses = []
gt_local_poses = []
ates = []
for count, dir_name in enumerate(sorted(os.listdir(orb_pose_dir))):
fileId = os.path.splitext(dir_name)[0]
id = fileId.zfill(6)
pose_file = os.path.join(orb_pose_dir, "{}.npy".format(id))
pred_poses.append(np.load(pose_file))
gt_pose_file = os.path.join(args.gt_pose_dir, "{}.npy".format(id))
gt_local_poses.append(np.load(gt_pose_file))
num_frames = len(gt_local_poses)
track_length = 5
for i in range(0, num_frames - track_length - 1):
if i == num_frames - track_length - 2:
print("break")
local_xyzs = np.array(dump_xyz(pred_poses[i : i + track_length - 1]))
gt_local_xyzs = np.array(dump_xyz(gt_local_poses[i : i + track_length - 1]))
ates.append(compute_ate(gt_local_xyzs, local_xyzs))
print(
"\n Trajectory error: {:0.3f}, std: {:0.3f}\n".format(
np.mean(ates), np.std(ates)
)
)
# from https://github.com/tinghuiz/SfMLearner
def dump_xyz(source_to_target_transformations):
xyzs = []
cam_to_world = np.eye(4)
xyzs.append(cam_to_world[:3, 3])
for source_to_target_transformation in source_to_target_transformations:
cam_to_world = np.dot(cam_to_world, source_to_target_transformation)
xyzs.append(cam_to_world[:3, 3])
return xyzs
# from https://github.com/tinghuiz/SfMLearner
def compute_ate(gtruth_xyz, pred_xyz_o):
# Make sure that the first matched frames align (no need for rotational alignment as
# all the predicted/ground-truth snippets have been converted to use the same coordinate
# system with the first frame of the snippet being the origin).
offset = gtruth_xyz[0] - pred_xyz_o[0]
pred_xyz = pred_xyz_o + offset[None, :]
# Optimize the scaling factor
scale = np.sum(gtruth_xyz * pred_xyz) / (np.sum(pred_xyz ** 2) + 0.00001)
alignment_error = pred_xyz * scale - gtruth_xyz
rmse = np.sqrt(np.sum(alignment_error ** 2)) / gtruth_xyz.shape[0]
return rmse
def save_depth_err_results(file_path, filename, err):
f = open(file_path, "a+")
print("----------------------------------------------")
print("image id:{}".format(filename))
print(
"\n "
+ ("{:>8} | " * 7).format(
"abs_rel", "sq_rel", "rmse", "rmse_log", "a1", "a2", "a3"
)
)
print(("&{: 8.3f} " * 7).format(*err) + "\\\\")
f.writelines("----------------------------------------------\n")
f.writelines("image id:{}".format(filename))
f.writelines(
"\n "
+ ("{:>8} | " * 7).format(
"abs_rel", "sq_rel", "rmse", "rmse_log", "a1", "a2", "a3"
)
)
f.writelines("\n")
f.writelines(("&{: 8.3f} " * 7).format(*err) + "\\\\")
f.writelines("\n")
return
def get_error(args, filename, points, gt_filename):
"""Get the realtive gt from it's filename and convert the scale of the predictions in order to compute the error
Args:
points: the predictions depth
gt_filename: the gt references filename
Returns:
the error computed on this examples
"""
MIN_DEPTH = 1e-3
if args.data_type == "KITTI_VO":
MAX_DEPTH = 100
gt_depth = cv2.imread(gt_filename, -1) / 256
if gt_depth is None:
print("gt path err {}".gt_filename)
return None
elif args.data_type == "TUM":
MAX_DEPTH = 10
gt_depth = (cv2.imread(gt_filename, -1) / 256) / 5000.0
if gt_depth is None:
print("gt path err {}".gt_filename)
return None
else:
print("Error data type {}".format(args.data_type))
return
pred_depth = points
mask_pred = pred_depth > 0
mask_gt = gt_depth > 0
mask = (mask_pred) & (mask_gt)
gt_depth = gt_depth[mask]
pred_depth = pred_depth[mask]
ratio = np.median(gt_depth) / np.median(pred_depth)
pred_depth *= ratio
pred_depth[pred_depth < MIN_DEPTH] = MIN_DEPTH
pred_depth[pred_depth > MAX_DEPTH] = MAX_DEPTH
err = compute_errors(gt_depth, pred_depth)
save_results = os.path.join(args.dest, "results.txt")
save_depth_err_results(save_results, filename, err)
return err
def load_images_KITTI_VO(path_to_sequence):
"""Return the sequence of the images found in the path and the corrispondent timestamp
Args:
path_to_sequence : the sequence in witch we can found the image sequences
Returns :
two array : one contains the sequence of the image filename and the second the timestamp in whitch they are acquired
"""
timestamps = []
t0 = None
with open(os.path.join(path_to_sequence, "times.txt")) as times_file:
for line in times_file:
if len(line) > 0:
timestamps.append(float(line))
return [
os.path.join(path_to_sequence, "data", str(idx).zfill(6) + ".png")
for idx in range(len(timestamps))
], timestamps
def load_images_TUM(path_to_sequence, file_name):
"""Return the sequence of the images found in the path and the corrispondent timestamp
Args:
path_to_sequence : the sequence in witch we can found the image sequences
Returns:
two array : one contains the sequence of the image filename and the second the timestamp in whitch they are acquired
"""
timestamps = []
rgb_filenames = []
with open(os.path.join(path_to_sequence, file_name)) as times_file:
for line in times_file:
if len(line) > 0 and not line.startswith("#"):
t, rgb = line.rstrip().split(" ")[0:2]
rgb_filenames.append(rgb)
timestamps.append(float(t))
return [os.path.join(path_to_sequence, name) for name in rgb_filenames], timestamps
def load_images_OTHERS(path_to_sequence):
"""Return the sequence of the images found in the path and the corrispondent timestamp
Args:
path_to_sequence : the sequence in witch we can found the image sequences
Returns :
two array : one contains the sequence of the image filename and the second the timestamp in whitch they are acquired
Inside of path_to_sequence must be: +data
xxxxxxxx.png
xxxxxxxy.png
....
-times.txt
where times.txt simply contains timestamps of every frame
"""
timestamps = []
framenames = []
with open(os.path.join(path_to_sequence, "times.txt")) as times_file:
for line in times_file:
if len(line) > 0 and not line.startswith("#"):
timestamps.append(float(line))
for framename in sorted(os.listdir(os.path.join(path_to_sequence, "data"))):
if framename.endswith(".png"):
framenames.append(framename)
return [
os.path.join(path_to_sequence, "data", name) for name in framenames
], timestamps
def load_images_TUM_VI(path_to_sequence):
"""This loader is created for Visual Inertial TUM datasets. Format of such datasets is:
path_to_sequence/mav0/cam0/+data/xxxx.png
/-times.txt
"""
timestamps = []
framenames = []
with open(os.path.join(path_to_sequence, "mav0/cam0/times.txt")) as times_file:
for line in times_file:
if len(line) > 0 and not line.startswith("#"):
framenames.append(line.split()[0] + ".png")
timestamps.append(float(line.split()[1]))
return [
os.path.join(path_to_sequence, "mav0/cam0/data", name) for name in framenames
], timestamps
def load_images_EuRoC(path_to_sequence):
"""This loader is created for Visual Inertial EuRoC datasets. Format of such datasets is:
path_to_sequence/mav0/cam0/+data/xxxx.png
/-times.txt
"""
timestamps = []
framenames = []
with open(os.path.join(path_to_sequence, "mav0/cam0/data.csv")) as times_file:
for line in times_file:
if len(line) > 0 and not line.startswith("#"):
framenames.append(line.split(",")[1].rstrip())
timestamps.append(float(line.split(",")[0]) * 1e-9)
return [
os.path.join(path_to_sequence, "mav0/cam0/data", name) for name in framenames
], timestamps
def load_IMU_datas_TUM_VI(path_to_sequence):
timestamp = []
gyro_data = []
acc_data = []
with open(os.path.join(path_to_sequence, "mav0/imu0/data.csv")) as imu_file:
for line in imu_file:
if len(line) > 0 and not line.startswith("#"):
imu_line = line.split(",")
timestamp.append(float(imu_line[0]) * 1e-9)
gyro_data.append(
[float(imu_line[1]), float(imu_line[2]), float(imu_line[3])]
)
acc_data.append(
[float(imu_line[4]), float(imu_line[5]), float(imu_line[6])]
)
return acc_data, gyro_data, timestamp