使用pybind11封装pcl的io接口
1、编译
interface.h
#include <iostream>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <pybind11/stl_bind.h>
#include <pcl/point_types.h>
#include <pcl/point_cloud.h>
#include <pcl/io/pcd_io.h>
#include "point_utils.h"
namespace py = pybind11;
py::array_t<float> LoadPCDWithLabel(const std::string& pcd_file);
interface.cpp
#include "interface.h"
#include <iostream>
// #include "interface_read_pcd.h"
// using suyunzheng_pcd_io;
// PYBIND11_MODULE(interface, m) {
// // optional module docstring
// m.doc() = "pcd io interface";
// // expose add function, and add keyword arguments and default arguments
// m.def("LoadPCDWithLabel", &LoadPCDWithLabel, "A function which load pcd xyz rgb ilf", py::arg("pcd_file")="test.pcd" );
// }
/*
* 读取pcd文件,x y z r g b intensity label frameindex
*/
py::array_t<float> LoadPCDWithLabel(const std::string& pcd_file){
adt::hdmap::CloudXYZRGBILFPtr cloud_ptr(new adt::hdmap::CloudXYZRGBILF);
if(pcl::io::loadPCDFile(pcd_file, *cloud_ptr)!=0){
std::cerr << "***Can't load pcd file from: " << pcd_file << std::endl;
py::array_t<float> _points = py::array_t<float>(8192*7);
_points.resize({ssize_t(8192), ssize_t(7)});
return _points;
}
float _point_num = cloud_ptr->size();
py::array_t<float> _points = py::array_t<float>(_point_num * 7);
_points.resize({ssize_t(_point_num), ssize_t(7)});
auto mat = _points.mutable_unchecked<2>();
for(int i = 0; i<cloud_ptr->size(); i++){
mat(i, 0) = cloud_ptr->points[i].x;
mat(i, 1) = cloud_ptr->points[i].y;
mat(i, 2) = cloud_ptr->points[i].z;
mat(i, 3) = static_cast<float>(cloud_ptr->points[i].r);
mat(i, 4) = static_cast<float>(cloud_ptr->points[i].g);
mat(i, 5) = static_cast<float>(cloud_ptr->points[i].b);
mat(i, 6) = cloud_ptr->points[i].label;
}
// std::cerr << "points num: " << cloud_ptr->size() << std::endl;
// std::cerr << "***Load succeed " << pcd_file << std::endl;
return _points;
}
pcl_io_wrap.cpp
#include <iostream>
#include "interface.h"
// using suyunzheng_pcd_io;
PYBIND11_MODULE(interface, m) {
// optional module docstring
m.doc() = "pcd io interface";
// expose add function, and add keyword arguments and default arguments
m.def("LoadPCDWithLabel", &LoadPCDWithLabel, "A function which load pcd xyz rgb ilf", py::arg("pcd_file")="test.pcd" );
}
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.12)
project(interface)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
# set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories(/usr/include/pcl-1.7 )
file(GLOB PCL_LIBRARIES "/usr/lib/x86_64-linux-gnu/libpcl*.so*")
message("\n---------------------")
message(${PCL_LIBRARIES})
message("---------------------\n")
find_package(Boost REQUIRED filesystem thread)
include_directories(${Boost_INCLUDE_DIRS})
message("\n------------------")
message(${Boost_LIBRARIES})
message("------------------\n")
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIR})
# find_package(yaml-cpp REQUIRED)
include_directories("/usr/local/include/yaml-cpp")
file(GLOB YAML_CPP_LIBRARIES "/usr/local/lib/libyaml-cpp.so")
message("\n------------------")
message(${YAML_CPP_LIBRARIES})
message("------------------\n")
# set(COMMON_INCLUDE "./common/include")
# set(COMMON_SRC "./common/src")
# include_directories(
# ${COMMON_INCLUDE}
# ./include/
# )
set(COMMON_INCLUDE "common/include")
set(COMMON_SRC "common/src")
include_directories(
${COMMON_INCLUDE}
./include/
)
find_package(pybind11 REQUIRED)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib)
# pybind11_add_module(example example.cpp)
# 名字要对应 pybind11_add_module
# pybind11_add_module(interface
# ${COMMON_SRC}/eigen_utils.cpp ${COMMON_SRC}/point_utils.cpp ${COMMON_SRC}/system_utils.cpp
# src/pcd_io_wrap.cpp
# src/interface.cpp
# )
pybind11_add_module(interface
src/pcd_io_wrap.cpp
src/interface.cpp
${COMMON_SRC}/eigen_utils.cpp ${COMMON_SRC}/point_utils.cpp ${COMMON_SRC}/system_utils.cpp)
target_link_libraries(interface PRIVATE
${PCL_LIBRARIES}
${Boost_LIBRARIES}
${YAML_CPP_LIBRARIES}
# ${OpenCV_LIBS}
)
# cmake_minimum_required(VERSION 2.8.12)
# project(example)
# set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib)
# find_package(pybind11 REQUIRED)
# # add_subdirectory(pybind11)
# pybind11_add_module(example example.cpp)
2、使用
import sys
# sys.path.append("./../lib")
sys.path.append("interface/")
from os.path import join
import numpy as np
import random, os, sys
import os, argparse, pickle
# import lib.interface as interface
import lib.interface as interface
from pypcd import pypcd
# print("pypcd.version:{}".format(pypcd.__version__))
# cloud = example.add(1,3)
file_path = "/media/s/zhouzixiang@baidu.com/luweixin/L3_New_City_32E/69585/69585_2_250.pcd"
cloud = interface.LoadPCDWithLabel(file_path)
print("***{}".format(cloud.shape))
print(cloud)