OpenCV Mat to NvBufSurface (to use in NvBufSurfTransform)

I’m also trying to populate a NvBufSurface from a cv::Mat but for whatever reasons fail doing so.

I’m more or less copying the code you wear using here to achieve this:

NvBufSurface *inf_buf = nullptr;
NvBufSurfaceCreateParams create_params2;
create_params2.gpuId = 0;
create_params2.width = 112;
create_params2.height = 112;
create_params2.size = 0;
create_params2.colorFormat = NVBUF_COLOR_FORMAT_BGR;
create_params2.layout = NVBUF_LAYOUT_PITCH;
create_params2.memType = NVBUF_MEM_CUDA_UNIFIED;
if (NvBufSurfaceCreate(&inf_buf, 1, &create_params2) != 0) {
GST_ELEMENT_ERROR(nvinfer, STREAM, FAILED, (“Failed creating inf_buf”), (NULL));
return GST_FLOW_ERROR;
}

// cf.
// OpenCV Mat to NvBufSurface (to use in NvBufSurfTransform) - #13 by DaneLLL
inf_buf->numFilled = 1;

NvBufSurfaceMemSet(inf_buf, 0, 0, 0);

auto map_err = NvBufSurfaceMap(inf_buf, 0, 0, NVBUF_MAP_READ_WRITE);
if (map_err != 0) {
GST_ELEMENT_ERROR(nvinfer, STREAM, FAILED, (“Failed mapping buf inf_buf: %d\n”, map_err),
(NULL));
return GST_FLOW_ERROR;
}

if (NvBufSurfaceSyncForCpu(inf_buf, 0, 0) != 0) {
GST_ELEMENT_ERROR(nvinfer, STREAM, FAILED, (“Failed syncing inf_buf for CPU”), (NULL));
return GST_FLOW_ERROR;
}
memcpy(inf_buf->surfaceList[0].mappedAddr.addr[0], crop.ptr(), 112 * 112 * 3);

Unfortunately this fails for me at NvBufSurfaceSyncForCpu. When switching create_params2.memType to NVBUF_MEM_DEFAULT as you are using here NvBufSurfaceMap fails with nvbufsurface: mapping of memory type (0) not supported.

So the solution you provided does unfortunately not work in my case.