如何在 C 回调中使用 Pybind11 访问 Python 对象?

How is it possible to access a Python object using Pybind11 in a C callback?

我有一个使用 Pybin11 嵌入 python 解释器的 C++ DLL。项目的 Python 端接受回调并将一些参数传递给它们。然后在 C++/C/C# 等部分执行回调。在 C++ 方面,我可以自由使用 pybind11::object 可以处理复杂类型,例如 OpenCV 图像(例如使用 py::array_t);然而,当涉及到将它暴露给 C,然后将其用于包括 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 中的对象指针?有可能吗?

这似乎是一个 Pybind11 错误并且在 Pybind11 上创建问题没有帮助,所以我最终使用了委托回调。那就是使用中间回调在两种格式之间进行转换。我在 .

中详细解释了这一点