-
Notifications
You must be signed in to change notification settings - Fork 949
/
kitti_dataset.py
134 lines (103 loc) · 4.58 KB
/
kitti_dataset.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
# Copyright Niantic 2019. Patent Pending. All rights reserved.
#
# This software is licensed under the terms of the Monodepth2 licence
# which allows for non-commercial use only, the full terms of which are made
# available in the LICENSE file.
from __future__ import absolute_import, division, print_function
import os
import skimage.transform
import numpy as np
import PIL.Image as pil
from kitti_utils import generate_depth_map
from .mono_dataset import MonoDataset
class KITTIDataset(MonoDataset):
"""Superclass for different types of KITTI dataset loaders
"""
def __init__(self, *args, **kwargs):
super(KITTIDataset, self).__init__(*args, **kwargs)
# NOTE: Make sure your intrinsics matrix is *normalized* by the original image size.
# To normalize you need to scale the first row by 1 / image_width and the second row
# by 1 / image_height. Monodepth2 assumes a principal point to be exactly centered.
# If your principal point is far from the center you might need to disable the horizontal
# flip augmentation.
self.K = np.array([[0.58, 0, 0.5, 0],
[0, 1.92, 0.5, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]], dtype=np.float32)
self.full_res_shape = (1242, 375)
self.side_map = {"2": 2, "3": 3, "l": 2, "r": 3}
def check_depth(self):
line = self.filenames[0].split()
scene_name = line[0]
frame_index = int(line[1])
velo_filename = os.path.join(
self.data_path,
scene_name,
"velodyne_points/data/{:010d}.bin".format(int(frame_index)))
return os.path.isfile(velo_filename)
def get_color(self, folder, frame_index, side, do_flip):
color = self.loader(self.get_image_path(folder, frame_index, side))
if do_flip:
color = color.transpose(pil.FLIP_LEFT_RIGHT)
return color
class KITTIRAWDataset(KITTIDataset):
"""KITTI dataset which loads the original velodyne depth maps for ground truth
"""
def __init__(self, *args, **kwargs):
super(KITTIRAWDataset, self).__init__(*args, **kwargs)
def get_image_path(self, folder, frame_index, side):
f_str = "{:010d}{}".format(frame_index, self.img_ext)
image_path = os.path.join(
self.data_path, folder, "image_0{}/data".format(self.side_map[side]), f_str)
return image_path
def get_depth(self, folder, frame_index, side, do_flip):
calib_path = os.path.join(self.data_path, folder.split("/")[0])
velo_filename = os.path.join(
self.data_path,
folder,
"velodyne_points/data/{:010d}.bin".format(int(frame_index)))
depth_gt = generate_depth_map(calib_path, velo_filename, self.side_map[side])
depth_gt = skimage.transform.resize(
depth_gt, self.full_res_shape[::-1], order=0, preserve_range=True, mode='constant')
if do_flip:
depth_gt = np.fliplr(depth_gt)
return depth_gt
class KITTIOdomDataset(KITTIDataset):
"""KITTI dataset for odometry training and testing
"""
def __init__(self, *args, **kwargs):
super(KITTIOdomDataset, self).__init__(*args, **kwargs)
def get_image_path(self, folder, frame_index, side):
f_str = "{:06d}{}".format(frame_index, self.img_ext)
image_path = os.path.join(
self.data_path,
"sequences/{:02d}".format(int(folder)),
"image_{}".format(self.side_map[side]),
f_str)
return image_path
class KITTIDepthDataset(KITTIDataset):
"""KITTI dataset which uses the updated ground truth depth maps
"""
def __init__(self, *args, **kwargs):
super(KITTIDepthDataset, self).__init__(*args, **kwargs)
def get_image_path(self, folder, frame_index, side):
f_str = "{:010d}{}".format(frame_index, self.img_ext)
image_path = os.path.join(
self.data_path,
folder,
"image_0{}/data".format(self.side_map[side]),
f_str)
return image_path
def get_depth(self, folder, frame_index, side, do_flip):
f_str = "{:010d}.png".format(frame_index)
depth_path = os.path.join(
self.data_path,
folder,
"proj_depth/groundtruth/image_0{}".format(self.side_map[side]),
f_str)
depth_gt = pil.open(depth_path)
depth_gt = depth_gt.resize(self.full_res_shape, pil.NEAREST)
depth_gt = np.array(depth_gt).astype(np.float32) / 256
if do_flip:
depth_gt = np.fliplr(depth_gt)
return depth_gt