NVIDIA DeepStream SDK API Reference

7.1 Release
postprocesslib_impl.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #ifndef __POSTPROCESSLIB_HPP__
25 #define __POSTPROCESSLIB_HPP__
26 
27 #include <cuda.h>
28 #include <cuda_runtime.h>
29 #include "post_processor.h"
31 #include "post_processor_detect.h"
32 #include "post_processor_bodypose.h"
36 
37 #define FORMAT_NV12 "NV12"
38 #define FORMAT_RGBA "RGBA"
39 #define FORMAT_I420 "I420"
40 #define GST_CAPS_FEATURE_MEMORY_NVMM "memory:NVMM"
41 
42 /* Strcture used to share between the threads */
43 struct PacketInfo {
45  guint frame_num;
46 };
47 
49 {
50 public:
51 
53  m_vectorProperty.clear();
54  outputthread_stopped = false;
55 
56  if (createParams){
57  m_element = createParams->m_element;
58  m_gpuId = createParams->m_gpuId;
59  m_cudaStream = createParams->m_cudaStream;
60  m_preprocessor_support = createParams->m_preprocessor_support;
61  }
62  else {
63  m_element = NULL;
64  m_gpuId = 0;
65  m_cudaStream = 0;
66  m_preprocessor_support = FALSE;
67  }
68  m_initParams.preprocessor_support = FALSE;
69  m_outputThread = new std::thread(&PostProcessAlgorithm::OutputThread, this);
70  m_initParams.uniqueID = 0;
71  m_initParams.maxBatchSize = 1;
72 
73  std::memset(m_initParams.labelsFilePath,0, sizeof(m_initParams.labelsFilePath));
75  std::memset(m_initParams.customClassifierParseFuncName, 0,
76  sizeof(m_initParams.customClassifierParseFuncName)-1);
77  std::memset(m_initParams.customBBoxParseFuncName, 0,
78  sizeof(m_initParams.customBBoxParseFuncName)-1);
79  std::memset(m_initParams.customBBoxInstanceMaskParseFuncName, 0,
80  sizeof(m_initParams.customBBoxInstanceMaskParseFuncName)-1);
81 
82 
84  m_initParams.numDetectedClasses = 0;
85 
88  m_initParams.perClassDetectionParams = NULL;
89 
92  m_initParams.classifierThreshold = 0.5;
93 
94  m_initParams.segmentationThreshold = 0.5;
95 
97  m_initParams.outputLayerNames = NULL;
99  m_initParams.numOutputLayers = 0;
100 
102  m_initParams.gpuID = 0;
103 
104  m_initParams.inferInputDims = {0,0,0};
105 
108 
109  m_initParams.classifier_type = NULL;
110  }
111 
112  /* Pass GST events to the library */
113  virtual bool HandleEvent(GstEvent *event);
114 
115  /* Read Config file */
116  virtual bool SetConfigFile (const gchar *config_file);
117 
118  /* Process Incoming Buffer */
119  virtual BufferResult ProcessBuffer(GstBuffer *inbuf);
120 
121  std::vector<std::string> SplitString (std::string input);
122 
123  std::set<gint> SplitStringInt (std::string input);
124 
125  bool GetAbsFilePath (const gchar * cfg_file_path, const gchar * file_path,
126  char *abs_path_str);
127 
128  gboolean hw_caps;
129 
130  /* Deinit members */
132 
133 private:
134  /* Helper Function to Extract Batch Meta from buffer */
135  NvDsBatchMeta * GetNVDS_BatchMeta (GstBuffer *buffer);
136 
137  /* Output Processing Thread, push buffer to downstream */
138  void OutputThread(void);
139 
140  /* Helper function to Dump NvBufSurface RAW content */
141  void DumpNvBufSurface (NvBufSurface *in_surface, NvDsBatchMeta *batch_meta);
142 
143  bool ParseLabelsFile(const std::string& labelsFilePath);
144  bool ParseConfAttr (YAML::Node node, gint64 class_id, NvDsPostProcessDetectionParams& params);
145  NvDsPostProcessStatus preparePostProcess();
146 
147  NvDsPostProcessNetworkType m_networkType;
148  NvDsPostProcessClusterMode m_clusterMode;
149  gfloat m_classifierThreshold;
150  gfloat m_segmentationThreshold;
151  gint m_numDetectedClasses;
152  gint m_processMode = 1;
153  guint m_gieUniqueId = 0;
154  gboolean m_isClassifier = 0;
155  gboolean m_releaseTensorMeta = FALSE;
156  gboolean m_outputInstanceMask = FALSE;
157  gboolean m_preprocessor_support = FALSE;
158  std::string m_classifierType;
159  std::set <gint> m_filterOutClassIds;
160  std::set <gint> m_operateOnClassIds;
161  std::vector <std::string> m_outputBlobNames;
162 
163  std::vector<NvDsPostProcessInstanceMaskInfo> m_InstanceMaskList;
164  std::unordered_map <gint, NvDsPostProcessDetectionParams> m_detectorClassAttr;
165  /* Vector of NvDsPostProcessInstanceMaskInfo vectors for each class. */
166  std::vector<std::vector<NvDsPostProcessInstanceMaskInfo>> m_PerClassInstanceMaskList;
167  std::vector<std::vector<std::string>> m_Labels;
168 
169  std::unique_ptr<ModelPostProcessor> m_Postprocessor;
171 public:
172  guint source_id = 0;
173  guint m_frameNum = 0;
174  bool outputthread_stopped = false;
175 
176  /* Output Thread Pointer */
177  std::thread *m_outputThread = NULL;
178 
179  /* Queue and Lock Management */
180  std::queue<PacketInfo> m_processQ;
181  std::mutex m_processLock;
182  std::condition_variable m_processCV;
183  /* Aysnc Stop Handling */
184  gboolean m_stop = FALSE;
185 
186  /* Vector Containing Key:Value Pair of Custom Lib Properties */
187  std::vector<Property> m_vectorProperty;
188 
189 };
190 
192 // Create Custom Algorithm / Library Context
193 
194 
195 #endif
PostProcessAlgorithm::PostProcessAlgorithm
PostProcessAlgorithm(DSPostProcess_CreateParams *createParams)
Definition: postprocesslib_impl.h:52
PostProcessAlgorithm::m_vectorProperty
std::vector< Property > m_vectorProperty
Definition: postprocesslib_impl.h:187
_NvDsPostProcessContextInitParams::clusterMode
NvDsPostProcessClusterMode clusterMode
Holds the type of clustering mode.
Definition: post_processor_struct.h:307
PostProcessAlgorithm::source_id
guint source_id
Definition: postprocesslib_impl.h:172
post_processor_instance_segment.h
PacketInfo
Definition: postprocesslib_impl.h:43
post_processor_segmentation.h
PostProcessAlgorithm::m_frameNum
guint m_frameNum
Definition: postprocesslib_impl.h:173
_NvDsPostProcessContextInitParams::labelsFilePath
char labelsFilePath[_PATH_MAX]
Holds the pathname of the labels file containing strings for the class labels.
Definition: post_processor_struct.h:276
IDSPostProcessLibrary
Definition: nvdspostprocesslib_interface.hpp:54
_NvDsPostProcessContextInitParams::customBBoxInstanceMaskParseFuncName
char customBBoxInstanceMaskParseFuncName[_MAX_STR_LENGTH]
Holds the name of the bounding box and instance mask parse function in the custom library.
Definition: post_processor_struct.h:311
PostProcessAlgorithm::hw_caps
gboolean hw_caps
Definition: postprocesslib_impl.h:128
_NvDsPostProcessContextInitParams::classifierThreshold
float classifierThreshold
Holds the minimum confidence threshold for the classifier to consider a label valid.
Definition: post_processor_struct.h:291
_NvDsPostProcessContextInitParams::inferInputDims
NvDsInferDimsCHW inferInputDims
Inference input dimensions for runtime engine.
Definition: post_processor_struct.h:304
PostProcessAlgorithm::HandleEvent
virtual bool HandleEvent(GstEvent *event)
NvDsPostProcessDetectionParams
Holds detection and bounding box grouping parameters.
Definition: post_processor_struct.h:218
NvDsPostProcessNetworkType
NvDsPostProcessNetworkType
Defines network types.
Definition: post_processor_struct.h:191
PostProcessAlgorithm::~PostProcessAlgorithm
~PostProcessAlgorithm()
NvBufSurface
Holds information about batched buffers.
Definition: nvbufsurface.h:520
post_processor.h
BufferResult
BufferResult
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_interface.hpp:22
_NvDsPostProcessContextInitParams::numDetectedClasses
unsigned int numDetectedClasses
Holds the number of classes detected by a detector network.
Definition: post_processor_struct.h:283
_NvDsPostProcessContextInitParams::perClassDetectionParams
NvDsPostProcessDetectionParams * perClassDetectionParams
Holds per-class detection parameters.
Definition: post_processor_struct.h:287
nvdspostprocesslib_base.hpp
_NvDsPostProcessContextInitParams::uniqueID
unsigned int uniqueID
Holds a unique identifier for the instance.
Definition: post_processor_struct.h:266
NvDsPostProcessStatus
NvDsPostProcessStatus
Enum for the status codes returned by NvDsPostProcessAlgorithm.
Definition: post_processor_struct.h:84
PostProcessAlgorithm
Definition: postprocesslib_impl.h:48
PacketInfo::frame_num
guint frame_num
Definition: postprocesslib_impl.h:45
DSPostProcessLibraryBase::m_cudaStream
cudaStream_t m_cudaStream
Definition: nvdspostprocesslib_base.hpp:57
DSPostProcess_CreateParams::m_gpuId
guint m_gpuId
Definition: nvdspostprocesslib_interface.hpp:39
DSPostProcessLibraryBase
Definition: nvdspostprocesslib_base.hpp:33
PostProcessAlgorithm::GetAbsFilePath
bool GetAbsFilePath(const gchar *cfg_file_path, const gchar *file_path, char *abs_path_str)
DSPostProcess_CreateParams::m_cudaStream
cudaStream_t m_cudaStream
Definition: nvdspostprocesslib_interface.hpp:40
DSPostProcess_CreateParams::m_preprocessor_support
bool m_preprocessor_support
Definition: nvdspostprocesslib_interface.hpp:41
DSPostProcess_CreateParams::m_element
GstBaseTransform * m_element
Definition: nvdspostprocesslib_interface.hpp:38
_NvDsPostProcessContextInitParams::segmentationThreshold
float segmentationThreshold
Definition: post_processor_struct.h:293
_NvDsPostProcessContextInitParams::gpuID
unsigned int gpuID
Holds the ID of the GPU which is to run the inference.
Definition: post_processor_struct.h:301
PostProcessAlgorithm::SetConfigFile
virtual bool SetConfigFile(const gchar *config_file)
PostProcessAlgorithm::m_processQ
std::queue< PacketInfo > m_processQ
Definition: postprocesslib_impl.h:180
_NvDsBatchMeta
Holds information about a formed batch containing frames from different sources.
Definition: nvdsmeta.h:242
NvDsPostProcessClusterMode
NvDsPostProcessClusterMode
Enum for clustering mode for detectors.
Definition: post_processor_struct.h:169
_NvDsPostProcessContextInitParams
Holds the initialization parameters required for the NvDsPostProcessContext interface.
Definition: post_processor_struct.h:262
_NvDsPostProcessContextInitParams::customClassifierParseFuncName
char customClassifierParseFuncName[_MAX_STR_LENGTH]
Name of the custom classifier attribute parsing function in the custom library.
Definition: post_processor_struct.h:318
DSPostProcess_CreateParams
Definition: nvdspostprocesslib_interface.hpp:37
post_processor_classify.h
DSPostProcessLibraryBase::m_gpuId
guint m_gpuId
GPU ID on which we expect to execute the algorithm.
Definition: nvdspostprocesslib_base.hpp:54
DSPostProcessLibraryBase::m_element
GstBaseTransform * m_element
Definition: nvdspostprocesslib_base.hpp:51
_NvDsPostProcessContextInitParams::classifier_type
char * classifier_type
Definition: post_processor_struct.h:323
PostProcessAlgorithm::outputthread_stopped
bool outputthread_stopped
Definition: postprocesslib_impl.h:174
_NvDsPostProcessContextInitParams::networkType
NvDsPostProcessNetworkType networkType
Holds the network type.
Definition: post_processor_struct.h:280
_NvDsPostProcessContextInitParams::outputLayerNames
char ** outputLayerNames
Holds a pointer to an array of pointers to output layer names.
Definition: post_processor_struct.h:296
_NvDsPostProcessContextInitParams::maxBatchSize
unsigned int maxBatchSize
Holds the maximum number of frames to be inferred together in a batch.
Definition: post_processor_struct.h:271
post_processor_detect.h
_NvDsPostProcessContextInitParams::preprocessor_support
gboolean preprocessor_support
Holds boolean value to show whether preprocessor support is there.
Definition: post_processor_struct.h:325
PostProcessAlgorithm::SplitStringInt
std::set< gint > SplitStringInt(std::string input)
GstBuffer
struct _GstBuffer GstBuffer
Definition: idatatype.h:19
_NvDsPostProcessContextInitParams::customBBoxParseFuncName
char customBBoxParseFuncName[_MAX_STR_LENGTH]
Holds the name of the custom bounding box function in the custom library.
Definition: post_processor_struct.h:315
PostProcessAlgorithm::m_outputThread
std::thread * m_outputThread
Definition: postprocesslib_impl.h:177
PacketInfo::inbuf
GstBuffer * inbuf
Definition: postprocesslib_impl.h:44
PostProcessAlgorithm::m_processLock
std::mutex m_processLock
Definition: postprocesslib_impl.h:181
PostProcessAlgorithm::m_stop
gboolean m_stop
Definition: postprocesslib_impl.h:184
NvDsPostProcessNetworkType_Other
@ NvDsPostProcessNetworkType_Other
Specifies other.
Definition: post_processor_struct.h:212
_NvDsPostProcessContextInitParams::numOutputLayers
unsigned int numOutputLayers
Holds the number of output layer names.
Definition: post_processor_struct.h:298
NVDSPOSTPROCESS_CLUSTER_NMS
@ NVDSPOSTPROCESS_CLUSTER_NMS
Definition: post_processor_struct.h:173
PostProcessAlgorithm::ProcessBuffer
virtual BufferResult ProcessBuffer(GstBuffer *inbuf)
CreateCustomAlgoCtx
IDSPostProcessLibrary * CreateCustomAlgoCtx(DSPostProcess_CreateParams *params)
PostProcessAlgorithm::SplitString
std::vector< std::string > SplitString(std::string input)
PostProcessAlgorithm::m_processCV
std::condition_variable m_processCV
Definition: postprocesslib_impl.h:182