|
| 1 | +# MIT License |
| 2 | +# |
| 3 | +# Copyright (c) 2023 Benedikt Mersch, Tiziano Guadagnino, Ignacio Vizzo, Cyrill Stachniss |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +import glob |
| 24 | +import os |
| 25 | +import numpy as np |
| 26 | + |
| 27 | + |
| 28 | +class HeliMOSDataset: |
| 29 | + def __init__(self, data_dir, sequence: str, *_, **__): |
| 30 | + self.sequence_id = sequence.split("/")[0] |
| 31 | + split_file = sequence.split("/")[1] |
| 32 | + self.sequence_dir = os.path.join(data_dir, self.sequence_id) |
| 33 | + self.scan_dir = os.path.join(self.sequence_dir, "velodyne/") |
| 34 | + |
| 35 | + self.scan_files = sorted(glob.glob(self.scan_dir + "*.bin")) |
| 36 | + self.calibration = self.read_calib_file(os.path.join(self.sequence_dir, "calib.txt")) |
| 37 | + |
| 38 | + # Load GT Poses (if available) |
| 39 | + self.poses_fn = os.path.join(self.sequence_dir, "poses.txt") |
| 40 | + if os.path.exists(self.poses_fn): |
| 41 | + self.gt_poses = self.load_poses(self.poses_fn) |
| 42 | + |
| 43 | + # No correction |
| 44 | + self.correct_kitti_scan = lambda frame: frame |
| 45 | + |
| 46 | + # Load labels |
| 47 | + self.label_dir = os.path.join(self.sequence_dir, "labels/") |
| 48 | + label_files = sorted(glob.glob(self.label_dir + "*.label")) |
| 49 | + |
| 50 | + # Get labels for train/val split if desired |
| 51 | + label_indices = np.loadtxt(os.path.join(data_dir, split_file), dtype=int).tolist() |
| 52 | + |
| 53 | + # Filter based on split if desired |
| 54 | + getIndex = lambda filename: int(os.path.basename(filename).split(".label")[0]) |
| 55 | + self.dict_label_files = { |
| 56 | + getIndex(filename): filename |
| 57 | + for filename in label_files |
| 58 | + if getIndex(filename) in label_indices |
| 59 | + } |
| 60 | + |
| 61 | + def __getitem__(self, idx): |
| 62 | + points = self.scans(idx) |
| 63 | + timestamps = np.zeros(len(points)) |
| 64 | + labels = ( |
| 65 | + self.read_labels(self.dict_label_files[idx]) |
| 66 | + if idx in self.dict_label_files.keys() |
| 67 | + else np.full(len(points), -1, dtype=np.int32) |
| 68 | + ) |
| 69 | + return points, timestamps, labels |
| 70 | + |
| 71 | + def __len__(self): |
| 72 | + return len(self.scan_files) |
| 73 | + |
| 74 | + def scans(self, idx): |
| 75 | + return self.read_point_cloud(self.scan_files[idx]) |
| 76 | + |
| 77 | + def apply_calibration(self, poses: np.ndarray) -> np.ndarray: |
| 78 | + """Converts from Velodyne to Camera Frame""" |
| 79 | + Tr = np.eye(4, dtype=np.float64) |
| 80 | + Tr[:3, :4] = self.calibration["Tr"].reshape(3, 4) |
| 81 | + return Tr @ poses @ np.linalg.inv(Tr) |
| 82 | + |
| 83 | + def read_point_cloud(self, scan_file: str): |
| 84 | + points = np.fromfile(scan_file, dtype=np.float32).reshape((-1, 4))[:, :3].astype(np.float64) |
| 85 | + return points |
| 86 | + |
| 87 | + def load_poses(self, poses_file): |
| 88 | + def _lidar_pose_gt(poses_gt): |
| 89 | + _tr = self.calibration["Tr"].reshape(3, 4) |
| 90 | + tr = np.eye(4, dtype=np.float64) |
| 91 | + tr[:3, :4] = _tr |
| 92 | + left = np.einsum("...ij,...jk->...ik", np.linalg.inv(tr), poses_gt) |
| 93 | + right = np.einsum("...ij,...jk->...ik", left, tr) |
| 94 | + return right |
| 95 | + |
| 96 | + poses = np.loadtxt(poses_file, delimiter=" ") |
| 97 | + n = poses.shape[0] |
| 98 | + poses = np.concatenate( |
| 99 | + (poses, np.zeros((n, 3), dtype=np.float32), np.ones((n, 1), dtype=np.float32)), axis=1 |
| 100 | + ) |
| 101 | + poses = poses.reshape((n, 4, 4)) # [N, 4, 4] |
| 102 | + |
| 103 | + # Ensure rotations are SO3 |
| 104 | + rotations = poses[:, :3, :3] |
| 105 | + U, _, Vh = np.linalg.svd(rotations) |
| 106 | + poses[:, :3, :3] = U @ Vh |
| 107 | + |
| 108 | + return _lidar_pose_gt(poses) |
| 109 | + |
| 110 | + @staticmethod |
| 111 | + def read_calib_file(file_path: str) -> dict: |
| 112 | + calib_dict = {} |
| 113 | + with open(file_path, "r") as calib_file: |
| 114 | + for line in calib_file.readlines(): |
| 115 | + tokens = line.split(" ") |
| 116 | + if tokens[0] == "calib_time:": |
| 117 | + continue |
| 118 | + # Only read with float data |
| 119 | + if len(tokens) > 0: |
| 120 | + values = [float(token) for token in tokens[1:]] |
| 121 | + values = np.array(values, dtype=np.float32) |
| 122 | + |
| 123 | + # The format in KITTI's file is <key>: <f1> <f2> <f3> ...\n -> Remove the ':' |
| 124 | + key = tokens[0][:-1] |
| 125 | + calib_dict[key] = values |
| 126 | + return calib_dict |
| 127 | + |
| 128 | + def read_labels(self, filename): |
| 129 | + """Load moving object labels from .label file""" |
| 130 | + orig_labels = np.fromfile(filename, dtype=np.int32).reshape((-1)) |
| 131 | + orig_labels = orig_labels & 0xFFFF # Mask semantics in lower half |
| 132 | + |
| 133 | + labels = np.zeros_like(orig_labels) |
| 134 | + labels[orig_labels <= 1] = -1 # Unlabeled (0), outlier (1) |
| 135 | + labels[orig_labels > 250] = 1 # Moving |
| 136 | + labels = labels.astype(dtype=np.int32).reshape(-1) |
| 137 | + return labels |
0 commit comments