NVIDIA DeepStream SDK API Reference

6.4 Release
postprocesslib_impl.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2022 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"
35 
36 #define FORMAT_NV12 "NV12"
37 #define FORMAT_RGBA "RGBA"
38 #define FORMAT_I420 "I420"
39 #define GST_CAPS_FEATURE_MEMORY_NVMM "memory:NVMM"
40 
41 /* Strcture used to share between the threads */
42 struct PacketInfo {
44  guint frame_num;
45 };
46 
48 {
49 public:
50 
52  m_vectorProperty.clear();
53  outputthread_stopped = false;
54 
55  if (createParams){
56  m_element = createParams->m_element;
57  m_gpuId = createParams->m_gpuId;
58  m_cudaStream = createParams->m_cudaStream;
59  }
60  else {
61  m_element = NULL;
62  m_gpuId = 0;
63  m_cudaStream = 0;
64  }
65  m_outputThread = new std::thread(&PostProcessAlgorithm::OutputThread, this);
66  m_initParams.uniqueID = 0;
67  m_initParams.maxBatchSize = 1;
68 
69  std::memset(m_initParams.labelsFilePath,0, sizeof(m_initParams.labelsFilePath));
71  std::memset(m_initParams.customClassifierParseFuncName, 0,
72  sizeof(m_initParams.customClassifierParseFuncName)-1);
73  std::memset(m_initParams.customBBoxParseFuncName, 0,
74  sizeof(m_initParams.customBBoxParseFuncName)-1);
75  std::memset(m_initParams.customBBoxInstanceMaskParseFuncName, 0,
76  sizeof(m_initParams.customBBoxInstanceMaskParseFuncName)-1);
77 
78 
80  m_initParams.numDetectedClasses = 0;
81 
84  m_initParams.perClassDetectionParams = NULL;
85 
88  m_initParams.classifierThreshold = 0.5;
89 
90  m_initParams.segmentationThreshold = 0.5;
91 
93  m_initParams.outputLayerNames = NULL;
95  m_initParams.numOutputLayers = 0;
96 
98  m_initParams.gpuID = 0;
99 
100  m_initParams.inferInputDims = {0,0,0};
101 
104 
105  m_initParams.classifier_type = NULL;
106  }
107 
108  /* Pass GST events to the library */
109  virtual bool HandleEvent(GstEvent *event);
110 
111  /* Read Config file */
112  virtual bool SetConfigFile (const gchar *config_file);
113 
114  /* Process Incoming Buffer */
115  virtual BufferResult ProcessBuffer(GstBuffer *inbuf);
116 
117  std::vector<std::string> SplitString (std::string input);
118 
119  std::set<gint> SplitStringInt (std::string input);
120 
121  bool GetAbsFilePath (const gchar * cfg_file_path, const gchar * file_path,
122  char *abs_path_str);
123 
124  gboolean hw_caps;
125 
126  /* Deinit members */
128 
129 private:
130  /* Helper Function to Extract Batch Meta from buffer */
131  NvDsBatchMeta * GetNVDS_BatchMeta (GstBuffer *buffer);
132 
133  /* Output Processing Thread, push buffer to downstream */
134  void OutputThread(void);
135 
136  /* Helper function to Dump NvBufSurface RAW content */
137  void DumpNvBufSurface (NvBufSurface *in_surface, NvDsBatchMeta *batch_meta);
138 
139  bool ParseLabelsFile(const std::string& labelsFilePath);
140  bool ParseConfAttr (YAML::Node node, gint64 class_id, NvDsPostProcessDetectionParams& params);
141  NvDsPostProcessStatus preparePostProcess();
142 
143  NvDsPostProcessNetworkType m_networkType;
144  NvDsPostProcessClusterMode m_clusterMode;
145  gfloat m_classifierThreshold;
146  gfloat m_segmentationThreshold;
147  gint m_numDetectedClasses;
148  gint m_processMode = 1;
149  guint m_gieUniqueId = 0;
150  gboolean m_isClassifier = 0;
151  gboolean m_releaseTensorMeta = FALSE;
152  gboolean m_outputInstanceMask = FALSE;
153  std::string m_classifierType;
154  std::set <gint> m_filterOutClassIds;
155  std::set <gint> m_operateOnClassIds;
156  std::vector <std::string> m_outputBlobNames;
157 
158  std::vector<NvDsPostProcessInstanceMaskInfo> m_InstanceMaskList;
159  std::unordered_map <gint, NvDsPostProcessDetectionParams> m_detectorClassAttr;
160  /* Vector of NvDsPostProcessInstanceMaskInfo vectors for each class. */
161  std::vector<std::vector<NvDsPostProcessInstanceMaskInfo>> m_PerClassInstanceMaskList;
162  std::vector<std::vector<std::string>> m_Labels;
163 
164  std::unique_ptr<ModelPostProcessor> m_Postprocessor;
166 public:
167  guint source_id = 0;
168  guint m_frameNum = 0;
169  bool outputthread_stopped = false;
170 
171  /* Output Thread Pointer */
172  std::thread *m_outputThread = NULL;
173 
174  /* Queue and Lock Management */
175  std::queue<PacketInfo> m_processQ;
176  std::mutex m_processLock;
177  std::condition_variable m_processCV;
178  /* Aysnc Stop Handling */
179  gboolean m_stop = FALSE;
180 
181  /* Vector Containing Key:Value Pair of Custom Lib Properties */
182  std::vector<Property> m_vectorProperty;
183 
184 };
185 
187 // Create Custom Algorithm / Library Context
188 
189 
190 #endif
PostProcessAlgorithm::PostProcessAlgorithm
PostProcessAlgorithm(DSPostProcess_CreateParams *createParams)
Definition: postprocesslib_impl.h:51
PostProcessAlgorithm::m_vectorProperty
std::vector< Property > m_vectorProperty
Definition: postprocesslib_impl.h:182
_NvDsPostProcessContextInitParams::clusterMode
NvDsPostProcessClusterMode clusterMode
Holds the type of clustering mode.
Definition: post_processor_struct.h:305
PostProcessAlgorithm::source_id
guint source_id
Definition: postprocesslib_impl.h:167
post_processor_instance_segment.h
PacketInfo
Definition: postprocesslib_impl.h:42
post_processor_segmentation.h
PostProcessAlgorithm::m_frameNum
guint m_frameNum
Definition: postprocesslib_impl.h:168
_NvDsPostProcessContextInitParams::labelsFilePath
char labelsFilePath[_PATH_MAX]
Holds the pathname of the labels file containing strings for the class labels.
Definition: post_processor_struct.h:274
IDSPostProcessLibrary
Definition: nvdspostprocesslib_interface.hpp:52
_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:309
PostProcessAlgorithm::hw_caps
gboolean hw_caps
Definition: postprocesslib_impl.h:124
_NvDsPostProcessContextInitParams::classifierThreshold
float classifierThreshold
Holds the minimum confidence threshold for the classifier to consider a label valid.
Definition: post_processor_struct.h:289
_NvDsPostProcessContextInitParams::inferInputDims
NvDsInferDimsCHW inferInputDims
Inference input dimensions for runtime engine.
Definition: post_processor_struct.h:302
PostProcessAlgorithm::HandleEvent
virtual bool HandleEvent(GstEvent *event)
NvDsPostProcessDetectionParams
Holds detection and bounding box grouping parameters.
Definition: post_processor_struct.h:216
NvDsPostProcessNetworkType
NvDsPostProcessNetworkType
Defines network types.
Definition: post_processor_struct.h:191
PostProcessAlgorithm::~PostProcessAlgorithm
~PostProcessAlgorithm()
NvBufSurface
Holds information about batched buffers.
Definition: nvbufsurface.h:503
post_processor.h
BufferResult
BufferResult
Copyright (c) 2023, NVIDIA CORPORATION.
Definition: gst-nvdsA2Vtemplate/includes/nvdscustomlib_interface.hpp:32
_NvDsPostProcessContextInitParams::numDetectedClasses
unsigned int numDetectedClasses
Holds the number of classes detected by a detector network.
Definition: post_processor_struct.h:281
_NvDsPostProcessContextInitParams::perClassDetectionParams
NvDsPostProcessDetectionParams * perClassDetectionParams
Holds per-class detection parameters.
Definition: post_processor_struct.h:285
nvdspostprocesslib_base.hpp
_NvDsPostProcessContextInitParams::uniqueID
unsigned int uniqueID
Holds a unique identifier for the instance.
Definition: post_processor_struct.h:264
NvDsPostProcessStatus
NvDsPostProcessStatus
Enum for the status codes returned by NvDsPostProcessAlgorithm.
Definition: post_processor_struct.h:84
PostProcessAlgorithm
Definition: postprocesslib_impl.h:47
PacketInfo::frame_num
guint frame_num
Definition: postprocesslib_impl.h:44
DSPostProcessLibraryBase::m_cudaStream
cudaStream_t m_cudaStream
Definition: nvdspostprocesslib_base.hpp:55
DSPostProcess_CreateParams::m_gpuId
guint m_gpuId
Definition: nvdspostprocesslib_interface.hpp:38
DSPostProcessLibraryBase
Copyright (c) 2022, NVIDIA CORPORATION.
Definition: nvdspostprocesslib_base.hpp:32
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:39
DSPostProcess_CreateParams::m_element
GstBaseTransform * m_element
Definition: nvdspostprocesslib_interface.hpp:37
_NvDsPostProcessContextInitParams::segmentationThreshold
float segmentationThreshold
Definition: post_processor_struct.h:291
_NvDsPostProcessContextInitParams::gpuID
unsigned int gpuID
Holds the ID of the GPU which is to run the inference.
Definition: post_processor_struct.h:299
PostProcessAlgorithm::SetConfigFile
virtual bool SetConfigFile(const gchar *config_file)
PostProcessAlgorithm::m_processQ
std::queue< PacketInfo > m_processQ
Definition: postprocesslib_impl.h:175
_NvDsBatchMeta
Holds information about a formed batch containing frames from different sources.
Definition: nvdsmeta.h:240
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:260
_NvDsPostProcessContextInitParams::customClassifierParseFuncName
char customClassifierParseFuncName[_MAX_STR_LENGTH]
Name of the custom classifier attribute parsing function in the custom library.
Definition: post_processor_struct.h:316
DSPostProcess_CreateParams
Definition: nvdspostprocesslib_interface.hpp:36
post_processor_classify.h
DSPostProcessLibraryBase::m_gpuId
guint m_gpuId
GPU ID on which we expect to execute the algorithm.
Definition: nvdspostprocesslib_base.hpp:53
DSPostProcessLibraryBase::m_element
GstBaseTransform * m_element
Definition: nvdspostprocesslib_base.hpp:50
_NvDsPostProcessContextInitParams::classifier_type
char * classifier_type
Definition: post_processor_struct.h:321
PostProcessAlgorithm::outputthread_stopped
bool outputthread_stopped
Definition: postprocesslib_impl.h:169
_NvDsPostProcessContextInitParams::networkType
NvDsPostProcessNetworkType networkType
Holds the network type.
Definition: post_processor_struct.h:278
_NvDsPostProcessContextInitParams::outputLayerNames
char ** outputLayerNames
Holds a pointer to an array of pointers to output layer names.
Definition: post_processor_struct.h:294
_NvDsPostProcessContextInitParams::maxBatchSize
unsigned int maxBatchSize
Holds the maximum number of frames to be inferred together in a batch.
Definition: post_processor_struct.h:269
post_processor_detect.h
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:313
PostProcessAlgorithm::m_outputThread
std::thread * m_outputThread
Definition: postprocesslib_impl.h:172
PacketInfo::inbuf
GstBuffer * inbuf
Definition: postprocesslib_impl.h:43
PostProcessAlgorithm::m_processLock
std::mutex m_processLock
Definition: postprocesslib_impl.h:176
PostProcessAlgorithm::m_stop
gboolean m_stop
Definition: postprocesslib_impl.h:179
NvDsPostProcessNetworkType_Other
@ NvDsPostProcessNetworkType_Other
Specifies other.
Definition: post_processor_struct.h:210
_NvDsPostProcessContextInitParams::numOutputLayers
unsigned int numOutputLayers
Holds the number of output layer names.
Definition: post_processor_struct.h:296
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:177