Skip to content

Commit

Permalink
Nacho/dekissify (#3)
Browse files Browse the repository at this point in the history
* 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
nachovizzo authored Jan 18, 2024
1 parent b68fa7e commit 978ceb3
Show file tree
Hide file tree
Showing 11 changed files with 1,023 additions and 130 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Set up Python3
uses: actions/setup-python@v3
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ A flexible, easy-to-use, LiDAR (or any point cloud) visualizer for Linux, Window

If you also need to obtain poses from your dataset, consider checking out [KISS-ICP](https://github.com/PRBonn/kiss-icp).

## Install
## Install (\*)

```sh
pip install lidar-visualizer
```

Next, follow the instructions on how to run the system by typing:
(\*) This package relies on the power of [Open3D](https://www.open3d.org) but does not list it as a dependency. If you haven't installed `open3d` then `pip install open3d` or check [the official instructions](https://www.open3d.org/docs/release/getting_started.html)

## Optional dependencies

Depending on the [dataloaders](./src/lidar_visualizer/datasets/) you plan to use you might need to install optional dependencies. The tool will prompt which tools is the one you are requesting and is not accessible, but if you want to go for brute force and install all of it just run:

```sh
pip install lidar-visualizer[all]
```

## Usage

```sh
lidar_visualizer --help
Expand Down
18 changes: 16 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"

Expand Down
80 changes: 80 additions & 0 deletions src/lidar_visualizer/datasets/__init__.py
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)
131 changes: 131 additions & 0 deletions src/lidar_visualizer/datasets/generic.py
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)
113 changes: 113 additions & 0 deletions src/lidar_visualizer/datasets/mcap.py
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]
Loading

0 comments on commit 978ceb3

Please sign in to comment.