deepstream 预处理结果保存(进入模型前的图片)

x33g5p2x  于2022-07-10 转载在 其他  
字(4.7k)|赞(0)|评价(0)|浏览(476)

首先,你需要知道模块插件是gstnvinfer ,所以你需要在这个模块增加打印代码

模块路径:/opt/nvidia/deepstream/deepstream-6.0/sources/gst-plugins/gst-nvinfer/gstnvinfer.cpp

此方法是DGPU方式,如果需要jetson方式,按照上篇后处理图片保存修改即可

首先是保存图片的方法:

我的printf("all_bbox_generated called! colorformat =%d\n", surface->surfaceList[frameIndex].colorFormat);

打印的结果是: 27 所以我的是RGB模式。

//zwh start

static void save_transformed_plate_images(NvBufSurface * surface) {
 
  for (uint frameIndex = 0; frameIndex < surface->numFilled;
      frameIndex++) {
    void *src_data = NULL;
    src_data = (char *)malloc(surface->surfaceList[frameIndex].dataSize);
    if (src_data == NULL) {
    g_print("Error: failed to malloc src_data \n");
    }
    cudaMemcpy((void *)src_data,
        (void *)surface->surfaceList[frameIndex].dataPtr,
        surface->surfaceList[frameIndex].dataSize,
        cudaMemcpyDeviceToHost);
    gint frame_width = (gint)surface->surfaceList[frameIndex].width;
    gint frame_height = (gint)surface->surfaceList[frameIndex].height;
    size_t frame_step = surface->surfaceList[frameIndex].pitch;
    printf("all_bbox_generated called! colorformat =%d\n", surface->surfaceList[frameIndex].colorFormat);
    cv::Mat frame = cv::Mat(frame_height, frame_width, CV_8UC3, src_data, frame_step);
    cv::Mat out_mat = cv::Mat(cv::Size(frame_width, frame_height), CV_8UC3);
    cv::cvtColor(frame, out_mat, CV_RGB2BGR);
    char yuv_name[100] = "";
    sprintf(yuv_name, "yuv_%ld.png", appCtx->kafka_last_send_time);
    cv::imwrite(yuv_name, out_mat);
  }
  return;
}

调用位置在convert_batch_and_push_to_input_thread方法中完成Transform后:

save_transformed_plate_images(mem->surf);
  printf(
        "\n---------------->saved transformed plate images to disk prior to sgie "
        "detection");
即可打印出来图片。

另外还有其他小的注意点:

#include <opencv2/opencv.hpp>

#include <opencv2/imgproc/types_c.h>

头文件及makefile配置,就不过多赘述了。

CFLAGS+=-I/usr/local/include/opencv4/

整个代码段展示:

//zwh start

static void save_transformed_plate_images(NvBufSurface * surface) {
 
  for (uint frameIndex = 0; frameIndex < surface->numFilled;
      frameIndex++) {
    void *src_data = NULL;
    src_data = (char *)malloc(surface->surfaceList[frameIndex].dataSize);
    if (src_data == NULL) {
    g_print("Error: failed to malloc src_data \n");
    }
    cudaMemcpy((void *)src_data,
        (void *)surface->surfaceList[frameIndex].dataPtr,
        surface->surfaceList[frameIndex].dataSize,
        cudaMemcpyDeviceToHost);
    gint frame_width = (gint)surface->surfaceList[frameIndex].width;
    gint frame_height = (gint)surface->surfaceList[frameIndex].height;
    size_t frame_step = surface->surfaceList[frameIndex].pitch;
    printf("all_bbox_generated called! colorformat =%d\n", surface->surfaceList[frameIndex].colorFormat);
    cv::Mat frame = cv::Mat(frame_height, frame_width, CV_8UC3, src_data, frame_step);
    cv::Mat out_mat = cv::Mat(cv::Size(frame_width, frame_height), CV_8UC3);
    cv::cvtColor(frame, out_mat, CV_RGB2BGR);
    char yuv_name[100] = "";
    sprintf(yuv_name, "yuv_%ld.png", appCtx->kafka_last_send_time);
    cv::imwrite(yuv_name, out_mat);
  }
  return;
}
 
 
static gboolean
convert_batch_and_push_to_input_thread (GstNvInfer *nvinfer,
    GstNvInferBatch *batch, GstNvInferMemory *mem)
{
  NvBufSurfTransform_Error err = NvBufSurfTransformError_Success;
  std::string nvtx_str;
 
  /* Set the transform session parameters for the conversions executed in this
   * thread. */
  err = NvBufSurfTransformSetSessionParams (&nvinfer->transform_config_params);
  if (err != NvBufSurfTransformError_Success) {
    GST_ELEMENT_ERROR (nvinfer, STREAM, FAILED,
        ("NvBufSurfTransformSetSessionParams failed with error %d", err), (NULL));
    return FALSE;
  }
 
  nvtxEventAttributes_t eventAttrib = {0};
  eventAttrib.version = NVTX_VERSION;
  eventAttrib.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE;
  eventAttrib.colorType = NVTX_COLOR_ARGB;
  eventAttrib.color = 0xFFFF0000;
  eventAttrib.messageType = NVTX_MESSAGE_TYPE_ASCII;
  nvtx_str = "convert_buf batch_num=" + std::to_string(nvinfer->current_batch_num);
  eventAttrib.message.ascii = nvtx_str.c_str();
 
  nvtxDomainRangePushEx(nvinfer->nvtx_domain, &eventAttrib);
 
  if (batch->frames.size() > 0) {
    /* Batched tranformation. */
    err = NvBufSurfTransform (&nvinfer->tmp_surf, mem->surf,
              &nvinfer->transform_params);
  }
 
  nvtxDomainRangePop (nvinfer->nvtx_domain);
 
  if (err != NvBufSurfTransformError_Success) {
    GST_ELEMENT_ERROR (nvinfer, STREAM, FAILED,
        ("NvBufSurfTransform failed with error %d while converting buffer", err),
        (NULL));
    return FALSE;
  }
  save_transformed_plate_images(mem->surf);
  printf(
        "\n---------------->saved transformed plate images to disk prior to sgie "
        "detection");
  LockGMutex locker (nvinfer->process_lock);
  /* Push the batch info structure in the processing queue and notify the output
   * thread that a new batch has been queued. */
  g_queue_push_tail (nvinfer->input_queue, batch);
  g_cond_broadcast (&nvinfer->process_cond);
 
  return TRUE;
}

————————————————
版权声明:本文为CSDN博主「zhang-wen-han」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_35377454/article/details/123797606

相关文章