minwoo0611 / HeLiPR-File-Player

This is the repository for HeLiPR file player.
https://sites.google.com/view/heliprdataset
40 stars 5 forks source link

How to read bin files through python? #5

Closed GedRelay closed 9 months ago

GedRelay commented 9 months ago

I tried pcd = np.fromfile(binpath, dtype=np.float32) and pcd = np.fromfile(binpath, dtype=np.float64) but it looks like the numbers aren't quite right

minwoo0611 commented 9 months ago

Hello @GedRelay,

It looks like you're encountering issues with reading binary files in Python. The direct approach using np.fromfile might not yield the correct results because our point cloud data follows a specific structure depending on the LiDAR type.

For example, here's how you can read a bin file for Ouster LiDAR in Python:


def read_bin_file(filename):
    points = []
    with open(filename, "rb") as file:
        while True:
            data = file.read(26)  # 26 bytes for one point
            if len(data) < 26:
                break  # End of file
            point = Point()
            point.x, point.y, point.z, point.intensity = struct.unpack('ffff', data[:16])
            point.t, = struct.unpack('I', data[16:20])
            point.reflectivity, point.ring, point.ambient = struct.unpack('HHH', data[20:26])
            points.append([point.x, point.y, point.z])  # Only x, y, z are needed for visualization
    return points

This code accounts for our specific point cloud structure. The structure details for different LiDAR types can be found in the HeLiPR File Player repository, specifically at: HeLiPR File Player Structure Definition.

Furthermore, if you need more advanced processing like undistortion, accumulation, and saving the binary file to a PCD file, please refer to our HeLiPR Pointcloud Toolbox repository: HeLiPR Pointcloud Toolbox.

Let me know if this helps or if you have any more questions.

GedRelay commented 9 months ago

Hello @GedRelay,

It looks like you're encountering issues with reading binary files in Python. The direct approach using np.fromfile might not yield the correct results because our point cloud data follows a specific structure depending on the LiDAR type.

For example, here's how you can read a bin file for Ouster LiDAR in Python:

def read_bin_file(filename):
    points = []
    with open(filename, "rb") as file:
        while True:
            data = file.read(26)  # 26 bytes for one point
            if len(data) < 26:
                break  # End of file
            point = Point()
            point.x, point.y, point.z, point.intensity = struct.unpack('ffff', data[:16])
            point.t, = struct.unpack('I', data[16:20])
            point.reflectivity, point.ring, point.ambient = struct.unpack('HHH', data[20:26])
            points.append([point.x, point.y, point.z])  # Only x, y, z are needed for visualization
    return points

This code accounts for our specific point cloud structure. The structure details for different LiDAR types can be found in the HeLiPR File Player repository, specifically at: HeLiPR File Player Structure Definition.

Furthermore, if you need more advanced processing like undistortion, accumulation, and saving the binary file to a PCD file, please refer to our HeLiPR Pointcloud Toolbox repository: HeLiPR Pointcloud Toolbox.

Let me know if this helps or if you have any more questions.

I've learned how to read binaries generated by different devices by looking at the source code of HeLiPR Pointcloud Toolbox and I've managed to read them using python. Thank you very much for your answer!