Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

implement save/load pointcloud

edgarriba7a295049daeb
diff --git a/docs/source/utils.rst b/docs/source/utils.rst
index 9ae8b465b..1ce842e56 100644
--- a/docs/source/utils.rst
+++ b/docs/source/utils.rst
@@ -6,6 +6,8 @@ kornia.utils
.. autofunction:: tensor_to_image
.. autofunction:: image_to_tensor
.. autofunction:: create_meshgrid
+.. autofunction:: save_pointcloud_ply
+.. autofunction:: load_pointcloud_ply
.. autofunction:: one_hot
.. toctree::
diff --git a/kornia/__init__.py b/kornia/__init__.py
index 4410dce8e..396132342 100644
--- a/kornia/__init__.py
+++ b/kornia/__init__.py
@@ -56,5 +56,7 @@ from kornia.utils import (
create_meshgrid,
tensor_to_image,
image_to_tensor,
+ save_pointcloud_ply,
+ load_pointcloud_ply,
)
from kornia.geometry import *
diff --git a/kornia/utils/__init__.py b/kornia/utils/__init__.py
index 79be95c17..0f9f9a911 100644
--- a/kornia/utils/__init__.py
+++ b/kornia/utils/__init__.py
@@ -1,6 +1,7 @@
from .one_hot import one_hot
from .grid import create_meshgrid
from .image import tensor_to_image, image_to_tensor
+from .pointcloud_io import save_pointcloud_ply, load_pointcloud_ply
from kornia.utils.metrics import *
@@ -9,4 +10,6 @@ __all__ = [
"create_meshgrid",
"tensor_to_image",
"image_to_tensor",
+ "save_pointcloud_ply",
+ "load_pointcloud_ply",
]
diff --git a/kornia/utils/pointcloud_io.py b/kornia/utils/pointcloud_io.py
new file mode 100644
index 000000000..8f90e2b7e
--- /dev/null
+++ b/kornia/utils/pointcloud_io.py
@@ -0,0 +1,90 @@
+from typing import Optional
+
+import os
+import torch
+
+
+def save_pointcloud_ply(filename: str, pointcloud: torch.Tensor) -> None:
+ r"""Utility function to save to disk a pointcloud in PLY format.
+
+ Args:
+ filename (str): the path to save the pointcloud.
+ pointcloud (torch.Tensor): tensor containing the pointcloud to save.
+ The tensor must be in the shape of :math:`(*, 3)` where the last
+ component is assumed to be a 3d point coordinate :math:`(X, Y, Z)`.
+ """
+ if not isinstance(filename, str) and filename[-3:] == '.ply':
+ raise TypeError("Input filename must be a string in with the .ply "
+ "extension. Got {}".format(filename))
+ if not torch.is_tensor(pointcloud):
+ raise TypeError("Input pointcloud type is not a torch.Tensor. Got {}"
+ .format(type(pointcloud)))
+ if not len(pointcloud.shape) == 3 and pointcloud.shape[-1] == 3:
+ raise TypeError("Input pointcloud must be in the following shape "
+ "HxWx3. Got {}.".format(pointcloud.shape))
+ # flatten the input pointcloud in a vector to iterate points
+ xyz_vec: torch.Tensor = pointcloud.reshape(-1, 3)
+
+ with open(filename, 'w') as f:
+ data_str: str = ''
+ num_points: int = xyz_vec.shape[0]
+ for idx in range(num_points):
+ xyz = xyz_vec[idx]
+ if not bool(torch.isfinite(xyz).any()):
+ continue
+ x: float = xyz[0].item()
+ y: float = xyz[1].item()
+ z: float = xyz[2].item()
+ data_str += '{0} {1} {2}\n'.format(x, y, z)
+
+ f.write("ply\n")
+ f.write("format ascii 1.0\n")
+ f.write("comment arraiy generated\n")
+ f.write("element vertex %d\n" % num_points)
+ f.write("property double x\n")
+ f.write("property double y\n")
+ f.write("property double z\n")
+ f.write("end_header\n")
+ f.write(data_str)
+
+
+def load_pointcloud_ply(
+ filename: str, header_size: Optional[int] = 8) -> torch.Tensor:
+ r"""Utility function to load from disk a pointcloud in PLY format.
+
+ Args:
+ filename (str): the path to the pointcloud.
+ header_size (Optional[int]): the size of the ply file header that will
+ be skipped during loading. Default is 8 lines.
+
+ Return:
+ torch.Tensor: a tensor containing the loaded point with shape
+ :math:`(*, 3)` where :math:`*` represents the number of points.
+ """
+ if not isinstance(filename, str) and filename[-3:] == '.ply':
+ raise TypeError("Input filename must be a string in with the .ply "
+ "extension. Got {}".format(filename))
+ if not os.path.isfile(filename):
+ raise ValueError("Input filename is not an existing file.")
+ if not (isinstance(header_size, int) and header_size > 0):
+ raise TypeError("Input header_size must be a positive integer. Got {}."
+ .format(header_size))
+ # open the file and populate tensor
+ with open(filename, 'r') as f:
+ points = []
+
+ # skip header
+ lines = f.readlines()[header_size:]
+
+ # iterate over the points
+ for line in lines:
+ x_str, y_str, z_str = line.split()
+ points.append((
+ torch.tensor(float(x_str)),
+ torch.tensor(float(y_str)),
+ torch.tensor(float(z_str)),
+ ))
+
+ # create tensor from list
+ pointcloud: torch.Tensor = torch.tensor(points)
+ return pointcloud
diff --git a/setup_travis_env.sh b/setup_travis_env.sh
index ea39e8ba7..be810f55e 100755
--- a/setup_travis_env.sh
+++ b/setup_travis_env.sh
@@ -19,7 +19,7 @@ fi
# Install CPU-PyTorch
$sdk_dir/.dev_env/bin/conda install -y \
- pytorch-nightly \
+ pytorch-nightly-cpu \
-c pytorch
# Tests dependencies
diff --git a/test/utils/test_pointcloud_io.py b/test/utils/test_pointcloud_io.py
new file mode 100644
index 000000000..5ca8ed668
--- /dev/null
+++ b/test/utils/test_pointcloud_io.py
@@ -0,0 +1,24 @@
+import pytest
+import os
+
+import kornia
+import torch
+from torch.testing import assert_allclose
+
+
+class TestSaveLoadPointCloud:
+ def test_save_pointcloud(self):
+ height, width = 10, 8
+ xyz_save = torch.rand(height, width, 3)
+
+ # save to file
+ filename = "pointcloud.ply"
+ kornia.save_pointcloud_ply(filename, xyz_save)
+
+ # load file
+ xyz_load = kornia.load_pointcloud_ply(filename)
+ assert_allclose(xyz_save.reshape(-1, 3), xyz_load)
+
+ # remove the temporal file
+ if os.path.exists(filename):
+ os.remove(filename)