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

利用pybind11在python中使用C++

編輯:Python

1、pybind的安裝

1.1、安裝依賴

sudo apt-get install python-dev (or python3-dev)
sudo apt-get install cmake
sudo pip install pytest
sudo pip install numpy
sudo pip install scipy

1.2、pip安裝

 sudo pip install pybind11

1.3、源碼安裝

  • 1、創建目錄
  • 2、獲取pybind11源碼包 https://github.com/pybind/pybind11
  • 3、下載Eigen http://eigen.tuxfamily.org/index.php?title=Main_Page
  • 4、在目錄執行 mkdir build
  • 5 、cd build
  • 6、cmake ..
  • 7、make check -j 4
  • 8、 sudo make install

1.4、使用brew 安裝

brew install pybind11

2、使用pybind進行C++代碼編寫


這裡以ROS為列,獲取Baxter機器人上方的圖片,為了簡潔方便看,這裡只放頭文件和cmake文件


2.1、頭文件

  • rosBase.hpp
#ifndef _ROS_H_
#define _ROS_H_
#include<ros/ros.h>
#include <std_msgs/Float64.h>
#include<string.h>
#include<robotstates_su/robotstates.h>
class rosBase{

public:
rosBase(const std::string node_name,int argc=0,char**argv=nullptr);
};
#endif
  • baxterImage.hpp
#include<rosBase.hpp>
#include<image_transport/image_transport.h>
#include<cv_bridge/cv_bridge.h>
#include<sensor_msgs/image_encodings.h>
#include <opencv2/opencv.hpp>
#include "opencv2/core/core.hpp"
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv/cv.h>
#include<zeromqConnect.hpp>
#include<vector>
#include<Python.h>
#include<pybind11/embed.h> 
static const std::string OPENCV_WINDOW = "Open-CV display window";
#define headImage "/cameras/head_camera/image"
#define leftImage "/cameras/left_hand_camera/image"
#define rightImage "/cameras/right_hand_camera/image"
class baxterImage:public rosBase{

public:
baxterImage(const std::string nodeName, std::string IP="*",int Port=5556,int rate=1000);
void getImage(const std::string cameraName);
void processImageCallBack(const sensor_msgs::ImageConstPtr& msg);
bool baxterOk();
void baxterSleep();
void baxterRate(int rate);
void baxterSpinOnce();
private:
ros::NodeHandle nh;
ros::Subscriber headSub;
ros::Subscriber leftSub;
ros::Subscriber rigthSub;
void *publisher=NULL;
int rate;
const std::string Port;
const std::string IP;
rosBase rosbase;
};

2.2、下面是一個重要的文件 bindings.cpp

用於把C++代碼和python代碼進行綁定

#include<pybind11/pybind11.h>
#include<baxterImage.hpp>
#include<pybind11/stl.h>
#include<pybind11/eigen.h>
namespace py = pybind11;
PYBIND11_MODULE(baxter,m)
{

py::class_<rosBase>rosbase(m,"rosBase");
rosbase.def(py::init<const std::string&>());
py::class_<baxterImage>(m,"baxterImage",rosbase)
.def(py::init<const std::string &,const std::string&,int ,int>())
.def("getImage",&baxterImage::getImage)
.def("baxterOk",&baxterImage::baxterOk)
.def("baxterSleep",&baxterImage::baxterSleep)
.def("baxterSpinOnce",&baxterImage::baxterSpinOnce)
.def("baxterRate",&baxterImage::baxterRate);
}

可以對著下面圖像進行理解

2.3、Cmake文件

cmake_minimum_required(VERSION 2.8.3)
project(Ros_control)
## Compile as C++11, supported in ROS Kinetic and newer
add_compile_options(-std=c++11)
find_package( OpenCV REQUIRED)
find_package(Eigen3 REQUIRED)
include_directories(
include
"/opt/ros/kinetic/include/"
"/usr/include/"
"/usr/include/boost/"
"/usr/lib/python3.7/dist-packages/numpy/core/include/"
"/usr/include/python3.7m/"
"/headless/baxter_ws/devel/include/"
)
LINK_DIRECTORIES(
"/opt/ros/kinetic/lib/"
"/usr/lib/x86_64-linux-gnu/"
"/opt/ros/kinetic/lib/x86_64-linux-gnu/"
"/lib64/"
)
include_directories(SYSTEM ${
EIGEN3_INCLUDE_DIRS})
add_subdirectory(
lib/pybind11
)
pybind11_add_module(baxter "src/rosBase.cpp" "src/baxterImage.cpp" "src/bindings.cpp")
target_link_libraries(
baxter PRIVATE ${
OpenCV_LIBS}
)

可以把pybind11 的lib庫拷入到編譯目錄的lib庫中(不是必須,能找到對應的庫就行)
編譯時會根據python版本生成baxter.cpython-37m-x86_64-linux-gnu.so文件

3、使用

把生成的so文件放到python文件使用的地方,或者讓使用的文件能夠找到此so文件

3.1、使用的案例代碼

import baxter
#push = baxter.baxterImage("pushimage","*",6666,1000)
push = baxter.baxterImage("pushimage","192.168.1.105",6666,1000)
push.getImage("h")
while push.baxterOk():
push.baxterSpinOnce()

參考學習


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