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

举报
风吹稻花香 发表于 2022/07/08 23:14:59 2022/07/08
【摘要】 首先,你需要知道模块插件是gstnvinfer ,所以你需要在这个模块增加打印代码 模块路径:/opt/nvidia/deepstream/deepstream-6.0/sources/gst-plugins/gst-nvinfer/gstnvinfer.cpp 此方法是DGPU方式,如果需要jetson方式,按照上篇后处理图片保存...

首先,你需要知道模块插件是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

文章来源: blog.csdn.net,作者:AI视觉网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/125677194

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。