我有一个python,其中嵌入了使用Pybin11的C++解释器。项目的Python端接受回调并将一些参数传递给它们。然后在C++/C/C#等部分执行回调。在C++方面,我可以自由地使用pybind11::object可以处理复杂的类型,如OpenCV图像(例如使用py::array_t );但是,当涉及到将其暴露给C语言时,我失败了。
我失败的原因是,我不能简单地将py::object类型转换为C可以理解的类型,反之亦然。例如,我不能简单地更改我的C++回调:
typedef void(*CallbackFn)(bool, std::string, py::array_t<uint8_t>&);
void default_callback(bool status, std::string id, py::array_t<uint8_t>& img)
{
auto rows = img.shape(0);
auto cols = img.shape(1);
auto type = CV_8UC3;
cv::Mat img1(rows, cols, type, img.mutable_data());
...
}到它的C兼容变体:
typedef void(*CallbackFn)(bool, char*, void*);
void default_callback(bool status, char* id, void* img)
{
//cast the void* into something useful
...
}这只会在Python端引起一个异常,简单地说,
TypeError: (): incompatible function arguments. The following argument types are supported:
1. (arg0: bool, arg1: str, arg2: capsule) -> None
Invoked with: True, '5', array([[[195, 216, 239],
[194, 215, 237],
[193, 214, 236],
...,
[ 98, 108, 143],
[100, 110, 147],
[101, 111, 149]]], dtype=uint8)使用其他对象(例如,在opencv img.data上,我会得到如下错误:
TypeError: (): incompatible function arguments. The following argument types are supported:
1. (arg0: bool, arg1: str, arg2: capsule) -> None
Invoked with: True, '5', <memory at 0x0000021BC3433D68>所以我的问题是,我应该怎么做,这样我才不会在Python部分得到异常?如何在Python中访问对象指针?这有可能吗?
发布于 2020-04-18 11:28:28
这似乎是一个Pybind11错误,在Pybind11上创建一个问题没有帮助,所以我最终使用了一个委托回调。也就是说,使用中间回调在两种格式之间进行转换。我在我的previous (related) question中详细解释了这一点。
https://stackoverflow.com/questions/61156126
复制相似问题