- 使用cmake编译安装pybind11:
cd pybind11
mkdir build
cd build
cmake ..
make install
- 使用cmake编译原本的c++文件,CMakeLists.txt如下:
cmake_minimum_required(VERSION 3.4)
project(skyeyec_cpp)
find_package(pybind11 REQUIRED)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
pybind11_add_module(skyeye_count test_sky.cpp src/Hungarian.cpp src/logger.cpp)
target_link_directories(skyeye_count PUBLIC
/usr/local/cuda/lib64
/home/xmj/.TensorRT-8.0.1.6/lib
)
target_link_libraries(skyeye_count PUBLIC
opencv_core
pthread
)
target_link_libraries(skyeye_count PUBLIC
nvinfer
nvparsers
nvinfer_plugin
nvonnxparser
)
target_include_directories(skyeye_count PUBLIC
/usr/include/opencv4
/usr/local/include
src
.TensorRT-8.0.1.6/include)
- c++代码中加入python的模块调用接口:
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <cstdlib>
#include <fstream>
#include "buffers.h"
#include "NvInfer.h"
#include <cuda_runtime_api.h>
#include "Hungarian.h"
#include <LightGBM/c_api.h>
#include <algorithm>
#include "opencv2/opencv.hpp"
int proc_one_video(string output_dir, string mask_filename, string video_filename, string detect_modelf, string weight_modelf, string color_modelf, bool save_result=true, bool need_weight=true){
return 0;
}
template <typename T>
T square(T x){
return x*x;
};
PYBIND11_MODULE(skyeye_count, m){
m.doc() = "run one video count";
m.def("square", &square<double>);
m.def("square", &square<float>);
m.def("proc_one_video", &proc_one_video);
};
- 编译运行,生成so库:
mkdir build
cd build
cmake ..
make
- 使用python调用so库:
import sys
sys.path.append('.')
import skyeye_count
print(skyeye_count.square(2))
print(skyeye_count.proc_one_video("f",
"e.png",
"d.mp4",
"c.engine",
"b.engine",
"a.txt",
True,True))
- 调用python文件时遇到undefined symbol问题,原因是so库依赖有问题,同一个库在多个文件夹下都有且版本不同,解决方法:
删除一个文件夹下的库,例如我删除了anaconda后装的so库,使用系统文件夹里自带的库。