NVIDIA DeepStream SDK API Reference

9.1 Release
sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/postprocesslib_impl.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #ifndef __POSTPROCESSLIB_HPP__
19 #define __POSTPROCESSLIB_HPP__
20 
21 #include <cuda.h>
22 #include <cuda_runtime.h>
23 #include "post_processor.h"
25 #include "post_processor_detect.h"
29 #include "nvdspostprocesslib_base.hpp"
30 
31 #define FORMAT_NV12 "NV12"
32 #define FORMAT_RGBA "RGBA"
33 #define FORMAT_I420 "I420"
34 #define GST_CAPS_FEATURE_MEMORY_NVMM "memory:NVMM"
35 
36 /* Strcture used to share between the threads */
37 struct PacketInfo {
39  guint frame_num;
40 };
41 
43 {
44 public:
45 
47  m_vectorProperty.clear();
48  outputthread_stopped = false;
49 
50  if (createParams){
51  m_element = createParams->m_element;
52  m_gpuId = createParams->m_gpuId;
53  m_cudaStream = createParams->m_cudaStream;
54  m_preprocessor_support = createParams->m_preprocessor_support;
55  }
56  else {
57  m_element = NULL;
58  m_gpuId = 0;
59  m_cudaStream = 0;
60  m_preprocessor_support = FALSE;
61  }
62  m_initParams.preprocessor_support = FALSE;
63  m_outputThread = new std::thread(&PostProcessAlgorithm::OutputThread, this);
64  m_initParams.uniqueID = 0;
65  m_initParams.maxBatchSize = 1;
66 
67  std::memset(m_initParams.labelsFilePath,0, sizeof(m_initParams.labelsFilePath));
69  std::memset(m_initParams.customClassifierParseFuncName, 0,
70  sizeof(m_initParams.customClassifierParseFuncName)-1);
71  std::memset(m_initParams.customBBoxParseFuncName, 0,
72  sizeof(m_initParams.customBBoxParseFuncName)-1);
73  std::memset(m_initParams.customBBoxInstanceMaskParseFuncName, 0,
74  sizeof(m_initParams.customBBoxInstanceMaskParseFuncName)-1);
75 
76 
78  m_initParams.numDetectedClasses = 0;
79 
82  m_initParams.perClassDetectionParams = NULL;
83 
86  m_initParams.classifierThreshold = 0.5;
87 
88  m_initParams.segmentationThreshold = 0.5;
89 
91  m_initParams.outputLayerNames = NULL;
93  m_initParams.numOutputLayers = 0;
94 
96  m_initParams.gpuID = 0;
97 
98  m_initParams.inferInputDims = {0,0,0};
99 
102 
103  m_initParams.classifier_type = NULL;
104  }
105 
106  /* Pass GST events to the library */
107  virtual bool HandleEvent(GstEvent *event);
108 
109  /* Read Config file */
110  virtual bool SetConfigFile (const gchar *config_file);
111 
112  /* Process Incoming Buffer */
113  virtual BufferResult ProcessBuffer(GstBuffer *inbuf);
114 
115  std::vector<std::string> SplitString (std::string input);
116 
117  std::set<gint> SplitStringInt (std::string input);
118 
119  bool GetAbsFilePath (const gchar * cfg_file_path, const gchar * file_path,
120  char *abs_path_str);
121 
122  gboolean hw_caps;
123 
124  /* Deinit members */
126 
127 private:
128  /* Helper Function to Extract Batch Meta from buffer */
129  NvDsBatchMeta * GetNVDS_BatchMeta (GstBuffer *buffer);
130 
131  /* Output Processing Thread, push buffer to downstream */
132  void OutputThread(void);
133 
134  /* Helper function to Dump NvBufSurface RAW content */
135  void DumpNvBufSurface (NvBufSurface *in_surface, NvDsBatchMeta *batch_meta);
136 
137  bool ParseLabelsFile(const std::string& labelsFilePath);
138  bool ParseConfAttr (YAML::Node node, gint64 class_id, NvDsPostProcessDetectionParams& params);
139  NvDsPostProcessStatus preparePostProcess();
140 
141  NvDsPostProcessNetworkType m_networkType;
142  NvDsPostProcessClusterMode m_clusterMode;
143  gfloat m_classifierThreshold;
144  gfloat m_segmentationThreshold;
145  gint m_numDetectedClasses;
146  gint m_processMode = 1;
147  guint m_gieUniqueId = 0;
148  gboolean m_isClassifier = 0;
149  gboolean m_releaseTensorMeta = FALSE;
150  gboolean m_outputInstanceMask = FALSE;
151  gboolean m_preprocessor_support = FALSE;
152  std::string m_classifierType;
153  std::set <gint> m_filterOutClassIds;
154  std::set <gint> m_operateOnClassIds;
155  std::vector <std::string> m_outputBlobNames;
156 
157  std::vector<NvDsPostProcessInstanceMaskInfo> m_InstanceMaskList;
158  std::unordered_map <gint, NvDsPostProcessDetectionParams> m_detectorClassAttr;
159  /* Vector of NvDsPostProcessInstanceMaskInfo vectors for each class. */
160  std::vector<std::vector<NvDsPostProcessInstanceMaskInfo>> m_PerClassInstanceMaskList;
161  std::vector<std::vector<std::string>> m_Labels;
162 
163  std::unique_ptr<ModelPostProcessor> m_Postprocessor;
165 public:
166  guint source_id = 0;
167  guint m_frameNum = 0;
168  bool outputthread_stopped = false;
169 
170  /* Output Thread Pointer */
171  std::thread *m_outputThread = NULL;
172 
173  /* Queue and Lock Management */
174  std::queue<PacketInfo> m_processQ;
175  std::mutex m_processLock;
176  std::condition_variable m_processCV;
177  /* Aysnc Stop Handling */
178  gboolean m_stop = FALSE;
179 
180  /* Vector Containing Key:Value Pair of Custom Lib Properties */
181  std::vector<Property> m_vectorProperty;
182 
183 };
184 
186 // Create Custom Algorithm / Library Context
187 
188 
189 #endif
PostProcessAlgorithm::PostProcessAlgorithm
PostProcessAlgorithm(DSPostProcess_CreateParams *createParams)
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/postprocesslib_impl.h:46
_NvDsPostProcessContextInitParams::clusterMode
NvDsPostProcessClusterMode clusterMode
Holds the type of clustering mode.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:301
PostProcessAlgorithm::source_id
guint source_id
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/postprocesslib_impl.h:166
PacketInfo
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/postprocesslib_impl.h:37
PostProcessAlgorithm::m_frameNum
guint m_frameNum
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/postprocesslib_impl.h:167
IDSPostProcessLibrary
Definition: sources/gst-plugins/gst-nvdspostprocess/includes/nvdspostprocesslib_interface.hpp:48
PostProcessAlgorithm::hw_caps
gboolean hw_caps
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/postprocesslib_impl.h:122
_NvDsPostProcessContextInitParams::classifierThreshold
float classifierThreshold
Holds the minimum confidence threshold for the classifier to consider a label valid.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:285
_NvDsPostProcessContextInitParams::inferInputDims
NvDsInferDimsCHW inferInputDims
Inference input dimensions for runtime engine.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:298
PostProcessAlgorithm::HandleEvent
virtual bool HandleEvent(GstEvent *event)
NvDsPostProcessDetectionParams
Holds detection and bounding box grouping parameters.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:212
post_processor.h
PostProcessAlgorithm::~PostProcessAlgorithm
~PostProcessAlgorithm()
NvBufSurface
Holds information about batched buffers.
Definition: sources/includes/nvbufsurface.h:597
_NvDsPostProcessContextInitParams::customBBoxParseFuncName
char customBBoxParseFuncName[_MAX_STR_LENGTH]
Holds the name of the custom bounding box function in the custom library.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:309
post_processor_instance_segment.h
_NvDsPostProcessContextInitParams::numDetectedClasses
unsigned int numDetectedClasses
Holds the number of classes detected by a detector network.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:277
NvDsPostProcessClusterMode
NvDsPostProcessClusterMode
Enum for clustering mode for detectors.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:163
NvDsPostProcessNetworkType
NvDsPostProcessNetworkType
Defines network types.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:185
_NvDsPostProcessContextInitParams::uniqueID
unsigned int uniqueID
Holds a unique identifier for the instance.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:260
NvDsPostProcessStatus
NvDsPostProcessStatus
Enum for the status codes returned by NvDsPostProcessAlgorithm.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:78
CreateCustomAlgoCtx
IDSPostProcessLibrary * CreateCustomAlgoCtx(DSPostProcess_CreateParams *params)
PacketInfo::inbuf
GstBuffer * inbuf
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/postprocesslib_impl.h:38
PostProcessAlgorithm
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/postprocesslib_impl.h:42
PostProcessAlgorithm::m_processQ
std::queue< PacketInfo > m_processQ
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/postprocesslib_impl.h:174
PacketInfo::frame_num
guint frame_num
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/postprocesslib_impl.h:39
DSPostProcessLibraryBase::m_cudaStream
cudaStream_t m_cudaStream
Definition: sources/gst-plugins/gst-nvdspostprocess/includes/nvdspostprocesslib_base.hpp:51
DSPostProcess_CreateParams::m_gpuId
guint m_gpuId
Definition: sources/gst-plugins/gst-nvdspostprocess/includes/nvdspostprocesslib_interface.hpp:33
DSPostProcessLibraryBase
Definition: sources/gst-plugins/gst-nvdspostprocess/includes/nvdspostprocesslib_base.hpp:27
PostProcessAlgorithm::GetAbsFilePath
bool GetAbsFilePath(const gchar *cfg_file_path, const gchar *file_path, char *abs_path_str)
_NvDsPostProcessContextInitParams::perClassDetectionParams
NvDsPostProcessDetectionParams * perClassDetectionParams
Holds per-class detection parameters.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:281
DSPostProcessLibraryBase::m_element
GstBaseTransform * m_element
Definition: sources/gst-plugins/gst-nvdspostprocess/includes/nvdspostprocesslib_base.hpp:45
DSPostProcess_CreateParams::m_cudaStream
cudaStream_t m_cudaStream
Definition: sources/gst-plugins/gst-nvdspostprocess/includes/nvdspostprocesslib_interface.hpp:34
DSPostProcess_CreateParams::m_preprocessor_support
bool m_preprocessor_support
Definition: sources/gst-plugins/gst-nvdspostprocess/includes/nvdspostprocesslib_interface.hpp:35
_NvDsPostProcessContextInitParams::segmentationThreshold
float segmentationThreshold
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:287
post_processor_detect.h
_NvDsPostProcessContextInitParams::gpuID
unsigned int gpuID
Holds the ID of the GPU which is to run the inference.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:295
PostProcessAlgorithm::SetConfigFile
virtual bool SetConfigFile(const gchar *config_file)
_NvDsPostProcessContextInitParams::customClassifierParseFuncName
char customClassifierParseFuncName[_MAX_STR_LENGTH]
Name of the custom classifier attribute parsing function in the custom library.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:312
_NvDsBatchMeta
Holds information about a formed batch containing frames from different sources.
Definition: sources/includes/nvdsmeta.h:262
_NvDsPostProcessContextInitParams
Holds the initialization parameters required for the NvDsPostProcessContext interface.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:256
PostProcessAlgorithm::m_outputThread
std::thread * m_outputThread
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/postprocesslib_impl.h:171
_NvDsPostProcessContextInitParams::classifier_type
char * classifier_type
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:317
post_processor_segmentation.h
DSPostProcess_CreateParams
Definition: sources/gst-plugins/gst-nvdspostprocess/includes/nvdspostprocesslib_interface.hpp:31
DSPostProcessLibraryBase::m_gpuId
guint m_gpuId
GPU ID on which we expect to execute the algorithm.
Definition: sources/gst-plugins/gst-nvdspostprocess/includes/nvdspostprocesslib_base.hpp:48
PostProcessAlgorithm::outputthread_stopped
bool outputthread_stopped
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/postprocesslib_impl.h:168
_NvDsPostProcessContextInitParams::networkType
NvDsPostProcessNetworkType networkType
Holds the network type.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:274
post_processor_classify.h
_NvDsPostProcessContextInitParams::customBBoxInstanceMaskParseFuncName
char customBBoxInstanceMaskParseFuncName[_MAX_STR_LENGTH]
Holds the name of the bounding box and instance mask parse function in the custom library.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:305
_NvDsPostProcessContextInitParams::labelsFilePath
char labelsFilePath[_PATH_MAX]
Holds the pathname of the labels file containing strings for the class labels.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:270
_NvDsPostProcessContextInitParams::maxBatchSize
unsigned int maxBatchSize
Holds the maximum number of frames to be inferred together in a batch.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:265
post_processor_bodypose.h
_NvDsPostProcessContextInitParams::outputLayerNames
char ** outputLayerNames
Holds a pointer to an array of pointers to output layer names.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:290
_NvDsPostProcessContextInitParams::preprocessor_support
gboolean preprocessor_support
Holds boolean value to show whether preprocessor support is there.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:319
PostProcessAlgorithm::SplitStringInt
std::set< gint > SplitStringInt(std::string input)
PostProcessAlgorithm::m_vectorProperty
std::vector< Property > m_vectorProperty
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/postprocesslib_impl.h:181
NvDsPostProcessNetworkType_Other
@ NvDsPostProcessNetworkType_Other
Specifies other.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:206
DSPostProcess_CreateParams::m_element
GstBaseTransform * m_element
Definition: sources/gst-plugins/gst-nvdspostprocess/includes/nvdspostprocesslib_interface.hpp:32
PostProcessAlgorithm::m_processLock
std::mutex m_processLock
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/postprocesslib_impl.h:175
PostProcessAlgorithm::m_stop
gboolean m_stop
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/postprocesslib_impl.h:178
_NvDsPostProcessContextInitParams::numOutputLayers
unsigned int numOutputLayers
Holds the number of output layer names.
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:292
PostProcessAlgorithm::ProcessBuffer
virtual BufferResult ProcessBuffer(GstBuffer *inbuf)
NVDSPOSTPROCESS_CLUSTER_NMS
@ NVDSPOSTPROCESS_CLUSTER_NMS
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/post_processor_struct.h:167
PostProcessAlgorithm::SplitString
std::vector< std::string > SplitString(std::string input)
GstBuffer
struct _GstBuffer GstBuffer
Definition: sources/includes/ds3d/common/idatatype.h:24
BufferResult
BufferResult
Definition: sources/gst-plugins/gst-nvdspostprocess/includes/nvdspostprocesslib_interface.hpp:24
PostProcessAlgorithm::m_processCV
std::condition_variable m_processCV
Definition: sources/gst-plugins/gst-nvdspostprocess/postprocesslib_impl/postprocesslib_impl.h:176