I wanted to have the crop which is being fed into SGIE. So what I did is the following:
In gstnvinfer.cpp
’s gst_nvinfer_process_objects
method, after get_converted_buffer
is called, as I wanted to make use of the actual used transformation parameters, I added the following code:
//create temporary buffer
NvBufSurface *nvbuf;
cv::Mat mat;
//create_params used to setup temporary buffer
NvBufSurfaceCreateParams create_params;
create_params.gpuId = 0;
create_params.width = nvinfer->transform_params.dst_rect[0].width;
create_params.height = nvinfer->transform_params.dst_rect[0].height;
create_params.size = 0;
create_params.colorFormat = NVBUF_COLOR_FORMAT_GRAY8; //my sgie model takes gray input
create_params.layout = NVBUF_LAYOUT_PITCH;
create_params.memType = NVBUF_MEM_CUDA_UNIFIED;
NvBufSurfaceCreate(&nvbuf, 1, &create_params);
//initialize with empty data
NvBufSurfaceMemSet(nvbuf, 0, 0, 0);
//transform according to in get_converted_buffer computed transform_params
auto err = NvBufSurfTransform(&nvinfer->tmp_surf, nvbuf, &nvinfer->transform_params);
if(err != NvBufSurfTransformError_Success) {
GST_ELEMENT_ERROR(nvinfer, STREAM, FAILED, (“NvBufSurfTransform failed with error %d while converting buffer”, err), (NULL));
}
if(NvBufSurfaceMap(nvbuf, 0, 0, NVBUF_MAP_READ) != 0) {
g_printerr(“NvBufSurfaceMap(nvbuf, 0, 0, NVBUF_MAP_READ) != 0\n”);
}
NvBufSurfaceSyncForCpu(nvbuf, 0, 0);
mat = cv::Mat(nvbuf->surfaceList[0].height, nvbuf->surfaceList[0].width, CV_8UC1, nvbuf->surfaceList[0].mappedAddr.addr[0], nvbuf->surfaceList[0].pitch);
char filename[64];
snprintf(filename, 64, “image.jpg”);
cv::imwrite(filename, mat);
g_print(“wrote file image file %s\n”, filename);
which actually gives me the expected output. May anyone confirm that this is the right way to do it?