.ply 到 .bin(velodyne) 转换

问题描述 投票:0回答:1

我正在研究 3D 对象检测,我有 .ply 形式的点云数据。我遇到了很多 github 存储库,其中大多数都使用 KITTI 数据集。

我尝试使用 PointPillars 算法进行 3D 对象检测,但它仅接受 .bin(velodyne) 形式的点云数据。有没有办法将 .ply 转换为 .bin(velodyne) 或将 .ply 文件作为 PointPillars 的输入?

3d computer-vision geometry object-detection point-clouds
1个回答
0
投票

要将

.ply
点云文件转换为
.bin
格式(PointPillars 所需),您可以使用以下 Python 代码。它允许您加载
.ply
文件,将其转换为二进制格式,并使用
.bin
扩展名保存它们。这应该使您的点云数据与 PointPillars 等算法兼容。

import os
from typing import Union
import numpy as np
import open3d as o3d
from glob import glob
from open3d.geometry import PointCloud

def make_directory(directory_path: str) -> None:
    """Creates a directory if it does not exist."""
    if not os.path.exists(directory_path):
        os.makedirs(directory_path)

def load_point_cloud(file_or_directory_path: str, file_extension: str = 'ply') -> Union[np.ndarray, PointCloud]:
    """
    Loads a point cloud file or all point cloud files in a directory.

    Args:
        file_or_directory_path (str): Path to a point cloud file or directory.
        file_extension (str): Extension of the point cloud files (default is 'ply').

    Returns:
        Union[np.ndarray, PointCloud]: The loaded point cloud(s).
    """
    if os.path.isdir(file_or_directory_path):
        point_clouds = []
        point_cloud_files = glob(f"{file_or_directory_path}/*.{file_extension}")
        for file_path in point_cloud_files:
            point_cloud = o3d.io.read_point_cloud(file_path, format=file_extension)
            point_clouds.append(point_cloud)
        return np.array(point_clouds)
    
    elif os.path.isfile(file_or_directory_path):
        return o3d.io.read_point_cloud(file_or_directory_path, format=file_extension)

def convert_point_cloud_to_bin(point_cloud: PointCloud, output_file_path: str, file_extension: str = 'ply') -> None:
    """
    Converts a point cloud to binary format (.bin).

    Args:
        point_cloud (Union[str, PointCloud, np.ndarray]): The point cloud or its path.
        output_file_path (str): Path for the output .bin file.
    """
    if isinstance(point_cloud, str):
        point_cloud = load_point_cloud(file_or_directory_path=point_cloud, file_extension=file_extension)
    
    output_file_path += "" if output_file_path.endswith(".bin") else ".bin"
    np.asarray(point_cloud.points).astype('float32').tofile(output_file_path)

def batch_convert_point_clouds_to_bin(input_directory_path: str, output_directory_path: str, file_extension: str = 'ply') -> None:
    """
    Converts all point clouds in a directory from their current format to .bin format.

    Args:
        input_directory_path (str): Path to the directory containing point cloud files.
        output_directory_path (str): Path to the directory where .bin files will be saved.

    Raises:
        ValueError: If input_directory_path is neither a file nor a directory.
    """
    if not os.path.isdir(output_directory_path):
        make_directory(output_directory_path)

    if os.path.isfile(input_directory_path):
        file_name_without_extension = os.path.basename(input_directory_path).split('.')[0]
        output_file_path = os.path.join(output_directory_path, file_name_without_extension)
        convert_point_cloud_to_bin(point_cloud=input_directory_path, output_file_path=output_file_path)
    
    elif os.path.isdir(input_directory_path):
        point_cloud_files = glob(f"{input_directory_path}/*.{file_extension}")
        for file_path in point_cloud_files:
            file_name_without_extension = os.path.basename(file_path).split('.')[0]
            output_file_path = os.path.join(output_directory_path, file_name_without_extension)
            convert_point_cloud_to_bin(point_cloud=file_path, output_file_path=output_file_path)
    
    else:
        raise ValueError(f"'{input_directory_path}' is neither a file nor a directory.")

使用方法:

  • 单个
    .ply
    文件:
    使用
    convert_point_cloud_to_bin
    文件的路径和所需的输出路径调用
    .ply
  • 批量转换:使用
    batch_convert_point_clouds_to_bin
    转换目录中的所有
    .ply
    文件。

此代码应帮助您将

.ply
文件转换为 KITTI 数据集使用的
.bin
格式,使它们与 PointPillars 兼容。


我修改了这里的代码:https://gist.github.com/AlexPasqua/f34994a0b0f8ee33f8730d6e660d55b5#file-pointclouds_pcd_to_bin-py

© www.soinside.com 2019 - 2024. All rights reserved.