程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python calls pybind11 of c++

編輯:Python

Has been engaged in c++ Related algorithm and code related work , Due to the need of internal code management , The algorithm needs to be encapsulated for python Platform use , According to this demand , Yes python call c++ Code way to learn , Finally, consider the difficulty of encapsulation and the use of multi code management pybind11 The realization of related functions .

pybind11 Is a c++ And python Libraries for mutual calls and data interaction

Take my own algorithm as an example to introduce pybind11 Basic use of , My call algorithm also includes other c++ library , for example opencv, There is also an unexpected bug Introduction to the follow-up meeting .

Windows System

Requires

win10,64bit

Visual Studio2015

python3.6(Anaconda)

pybind11 install

download pybind11 Source code , Get its header file , Download address :https://github.com/pybind/pybind11

Because it is Head-only Formal , There is no need to compile dynamic libraries , Use it directly include that will do .

demo Application testing

1、 establish Vistual Studio engineering , Will need to call c++ Code into it

Set the project type and output file type , Respectively .pyd And dll

2、 add to pybind11 and python Relevant header file path and library file and its path , Compiled into the library

The header file settings are as follows :

The library file directory and attachment library are set as follows :

Need to use pybind11 The format of , Encapsulate functions and structures ,c++ The format of the encapsulated header file and source file is as follows :

Passenger_pyd.h

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
struct outdata
{
int in;
int out;
};
int test( Mat frame);
outdata* video_test(char* file_path, int channel, int inNum, int outNum);

Passenger_pyd.cpp

#include<pybind11/pybind11.h>
namespace py = pybind11;
PYBIND11_MODULE(Passenger_pyd, m) {
m.doc() = "pybind11 example module";
py::class_<outdata>(m, "outdata")
.def_readonly("in_num", &outdata::in)
.def_readonly("out_num", &outdata::out);
// Add bindings here
m.def("Testframe", &test, "Test frame");
m.def("DetectionTrack", &video_test, "Detection and Track");

Compile the generated .pyd and lib

3、python End (pycharm) The call format is as follows :

import Passenger_pyd
if __name__ == '__main__':
innum=0
outnum=0
ichn=0
jpeg_file="e:\\1.jpeg"
frame=cv2.imread(jpeg_file)
outdata=Passenger_pyd.DetectionTrack(jpeg_file,ichn,innum,outnum)
print(outdata.in_num,outdata.out_num)
#Passenger_pyd.Testframe(frame)

Some problems in the testing process

1、 Need to be Be careful PYBIND11_MODULE Of name It needs to be consistent with the library file name , Otherwise, the following error will occur :

 import Passenger_pyd
ImportError: dynamic module does not define module export function (PyInit_Passenger_pyd)

2、 this demo Has tried to opencv Of Mat Passed in as a parameter to c++ In the code , That is, the following line commented out

 #Passenger_pyd.Testframe(frame)

because pybind11 I won't support it opencv Of Mat Class will report the following error :

Passenger_pyd.Testframe(frame)
TypeError: Testframe(): incompatible function arguments. The following argument types are supported:
1. (arg0: cv::Mat) -> int
Invoked with: array [[ ...,
[ 36, 39, 37],
[ 19, 22, 20],
[ 0, 1, 0]]], dtype=uint8)

There are two solutions to this situation , One is to pass in the file path c++ Use in imread obtain Mat, The other can be found in c++ End to side array Format , For relevant conversion, please refer to the link :https://github.com/edmBernard/pybind11_opencv_numpy, I didn't want to waste my energy on this. I directly chose the first approach .

3、c++ Return value problem , If there are many parameters to return , Data interaction can be carried out in the form of structure . As in this example outdata, The purpose is to return the structure to python in , It is more suitable for the situation where there are many returned parameters and the types are inconsistent .

Finally, I wish you progress every day !! Study Python The most important thing is mentality . We are bound to encounter many problems in the process of learning , Maybe you can't solve it if you want to break your head . It's all normal , Don't rush to deny yourself , Doubt yourself . If you have difficulties in learning at the beginning , Looking for one python Learning communication environment , You can join us , Get the learning materials 、 Discuss together .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved