将PyObject gpumat转换为C++

2024-09-30 20:29:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用pybDun11创建一个绑定,以便将一个Python ^ {< CD1>}直接传递给C++代码。实际上,我找不到一种方法来将PyObject *转换为cv::cuda::GpuMat

torch::Tensor gpumat2torch(PyObject* frame) {
    cv::cuda::GpuMat frame_gpu;
    pyopencv_to(frame, frame_gpu); # <-- NOT FOUND
    at::ScalarType torch_type = get_torch_type(frame_gpu.type());
    auto options = torch::TensorOptions().dtype(torch_type).device(torch::kCUDA);
    std::vector<int64_t> sizes = {1,
                                  static_cast<int64_t>(frame_gpu.channels()),
                                  static_cast<int64_t>(frame_gpu.rows),
                                  static_cast<int64_t>(frame_gpu.cols)};
    return torch::from_blob(frame_gpu.data, sizes, options);
}
PYBIND11_MODULE(gpumat, m) {
    m.doc() = "gpumat2torch function!";
    m.def("gpumat2torch", &gpumat2torch, "A function to convert GpuMat to CudaTorch");
#ifdef VERSION_INFO
    m.attr("__version__") = VERSION_INFO;
#else
    m.attr("__version__") = "dev";
#endif

我发现在OpenCV中有一个要转换的方法,但似乎不是公共的:https://github.com/opencv/opencv/blob/d60ac98ef269f06c433450998bd71236f84ef504/modules/core/misc/python/pyopencv_cuda.hpp

有办法解决这个问题吗


Tags: to方法gputypestatictorchframecv