python - 在 pybind11 中使用 opencv 类型
问题描述
我想使用 pybind11 为使用 opencv 类型的函数创建一个包装器。
这是我的尝试:
例子.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);
};
例子.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
我能够通过cv::Rect
技巧将静态函数与opencv类型绑定(参见func4),但是如何绑定非静态函数,即func2?以及如何确保MyClass
调用构造函数?
解决方案
这非常有用。
我今天尝试C++
用 Python 调用 API 。OpenCV
并遇到了错误消息:
Expected in: flat namespace.
按照我在中添加PRIVATE的代码target_link_libraries
,该错误已解决。
推荐阅读
- firebase - 使用 Firestore 在聊天应用中加载对话消息
- python - 试图读取 BSON 文件,得到 bson.errors.InvalidBSON: objsize too large
- odoo - Odoo 方法覆盖
- arraylist - 多维超大数组
- python - 为什么我的数据库会话提交不起作用?
- ios - 目标c如何在ios中获取特定的json键值
- java - 从可执行jar创建可执行文件时如何添加依赖库?
- maven - 如何在 build.sbt 中提供自定义本地 maven 存储库
- c# - 如何从登录页面的下拉组合框中选择 sql 数据库服务器地址,而不是在 C# 中进行硬编码?
- angularjs - 我无法在我的保险丝主题 Angularjs 应用程序中翻译导航菜单中的标题