NVIDIA DeepStream SDK API Reference

8.0 Release
resnet_tensor_parser.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
4  *
5  * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
6  * property and proprietary rights in and to this material, related
7  * documentation and any modifications thereto. Any use, reproduction,
8  * disclosure or distribution of this material and related documentation
9  * without an express license agreement from NVIDIA CORPORATION or
10  * its affiliates is strictly prohibited.
11  */
12 
13 #include <iostream>
14 #include <string>
15 #include <memory>
16 #include <vector>
17 
18 #include "buffer_probe.hpp"
19 
20 #include <gst/gst.h>
21 #include "nvdsmeta.h"
22 #include "gstnvdsinfer.h"
23 #include "nvdsinfer_custom_impl.h"
24 #include "nvdsinfer_dbscan.h"
25 
26 using namespace std;
27 
28 extern "C"
29  bool NvDsInferParseCustomResnet (std::vector < NvDsInferLayerInfo >
30  const &outputLayersInfo, NvDsInferNetworkInfo const &networkInfo,
31  NvDsInferParseDetectionParams const &detectionParams,
32  std::vector < NvDsInferObjectDetectionInfo > &objectList);
33 
34 #define PGIE_CLASS_ID_VEHICLE 0
35 #define PGIE_CLASS_ID_PERSON 2
36 
37 namespace deepstream
38 {
39 
41  {
42  public:
44  dbscan_ = NvDsInferDBScanCreate();
45  }
46  virtual ~TensorMetaParser() {
47  NvDsInferDBScanDestroy(dbscan_);
48  }
49 
51  FrameMetadata::Iterator frame_itr;
52  for (data.initiateIterator(frame_itr); !frame_itr->done(); frame_itr->next()) {
53  int network_width = 0;
54  int network_height = 0;
55  int stream_width = 0;
56  int stream_height = 0;
57  int n_classes = 0;
58  NvDsInferParseDetectionParams detectionParams;
59  detectionParams.numClassesConfigured = 4;
60  detectionParams.perClassPreclusterThreshold = {0.2, 0.2, 0.2, 0.2};
61 
62  probe.getProperty("network-width", network_width)
63  .getProperty("network-height", network_height)
64  .getProperty("stream-width", stream_width)
65  .getProperty("stream-height", stream_height)
66  .getProperty("num-classes", n_classes);
67  float scalers[] = {(float)stream_width/network_width, (float)stream_height/network_height };
68  NvDsInferNetworkInfo networkInfo{(unsigned int)network_width, (unsigned int)network_height, 3};
69 
70  FrameMetadata &frame_meta = frame_itr->get();
71  frame_meta.iterate([&](const UserMetadata& user_meta) {
72  /* We know what tensor meta it is, so we directly cast it to the right type */
74  NvDsInferTensorMeta& output_tensor = tensor_meta.get();
75  for (unsigned int i = 0; i < output_tensor.num_output_layers; i++) {
76  NvDsInferLayerInfo &info = output_tensor.output_layers_info[i];
77  /* fill in the buffer address */
78  info.buffer = output_tensor.out_buf_ptrs_host[i];
79  }
80  /* Parse output tensor and fill detection results into objectList. */
81  std::vector <NvDsInferLayerInfo> outputLayersInfo(output_tensor.output_layers_info, output_tensor.output_layers_info + output_tensor.num_output_layers);
82  std::vector <NvDsInferObjectDetectionInfo> objectList;
83  NvDsInferParseCustomResnet(outputLayersInfo, networkInfo, detectionParams, objectList);
84 
85  NvDsInferDBScanClusteringParams clusteringParams;
86  clusteringParams.enableATHRFilter = true;
87  clusteringParams.thresholdATHR = 60.0;
88  clusteringParams.eps = 0.95;
89  clusteringParams.minBoxes = 3;
90  clusteringParams.minScore = 0.5;
91 
92  /* Create perClassObjectList: vector<vector<NvDsInferObjectDetectionInfo>>. Each vector is of same classID */
93  std::vector <std::vector<NvDsInferObjectDetectionInfo>> perClassObjectList(n_classes);
94  for (auto & obj:objectList) {
95  perClassObjectList[obj.classId].emplace_back (obj);
96  }
97 
98  /* Call NvDsInferDBScanCluster on each of the vector and resize it */
99  for (unsigned int c = 0; c < perClassObjectList.size(); c++) {
100  NvDsInferObjectDetectionInfo *objArray = (NvDsInferObjectDetectionInfo*) (perClassObjectList[c].data());
101  size_t numObjects = perClassObjectList[c].size();
102 
103  /* Cluster together rectangles with similar locations and sizes since these rectangles might represent the same object using DBSCAN. */
104  if (clusteringParams.minBoxes > 0) {
105  NvDsInferDBScanCluster(dbscan_, &clusteringParams, objArray, &numObjects);
106  }
107  perClassObjectList[c].resize(numObjects);
108 
109  /* Generate object meta for downstream usage such as OSD display */
110  for (unsigned int i = 0; i < numObjects; i++) {
111  ObjectMetadata object_meta;
112  if (data.acquire(object_meta)) {
113  object_meta.setClassId(c);
114  object_meta.setConfidence(objArray[i].detectionConfidence);
115  object_meta.setRectParams(
116  NvOSD_RectParams{objArray[i].left*scalers[0], objArray[i].top*scalers[1], objArray[i].width*scalers[0], objArray[i].height*scalers[1], 1, NvOSD_ColorParams{1.0, 0, 0, 1.0}}
117  );
118  frame_meta.append(object_meta);
119  }
120  }
121  }
122 
123  std::cout << "Object Counter: " << " Pad Idx = " << frame_meta.padIndex() << " Frame Number = " << frame_meta.frameNum() <<
124  " Vehicle Count = " << perClassObjectList[PGIE_CLASS_ID_VEHICLE].size() <<
125  " Person Count = " << perClassObjectList[PGIE_CLASS_ID_PERSON].size() << std::endl;
126 
127 
129  }
130 
131  return probeReturn::Probe_Ok;
132  }
133  protected:
134  NvDsInferDBScan* dbscan_;
135  };
136 
137 }
buffer_probe.hpp
NvDsInferParseCustomResnet
bool NvDsInferParseCustomResnet(std::vector< NvDsInferLayerInfo > const &outputLayersInfo, NvDsInferNetworkInfo const &networkInfo, NvDsInferParseDetectionParams const &detectionParams, std::vector< NvDsInferObjectDetectionInfo > &objectList)
_NvOSD_RectParams
Holds the box parameters of the box to be overlayed.
Definition: nvll_osd_struct.h:140
NvDsInferTensorMeta
Holds the raw tensor output information for one frame / one object.
Definition: gstnvdsinfer.h:72
NvDsInferDBScanClusteringParams::minScore
float minScore
Holds the sum of neighborhood confidence thresholds.
Definition: nvdsinfer_dbscan.h:59
deepstream::TensorMetaParser::dbscan_
NvDsInferDBScan * dbscan_
Definition: resnet_tensor_parser.hpp:134
NvDsInferObjectDetectionInfo::width
float width
Holds the width of the object's bounding box.
Definition: nvdsinfer.h:155
NvDsInferDBScanClusteringParams::enableATHRFilter
int enableATHRFilter
Holds a Boolean; true enables the area-to-hit ratio (ATHR) filter.
Definition: nvdsinfer_dbscan.h:55
NVDSINFER_TENSOR_OUTPUT_META
@ NVDSINFER_TENSOR_OUTPUT_META
Specifies metadata type for raw inference output attached by Gst-nvinfer.
Definition: nvdsmeta.h:126
NvDsInferObjectDetectionInfo::left
float left
Holds the horizontal offset of the bounding box shape for the object.
Definition: nvdsinfer.h:151
NvDsInferTensorMeta::out_buf_ptrs_host
void ** out_buf_ptrs_host
Array of pointers to the output host buffers for the batch / frame / object.
Definition: gstnvdsinfer.h:83
deepstream::BufferProbe
Represent a custom object for the purpose of probing output buffers.
Definition: buffer_probe.hpp:58
gstnvdsinfer.h
NvDsInferTensorMeta::num_output_layers
guint num_output_layers
Number of bound output layers.
Definition: gstnvdsinfer.h:77
deepstream::BufferProbe::IBatchMetadataOperator
Read/write interface for handling batch metadata.
Definition: buffer_probe.hpp:111
deepstream::TensorMetaParser
Definition: resnet_tensor_parser.hpp:40
deepstream::UserMetadataTemplate::get
C & get()
Definition: metadata.hpp:342
NvDsInferDBScanClusteringParams::thresholdATHR
float thresholdATHR
Holds the area-to-hit ratio threshold.
Definition: nvdsinfer_dbscan.h:57
deepstream::TensorMetaParser::~TensorMetaParser
virtual ~TensorMetaParser()
Definition: resnet_tensor_parser.hpp:46
NvDsInferParseDetectionParams::numClassesConfigured
unsigned int numClassesConfigured
Holds the number of classes requested to be parsed, starting with class ID 0.
Definition: nvdsinfer_custom_impl.h:184
NvDsInferDBScanCreate
NvDsInferDBScanHandle NvDsInferDBScanCreate()
Creates a new DBScan object clustering context.
NvDsInferDBScanCluster
void NvDsInferDBScanCluster(NvDsInferDBScanHandle handle, NvDsInferDBScanClusteringParams *params, NvDsInferObjectDetectionInfo *objects, size_t *numObjects)
Clusters an array of objects in place using specified clustering parameters.
deepstream::TensorMetaParser::handleData
virtual probeReturn handleData(BufferProbe &probe, BatchMetadata &data)
Definition: resnet_tensor_parser.hpp:50
NvDsInferParseDetectionParams
Holds the detection parameters required for parsing objects.
Definition: nvdsinfer_custom_impl.h:179
nvdsinfer_dbscan.h
NvDsInferObjectDetectionInfo
Holds information about one parsed object from a detector's output.
Definition: nvdsinfer.h:145
deepstream::UserMetadataTemplate
Template for customized user metadata.
Definition: metadata.hpp:335
deepstream::ObjectMetadata::setRectParams
void setRectParams(const NvOSD_RectParams &)
deepstream::FrameMetadata::append
void append(const DisplayMetadata &)
Append display metadata to the frame.
deepstream::ObjectMetadata
Object metadata.
Definition: metadata.hpp:155
deepstream::probeReturn
probeReturn
Return values from user implemented probe interfaces.
Definition: buffer_probe.hpp:42
NvDsInferNetworkInfo
Holds information about the model network.
Definition: nvdsinfer.h:114
deepstream::TensorMetaParser::TensorMetaParser
TensorMetaParser()
Definition: resnet_tensor_parser.hpp:43
NvDsInferLayerInfo
Holds information about one layer in the model.
Definition: nvdsinfer.h:91
NvDsInferDBScanClusteringParams
Holds object clustering parameters required by DBSCAN.
Definition: nvdsinfer_dbscan.h:49
NvDsInferLayerInfo::buffer
void * buffer
Holds a pointer to the buffer for the layer data.
Definition: nvdsinfer.h:105
deepstream
Definition: buffer.hpp:33
PGIE_CLASS_ID_PERSON
#define PGIE_CLASS_ID_PERSON
Definition: resnet_tensor_parser.hpp:35
NvDsInferObjectDetectionInfo::top
float top
Holds the vertical offset of the object's bounding box.
Definition: nvdsinfer.h:153
nvdsinfer_custom_impl.h
deepstream::BatchMetadata::initiateIterator
void initiateIterator(FrameMetadata::Iterator &) const
Get the iterator for frame metadata within it.
deepstream::FrameMetadata::padIndex
unsigned int padIndex() const
Index of the pad from which the frame is generated.
NvDsInferTensorMeta::output_layers_info
NvDsInferLayerInfo * output_layers_info
Pointer to the array containing information for the bound output layers.
Definition: gstnvdsinfer.h:81
deepstream::BatchMetadata
Holds information about a formed batch containingframes from different sources.
Definition: metadata.hpp:652
NvDsInferDBScanDestroy
void NvDsInferDBScanDestroy(NvDsInferDBScanHandle handle)
Destroys a DBScan object clustering context.
NvDsInferParseDetectionParams::perClassPreclusterThreshold
std::vector< float > perClassPreclusterThreshold
Holds a per-class vector of detection confidence thresholds to be applied prior to the clustering ope...
Definition: nvdsinfer_custom_impl.h:190
deepstream::FrameMetadata::Iterator
std::unique_ptr< AbstractIterator< FrameMetadata > > Iterator
Definition: metadata.hpp:419
deepstream::UserMetadata
Base class of user defined metadata.
Definition: metadata.hpp:101
NvDsInferObjectDetectionInfo::height
float height
Holds the height of the object's bounding box.
Definition: nvdsinfer.h:157
deepstream::FrameMetadata::frameNum
int frameNum() const
Frame number.
_NvOSD_ColorParams
Holds the color parameters of the box or text to be overlayed.
Definition: nvll_osd_struct.h:81
deepstream::ObjectMetadata::setClassId
void setClassId(unsigned int)
NvDsInferDBScanClusteringParams::minBoxes
uint32_t minBoxes
Definition: nvdsinfer_dbscan.h:52
nvdsmeta.h
deepstream::FrameMetadata::iterate
unsigned int iterate(const std::function< void(const ObjectMetadata &)> &func) const
Iterate the object metadata within it.
deepstream::BatchMetadata::acquire
bool acquire(DisplayMetadata &)
Initialize an empty display metadata.
deepstream::ObjectMetadata::setConfidence
void setConfidence(float)
deepstream::Object::getProperty
Object & getProperty(const std::string &name, T &value, Args &... args)
Template for getting multiple properties.
Definition: object.hpp:171
NvDsInferDBScanClusteringParams::eps
float eps
Definition: nvdsinfer_dbscan.h:51
PGIE_CLASS_ID_VEHICLE
#define PGIE_CLASS_ID_VEHICLE
Definition: resnet_tensor_parser.hpp:34
deepstream::FrameMetadata
Holds information for a single frame.
Definition: metadata.hpp:417