在pybind11

时间:2019-02-14 08:23:59

标签: python c++ opencv pybind11

我想用pybind11为使用opencv类型的函数创建包装器。

这是我的尝试:

example.hpp

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

class MyClass
{
public:
    MyClass();

    void func1(cv::Rect &rc);
    cv::Rect func2(cv::Rect &rc);
    static void func3(cv::Rect &rc);
    static cv::Rect func4(cv::Rect &rc);
    void func5(int f);
};

example.cpp

#include "example.hpp"

#include <iostream>

    MyClass::MyClass()
    {
        std::cout << "MyClass::MyClass() called! " << std::endl;
    }

    void MyClass::func1(cv::Rect &rc)
    {

    }

    cv::Rect MyClass::func2(cv::Rect &rc)
    {
        cv::Rect rc2(10,20,30,40);
        return rc2;
    }

    void MyClass::func3(cv::Rect &rc)
    {

    }

    cv::Rect MyClass::func4(cv::Rect &rc)
    {
        cv::Rect rc2(10,20,30,40);
        return rc2;
    }


    void MyClass::func5(int f)
    {

    }

pybind11_example.cpp

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>

#include "example.hpp"

namespace py = pybind11;

PYBIND11_MODULE(pybind11_example, m) {
    py::class_<MyClass>(m, "MyClass")
    .def(py::init<>())
    .def("func1", &MyClass::func1, "some func1", py::arg("rc"))
    .def("func5", &MyClass::func5, "some func5", py::arg("f"));


    // Static function binding with passing custom type and return custom type
    m.def("func4", [](std::vector<int> &v) -> py::array {
        if (v.size()!=4)
        {
            throw std::runtime_error("v.size()!=4");
        }

        cv::Rect rc;
        cv::Rect rc2 = MyClass::func4(rc);
        std::vector<int> v2;
        v2.push_back(rc2.x);
        v2.push_back(rc2.y);
        v2.push_back(rc2.width);
        v2.push_back(rc2.height);
        return py::array(v2.size(), v2.data());
    });
}

CMakeLists.txt

project("pybind11_example")
cmake_minimum_required(VERSION 2.8)

find_package(PythonLibs)
execute_process(
    COMMAND "python3" "-c" "import os, numpy.distutils; print(os.pathsep.join(numpy.distutils.misc_util.get_numpy_include_dirs()))"
    RESULT_VARIABLE _numpy_process
    OUTPUT_VARIABLE _numpy_include_dirs
    OUTPUT_STRIP_TRAILING_WHITESPACE
)
if (NOT _numpy_process EQUAL 0)
    message(FATAL_ERROR "Can't locate numpy include dirs")
endif()
include_directories(${PYTHON_INCLUDE_DIRS} ${_numpy_include_dirs})
message("Python libs: ${PYTHON_LIBRARIES}")

find_package(pybind11 REQUIRED)
pybind11_add_module(pybind11_example pybind11_example.cpp example.cpp)
target_link_libraries(pybind11_example PRIVATE ${OpenCV_LIBS} ${Boost_LIBRARIES})

这是我的问题:

func5绑定有什么问题?当我执行from pybind11_example import MyClass,例如MyClass.func5(3)时,我得到了错误:

MyClass.func5(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: func5(): incompatible function arguments. The following argument types are supported:
    1. (self: pybind11_example.MyClass, f: int) -> None

Invoked with: 3

我可以用技巧将opencv类型为cv::Rect的静态函数绑定(请参阅func4),但是如何绑定非静态的函数,即func2?以及如何确保将调用MyClass构造函数?

2 个答案:

答案 0 :(得分:0)

这非常有用。

我今天尝试通过OpenCV用Python调用C ++ API。并遇到了错误消息Expected in: flat namespace

按照您的代码,在target_link_libraries中添加** PRIVATE **,此错误已解决。

答案 1 :(得分:0)

MyClass::func5不是静态成员函数。使其成为静态,或在调用它之前创建一个实例:

from pybind11_example import MyClass
m = MyClass()
m.func5(3)