• Hardware Platform: GPU
• DeepStream Version: 5.0.0
• TensorRT Version: 7.0.0.11
• NVIDIA GPU Driver Version (valid for GPU only): 460.32.03
I edited the gst-nvinfer plugin to do some pre-processing before inference. I actually want to exchange the buffer for an inference on a detected bbox in an sgie. All that happens in gst_nvinfer_process_objects
just before calling get_converted_buffer
.
Here is what I’m doing:
- Create a copy of the actual buffer
- Create a cv::Mat from that buffer
- Do some postprocessing on that mat
- Create and init a NvBufSurface
- Copy cv::Mat content from 2 into NvBufSurface
I’m following this thread but unfortunately it does not work for me
Here’s my code:
NvBufSurface *nvbuf_copy = nullptr;
cv::Mat mat;
NvBufSurfaceCreateParams create_params;
create_params.gpuId = in_surf->gpuId;
create_params.width = in_surf->surfaceList[frame_meta->batch_id].width;
create_params.height = in_surf->surfaceList[frame_meta->batch_id].height;
create_params.size = 0;
create_params.colorFormat = NVBUF_COLOR_FORMAT_BGR;
create_params.layout = in_surf->surfaceList[frame_meta->batch_id].layout;
create_params.memType = NVBUF_MEM_CUDA_UNIFIED;
NvBufSurfaceCreate(&nvbuf_copy, 1, &create_params);NvBufSurfTransformParams transform_params;
NvBufSurfTransformRect src_rect = {0, 0, in_surf->surfaceList[frame_meta->batch_id].width,
in_surf->surfaceList[frame_meta->batch_id].height};
NvBufSurfTransformRect dst_rect = {0, 0, in_surf->surfaceList[frame_meta->batch_id].width,
in_surf->surfaceList[frame_meta->batch_id].height};transform_params.src_rect = &src_rect;
transform_params.dst_rect = &dst_rect;NvBufSurfTransform_Error err = NvBufSurfTransform(in_surf, nvbuf_copy, &transform_params);
if (err != NvBufSurfTransformError_Success) {
GST_ELEMENT_ERROR(nvinfer, STREAM, FAILED, (“Error for NvBufSurfTransform”),
(std::to_string(err).c_str()));
return GST_FLOW_ERROR;
}if (NvBufSurfaceMap(nvbuf_copy, 0, 0, NVBUF_MAP_READ) != 0) {
GST_ELEMENT_ERROR(nvinfer, STREAM, FAILED, (“Error mapping buffer”), (NULL));
return GST_FLOW_ERROR;
}NvBufSurfaceSyncForCpu(nvbuf_copy, 0, 0);
mat = cv::Mat(in_surf->surfaceList[frame_meta->batch_id].height,
in_surf->surfaceList[frame_meta->batch_id].width, CV_8UC3,
nvbuf_copy->surfaceList[frame_meta->batch_id].mappedAddr.addr[0],
nvbuf_copy->surfaceList[frame_meta->batch_id].pitch);cv::Mat transformation_matrix(2, 3, CV_32F);
get_transformation_matrix(vals, dst_vals, 5, transformation_matrix);
cv::Mat warped(mat.rows, mat.cols, CV_32F);
cv::warpAffine(mat, warped, transformation_matrix, warped.size());cv::Rect roi(0, 0, 112, 112);
cv::Mat crop = warped(roi);NvBufSurface *inf_buf = nullptr;
create_params.gpuId = in_surf->gpuId;
create_params.width = 112;
create_params.height = 112;
create_params.size = 0;
create_params.colorFormat = NVBUF_COLOR_FORMAT_BGR;
create_params.layout = NVBUF_LAYOUT_PITCH;
create_params.memType = NVBUF_MEM_CUDA_UNIFIED;
NvBufSurfaceCreate(&inf_buf, 1, &create_params);// cf.
// OpenCV Mat to NvBufSurface (to use in NvBufSurfTransform) - #13 by DaneLLL
inf_buf->numFilled = 1;NvBufSurfaceMemSet(inf_buf, 0, 0, 0);
if (NvBufSurfaceMap(inf_buf, 0, 0, NVBUF_MAP_READ_WRITE) != 0) {
GST_ELEMENT_ERROR(nvinfer, STREAM, FAILED, (“Failed mapping buf inf_buf”), (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);
Questions:
- Is there a way to copy the data without this intermediate nvbuf_copy? Meaning in this part
mat = cv::Mat(in_surf->surfaceList[frame_meta->batch_id].height,
in_surf->surfaceList[frame_meta->batch_id].width, CV_8UC3,
nvbuf_copy->surfaceList[frame_meta->batch_id].mappedAddr.addr[0],
nvbuf_copy->surfaceList[frame_meta->batch_id].pitch);
is there any way to copy directly from in_surf? The respective in_surf->surfaceList[frame_meta->batch_id].mappedAddr.addr[0]
was null in my case and mapping using a preceding NvBufSurfaceMap
did not work.
- How would I populate the
inf_buf
object with thecrop
Mat data? The suggested solution from the mentioned thread did not work asNvBufSurfaceSyncForCpu
returns -1.