-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix cols * Import cherry-picked implementations from KISS-ICP * merge duplicated implementation * let's version later * bump version * lol * whatever * fix build? * Remove indirect import Yes, CLI boot time is now slower * Fix python version in CI * Revert "Remove indirect import" This reverts commit fa162a8. * Go back to optional dependencies * Go back to delayed import for performance reasons * Make the errors more informative * Better error handling
- Loading branch information
1 parent
b68fa7e
commit 978ceb3
Showing
11 changed files
with
1,023 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,6 @@ authors = [ | |
{name = "Ignacio Vizzo", email = "[email protected]"}, | ||
] | ||
|
||
requires-python = ">=3.7" | ||
dependencies = [ "kiss-icp>=0.2.9" ] | ||
classifiers = [ | ||
"Operating System :: Unix", | ||
"Operating System :: MacOS", | ||
|
@@ -29,6 +27,22 @@ classifiers = [ | |
"License :: OSI Approved :: MIT License", | ||
] | ||
|
||
|
||
requires-python = ">=3.7, <3.11" | ||
dependencies = [ | ||
"natsort", | ||
"numpy", | ||
"tqdm", | ||
"typer[all]>=0.6.0", | ||
] | ||
|
||
[project.optional-dependencies] | ||
all = [ | ||
"pyntcloud", | ||
"trimesh", | ||
"ouster-sdk", | ||
] | ||
|
||
[project.urls] | ||
homepage = "https://github.com/PRBonn/lidar-visualizer" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# MIT License | ||
# | ||
# Copyright (c) 2022 Ignacio Vizzo, Tiziano Guadagnino, Benedikt Mersch, Cyrill | ||
# Stachniss. | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
from pathlib import Path | ||
from typing import Dict, List | ||
|
||
|
||
def supported_file_extensions(): | ||
return [ | ||
"bin", | ||
"pcd", | ||
"ply", | ||
"xyz", | ||
"obj", | ||
"ctm", | ||
"off", | ||
"stl", | ||
] | ||
|
||
|
||
def available_dataloaders() -> List: | ||
import os.path | ||
import pkgutil | ||
|
||
pkgpath = os.path.dirname(__file__) | ||
dataloaders = [name for _, name, _ in pkgutil.iter_modules([pkgpath])] | ||
dataloaders.remove("point_cloud2") | ||
return dataloaders | ||
|
||
|
||
def jumpable_dataloaders(): | ||
_jumpable_dataloaders = available_dataloaders() | ||
_jumpable_dataloaders.remove("mcap") | ||
_jumpable_dataloaders.remove("ouster") | ||
_jumpable_dataloaders.remove("rosbag") | ||
return _jumpable_dataloaders | ||
|
||
|
||
def dataloader_types() -> Dict: | ||
import ast | ||
import importlib | ||
|
||
dataloaders = available_dataloaders() | ||
_types = {} | ||
for dataloader in dataloaders: | ||
script = importlib.util.find_spec(f".{dataloader}", __name__).origin | ||
with open(script) as f: | ||
tree = ast.parse(f.read(), script) | ||
classes = [cls for cls in tree.body if isinstance(cls, ast.ClassDef)] | ||
_types[dataloader] = classes[0].name # assuming there is only 1 class | ||
return _types | ||
|
||
|
||
def dataset_factory(dataloader: str, data_dir: Path, *args, **kwargs): | ||
import importlib | ||
|
||
dataloader_type = dataloader_types()[dataloader] | ||
module = importlib.import_module(f".{dataloader}", __name__) | ||
assert hasattr(module, dataloader_type), f"{dataloader_type} is not defined in {module}" | ||
dataset = getattr(module, dataloader_type) | ||
return dataset(data_dir=data_dir, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# MIT License | ||
# | ||
# Copyright (c) 2022 Ignacio Vizzo, Tiziano Guadagnino, Benedikt Mersch, Cyrill | ||
# Stachniss. | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
import os | ||
import sys | ||
from pathlib import Path | ||
|
||
import natsort | ||
import numpy as np | ||
|
||
from lidar_visualizer.datasets import supported_file_extensions | ||
|
||
|
||
class GenericDataset: | ||
def __init__(self, data_dir: Path, *_, **__): | ||
# Config stuff | ||
self.sequence_id = os.path.basename(data_dir) | ||
self.scans_dir = os.path.join(os.path.realpath(data_dir), "") | ||
self.scan_files = np.array( | ||
natsort.natsorted( | ||
[ | ||
os.path.join(self.scans_dir, fn) | ||
for fn in os.listdir(self.scans_dir) | ||
if any(fn.endswith(ext) for ext in supported_file_extensions()) | ||
] | ||
), | ||
dtype=str, | ||
) | ||
if len(self.scan_files) == 0: | ||
raise ValueError(f"Tried to read point cloud files in {self.scans_dir} but none found") | ||
self.file_extension = self.scan_files[0].split(".")[-1] | ||
if self.file_extension not in supported_file_extensions(): | ||
raise ValueError(f"Supported formats are: {supported_file_extensions()}") | ||
|
||
# Obtain the pointcloud reader for the given data folder | ||
self._read_point_cloud = self._get_point_cloud_reader() | ||
|
||
def __len__(self): | ||
return len(self.scan_files) | ||
|
||
def __getitem__(self, idx): | ||
return self.read_point_cloud(self.scan_files[idx]) | ||
|
||
def read_point_cloud(self, file_path: str): | ||
points = self._read_point_cloud(file_path) | ||
return points.astype(np.float64) | ||
|
||
def _get_point_cloud_reader(self): | ||
"""Attempt to guess with try/catch blocks which is the best point cloud reader to use for | ||
the given dataset folder. Supported readers so far are: | ||
- np.fromfile | ||
- trimesh.load | ||
- PyntCloud | ||
- open3d[optional] | ||
""" | ||
# This is easy, the old KITTI format | ||
if self.file_extension == "bin": | ||
print("[WARNING] Reading .bin files, the only format supported is the KITTI format") | ||
return lambda file: np.fromfile(file, dtype=np.float32).reshape((-1, 4))[:, :3] | ||
|
||
first_scan_file = self.scan_files[0] | ||
|
||
tried_libraries = [] | ||
missing_libraries = [] | ||
# First with open3d | ||
try: | ||
import open3d as o3d | ||
|
||
o3d.io.read_point_cloud(first_scan_file) | ||
return lambda file: np.asarray(o3d.io.read_point_cloud(file).points, dtype=np.float64) | ||
except ModuleNotFoundError: | ||
missing_libraries.append("open3d") | ||
except: | ||
tried_libraries.append("open3d") | ||
|
||
# first try trimesh | ||
try: | ||
import trimesh | ||
|
||
trimesh.load(first_scan_file) | ||
return lambda file: np.asarray(trimesh.load(file).vertices) | ||
except ModuleNotFoundError: | ||
missing_libraries.append("trimesh") | ||
except: | ||
tried_libraries.append("trimesh") | ||
|
||
# then try pyntcloud | ||
try: | ||
from pyntcloud import PyntCloud | ||
|
||
PyntCloud.from_file(first_scan_file) | ||
return lambda file: PyntCloud.from_file(file).points[["x", "y", "z"]].to_numpy() | ||
except ModuleNotFoundError: | ||
missing_libraries.append("pyntcloud") | ||
except: | ||
tried_libraries.append("pyntcloud") | ||
|
||
# If reach this point means that none of the librares exist/could read the file | ||
if not tried_libraries: | ||
print( | ||
"No 3D library is insallted in your system. Install one of the following " | ||
"to read the pointclouds" | ||
) | ||
print("\n".join(missing_libraries)) | ||
else: | ||
print("[ERROR] File format not supported") | ||
|
||
print("Tried to load the point cloud with:") | ||
print("\n".join(tried_libraries)) | ||
print("Skipped libraries (not installed):") | ||
print("\n".join(missing_libraries)) | ||
sys.exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# MIT License | ||
# | ||
# Copyright (c) 2023 Ignacio Vizzo, Tiziano Guadagnino, Benedikt Mersch, Cyrill | ||
# Stachniss. | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining a copy | ||
# of this software and associated documentation files (the "Software"), to deal | ||
# in the Software without restriction, including without limitation the rights | ||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
# copies of the Software, and to permit persons to whom the Software is | ||
# furnished to do so, subject to the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be included in all | ||
# copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
# SOFTWARE. | ||
import os | ||
import sys | ||
|
||
|
||
class McapDataloader: | ||
def __init__(self, data_dir: str, topic: str, *_, **__): | ||
"""Standalone .mcap dataloader withouth any ROS distribution.""" | ||
# First try rosbags | ||
from lidar_visualizer.datasets.point_cloud2 import read_point_cloud | ||
|
||
# Then MCAP support | ||
try: | ||
from mcap.reader import make_reader | ||
from mcap_ros2.reader import read_ros2_messages | ||
except ModuleNotFoundError as e: | ||
raise ModuleNotFoundError( | ||
"mcap plugins not installed: 'pip install mcap-ros2-support'" | ||
) from e | ||
|
||
# we expect `data_dir` param to be a path to the .mcap file, so rename for clarity | ||
assert os.path.isfile(data_dir), "mcap dataloader expects an existing MCAP file" | ||
self.sequence_id = os.path.basename(data_dir).split(".")[0] | ||
mcap_file = str(data_dir) | ||
|
||
self.bag = make_reader(open(mcap_file, "rb")) | ||
self.summary = self.bag.get_summary() | ||
self.topic = self.check_topic(topic) | ||
self.n_scans = self._get_n_scans() | ||
self.msgs = read_ros2_messages(mcap_file, topics=topic) | ||
self.read_point_cloud = read_point_cloud | ||
self.use_global_visualizer = True | ||
|
||
def __del__(self): | ||
if hasattr(self, "bag"): | ||
del self.bag | ||
|
||
def __getitem__(self, idx): | ||
msg = next(self.msgs).ros_msg | ||
return self.read_point_cloud(msg) | ||
|
||
def __len__(self): | ||
return self.n_scans | ||
|
||
def _get_n_scans(self) -> int: | ||
return sum( | ||
count | ||
for (id, count) in self.summary.statistics.channel_message_counts.items() | ||
if self.summary.channels[id].topic == self.topic | ||
) | ||
|
||
def check_topic(self, topic: str) -> str: | ||
# Extract schema id from the .mcap file that encodes the PointCloud2 msg | ||
schema_id = [ | ||
schema.id | ||
for schema in self.summary.schemas.values() | ||
if schema.name == "sensor_msgs/msg/PointCloud2" | ||
][0] | ||
|
||
point_cloud_topics = [ | ||
channel.topic | ||
for channel in self.summary.channels.values() | ||
if channel.schema_id == schema_id | ||
] | ||
|
||
def print_available_topics_and_exit(): | ||
print(50 * "-") | ||
for t in point_cloud_topics: | ||
print(f"--topic {t}") | ||
print(50 * "-") | ||
sys.exit(1) | ||
|
||
if topic and topic in point_cloud_topics: | ||
return topic | ||
# when user specified the topic check that exists | ||
if topic and topic not in point_cloud_topics: | ||
print( | ||
f'[ERROR] Dataset does not containg any msg with the topic name "{topic}". ' | ||
"Please select one of the following topics with the --topic flag" | ||
) | ||
print_available_topics_and_exit() | ||
if len(point_cloud_topics) > 1: | ||
print( | ||
"Multiple sensor_msgs/msg/PointCloud2 topics available." | ||
"Please select one of the following topics with the --topic flag" | ||
) | ||
print_available_topics_and_exit() | ||
|
||
if len(point_cloud_topics) == 0: | ||
print("[ERROR] Your dataset does not contain any sensor_msgs/msg/PointCloud2 topic") | ||
if len(point_cloud_topics) == 1: | ||
return point_cloud_topics[0] |
Oops, something went wrong.