在 DeepStream 中获取识别框坐标需要通过解析推理结果来实现。DeepStream 提供了多种解析推理结果的方法,其中最常用的是使用 NvDsInferParseCustomFunc 函数来解析推理结果。此函数需要用户提供一个回调函数,用于解析推理结果并将解析得到的识别框坐标等信息保存到 NvDsObjectMeta 中。下面是一个示例回调函数,可以解析 YOLOv3-tiny 模型输出的推理结果:

static GstFlowReturn
parse_bbox_from_tensor_meta (GstBuffer *buf, NvDsInferTensorMeta *tensor_meta,
                             NvDsInferParseDetectionParams *detection_params,
                             gpointer user_data)
{
  NvDsInferDimsCHW dims = tensor_meta->dims;
  gint output_width = dims.w;
  gint output_height = dims.h;
  gint output_channel = dims.c;
  gfloat *output_buffer = (gfloat *) tensor_meta->buffer;
  guint num_classes = detection_params->numClassesConfigured;

  NvDsBatchMeta *batch_meta = gst_buffer_get_nvds_batch_meta (buf);
  if (!batch_meta) {
    g_print ("NvDsBatchMeta not found for buffer\n");
    return GST_FLOW_OK;
  }

  for (guint i = 0; i < tensor_meta->numElements; i += output_channel) {
    gfloat *bbox = &output_buffer[i];

    gfloat x_center = bbox[0];
    gfloat y_center = bbox[1];
    gfloat width = bbox[2];
    gfloat height = bbox[3];

    x_center = (x_center * 2.0 - 0.5 + detection_params->networkInputWidth)
        * detection_params->networkStrideWidth;
    y_center = (y_center * 2.0 - 0.5 + detection_params->networkInputHeight)
        * detection_params->networkStrideHeight;
    width = width * detection_params->networkInputWidth;
    height = height * detection_params->networkInputHeight;

    NvDsObjectMeta *obj_meta = nvds_acquire_obj_meta_from_pool (batch_meta);
    obj_meta->unique_component_id = tensor_meta->uniqueId;

    obj_meta->rect_params.left = x_center - width / 2;
    obj_meta->rect_params.top = y_center - height / 2;
    obj_meta->rect_params.width = width;
    obj_meta->rect_params.height = height;

    obj_meta->confidence = bbox[4];

    guint class_id = 0;
    gfloat class_prob = 0;
    for (guint j = 0; j < num_classes; j++) {
      gfloat prob = bbox[5 + j];
      if (prob > class_prob) {
        class_id = j;
        class_prob = prob;
      }
    }
    obj_meta->class_id = class_id;
    obj_meta->class_label = detection_params->classLabels[class_id];

    nvds_add_obj_meta_to_frame (batch_meta->frame_meta, obj_meta, NULL);
  }

  return GST_FLOW_OK;
}

该回调函数首先获取输出张量的维度和内容,然后计算出每个识别框的坐标和置信度,并将这些信息保存到 NvDsObjectMeta 中。最后,将 NvDsObjectMeta 添加到当前帧的对象列表中。在 DeepStream 的管道中,可以使用 NvInfer 元素的 parse-bbox-func 属性来指定该回调函数。例如:

NvDsInferConfig infer_config = {
  ...
  .outputTensorMetaCallback = parse_bbox_from_tensor_meta,
  .outputLayerName = "yolo-det",
  .networkMode = NvDsInferNetworkMode_FP32,
  ...
};
...
NvDsInferContext *infer_ctx = nvds_infer_context_new (&infer_config);
...
nvds_set_property (infer_elem, "parse-bbox-func", G_TYPE_POINTER, parse_bbox_from_tensor_meta, NULL);
``
deepstream中如何获取识别框坐标

原文地址: https://www.cveoy.top/t/topic/eB6y 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录