NVIDIA DeepStream SDK API Reference

8.0 Release
post_processor_bodypose.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2022-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 __POST_PROCESSOR_BODYPOSE_HPP__
25 #define __POST_PROCESSOR_BODYPOSE_HPP__
26 
27 #include "post_processor.h"
28 #include "nvdsmeta_schema.h"
29 
30 #include <cmath>
31 
33 public:
36  reset(30.0f /* Hz */, 0.1f /* Hz */, 0.09f /* ??? */, 0.5f /* Hz */);
37  }
43  OneEuroFilter(float dataUpdateRate, float minCutoffFreq, float cutoffSlope, float derivCutoffFreq) {
44  reset(dataUpdateRate, minCutoffFreq, cutoffSlope, derivCutoffFreq);
45  }
51  void reset(float dataUpdateRate, float minCutoffFreq, float cutoffSlope, float derivCutoffFreq) {
52  reset(); _rate = dataUpdateRate; _minCutoff = minCutoffFreq; _beta = cutoffSlope; _dCutoff = derivCutoffFreq;
53  }
55  void reset() { _firstTime = true; _xFilt.reset(); _dxFilt.reset(); }
59  float filter(float x)
60  {
61  float dx, edx, cutoff;
62  if (_firstTime) {
63  _firstTime = false;
64  dx = 0;
65  } else {
66  dx = (x - _xFilt.hatXPrev()) * _rate;
67  }
68  edx = _dxFilt.filter(dx, alpha(_rate, _dCutoff));
69  cutoff = _minCutoff + _beta * fabsf(edx);
70  return _xFilt.filter(x, alpha(_rate, cutoff));
71  }
72 
73 
74 private:
75  class LowPassFilter {
76  public:
77  LowPassFilter() { reset(); }
78  void reset() { _firstTime = true; }
79  float hatXPrev() const { return _hatXPrev; }
80  float filter(float x, float alpha){
81  if (_firstTime) {
82  _firstTime = false;
83  _hatXPrev = x;
84  }
85  float hatX = alpha * x + (1.f - alpha) * _hatXPrev;
86  _hatXPrev = hatX;
87  return hatX;
88 
89  }
90  private:
91  float _hatXPrev;
92  bool _firstTime;
93  };
94  inline float alpha(float rate, float cutoff) {
95  const float kOneOverTwoPi = 0.15915494309189533577f; // 1 / (2 * pi)
96  // The paper has 4 divisions, but we only use one
97  // float tau = kOneOverTwoPi / cutoff, te = 1.f / rate;
98  // return 1.f / (1.f + tau / te);
99  return cutoff / (rate * kOneOverTwoPi + cutoff);
100 }
101  bool _firstTime;
102  float _rate, _minCutoff, _dCutoff, _beta;
103  LowPassFilter _xFilt, _dxFilt;
104 };
105 
106 constexpr int NVDS_OBJECT_TYPE_PERSON_EXT_POSE = 0x103;
107 
108 typedef struct NvDsPersonPoseExt {
109  gint num_poses;
112 
113 
115 
116 public:
117  BodyPoseModelPostProcessor(int id, int gpuId = 0)
119 
120  ~BodyPoseModelPostProcessor() override = default;
121 
123  initResource(NvDsPostProcessContextInitParams& initParams) override;
124 
125  NvDsPostProcessStatus parseEachFrame(const std::vector <NvDsInferLayerInfo>&
126  outputLayers,
127  NvDsPostProcessFrameOutput &result) override;
128 
129  void
130  attachMetadata (NvBufSurface *surf, gint batch_idx,
131  NvDsBatchMeta *batch_meta,
132  NvDsFrameMeta *frame_meta,
133  NvDsObjectMeta *object_meta,
134  NvDsObjectMeta *parent_obj_meta,
135  NvDsPostProcessFrameOutput & detection_output,
136  NvDsPostProcessDetectionParams *all_params,
137  std::set <gint> & filterOutClassIds,
138  int32_t unique_id,
139  gboolean output_instance_mask,
140  gboolean process_full_frame,
141  float segmentationThreshold,
142  gboolean maintain_aspect_ratio,
143  NvDsRoiMeta *roi_meta,
144  gboolean symmetric_padding)
145  override;
146 
147  void releaseFrameOutput(NvDsPostProcessFrameOutput& frameOutput) override;
148 private:
149  NvDsPostProcessStatus fillBodyPoseOutput(
150  const std::vector<NvDsInferLayerInfo>& outputLayers,
152  void osdBody(NvDsFrameMeta* frame_meta,
153  NvDsBatchMeta *bmeta,
154  NvDsDisplayMeta *dmeta,
155  const int numKeyPoints,
156  const float keypoints[],
157  const float keypoints_confidence[]);
158 
159  void movenetposeFromTensorMeta(
160  const std::vector<NvDsInferLayerInfo>& outputLayers,
162 
163  float median(std::vector<float>& v);
164 
165  float m_ClassificationThreshold = 0.50;
166  std::unordered_map<gint , std::vector<OneEuroFilter>> m_filter_pose;
167  OneEuroFilter m_filterRootDepth; // Root node in pose25d.
168  fpos_t m_fp_25_pos;
169 };
170 
171 #endif
NvDsDisplayMeta
Holds display metadata that the user can specify in the frame.
Definition: nvdsmeta.h:452
OneEuroFilter::reset
void reset(float dataUpdateRate, float minCutoffFreq, float cutoffSlope, float derivCutoffFreq)
Reset all parameters of the filter.
Definition: post_processor_bodypose.h:51
BodyPoseModelPostProcessor::initResource
NvDsPostProcessStatus initResource(NvDsPostProcessContextInitParams &initParams) override
NvDsPostProcessDetectionParams
Holds detection and bounding box grouping parameters.
Definition: post_processor_struct.h:218
NvDsPersonPoseExt
Definition: post_processor_bodypose.h:108
ModelPostProcessor
Definition: post_processor.h:57
BodyPoseModelPostProcessor::releaseFrameOutput
void releaseFrameOutput(NvDsPostProcessFrameOutput &frameOutput) override
NvBufSurface
Holds information about batched buffers.
Definition: nvbufsurface.h:577
post_processor.h
BodyPoseModelPostProcessor::parseEachFrame
NvDsPostProcessStatus parseEachFrame(const std::vector< NvDsInferLayerInfo > &outputLayers, NvDsPostProcessFrameOutput &result) override
BodyPoseModelPostProcessor::~BodyPoseModelPostProcessor
~BodyPoseModelPostProcessor() override=default
NvDsPostProcessStatus
NvDsPostProcessStatus
Enum for the status codes returned by NvDsPostProcessAlgorithm.
Definition: post_processor_struct.h:84
OneEuroFilter::reset
void reset()
Reset only the initial condition of the filter, leaving parameters the same.
Definition: post_processor_bodypose.h:55
BodyPoseModelPostProcessor
Definition: post_processor_bodypose.h:114
BodyPoseModelPostProcessor::attachMetadata
void attachMetadata(NvBufSurface *surf, gint batch_idx, NvDsBatchMeta *batch_meta, NvDsFrameMeta *frame_meta, NvDsObjectMeta *object_meta, NvDsObjectMeta *parent_obj_meta, NvDsPostProcessFrameOutput &detection_output, NvDsPostProcessDetectionParams *all_params, std::set< gint > &filterOutClassIds, int32_t unique_id, gboolean output_instance_mask, gboolean process_full_frame, float segmentationThreshold, gboolean maintain_aspect_ratio, NvDsRoiMeta *roi_meta, gboolean symmetric_padding) override
OneEuroFilter
Definition: post_processor_bodypose.h:32
NvDsPersonPoseExt::poses
NvDsJoints * poses
Definition: post_processor_bodypose.h:110
_NvDsBatchMeta
Holds information about a formed batch containing frames from different sources.
Definition: nvdsmeta.h:257
_NvDsPostProcessContextInitParams
Holds the initialization parameters required for the NvDsPostProcessContext interface.
Definition: post_processor_struct.h:262
OneEuroFilter::OneEuroFilter
OneEuroFilter()
Default constructor.
Definition: post_processor_bodypose.h:35
OneEuroFilter::OneEuroFilter
OneEuroFilter(float dataUpdateRate, float minCutoffFreq, float cutoffSlope, float derivCutoffFreq)
Constructor.
Definition: post_processor_bodypose.h:43
NvDsPostProcessFrameOutput
Holds the information inferred by the network on one frame.
Definition: post_processor_struct.h:508
OneEuroFilter::filter
float filter(float x)
Apply the one euro filter to the given input.
Definition: post_processor_bodypose.h:59
NvDsPostProcessBodyPoseOutput
Holds information parsed from bodypose network output for one frame.
Definition: post_processor_struct.h:493
NVDS_OBJECT_TYPE_PERSON_EXT_POSE
constexpr int NVDS_OBJECT_TYPE_PERSON_EXT_POSE
Definition: post_processor_bodypose.h:106
NvDsPostProcessNetworkType_BodyPose
@ NvDsPostProcessNetworkType_BodyPose
Bodypose 3D.
Definition: post_processor_struct.h:207
NvDsRoiMeta
Holds Information about ROI Metadata.
Definition: nvds_roi_meta.h:90
NvDsPersonPoseExt
struct NvDsPersonPoseExt NvDsPersonPoseExt
NvDsJoints
Holds a body pose's joint points.
Definition: nvdsmeta_schema.h:252
_NvDsFrameMeta
Holds metadata for a frame in a batch.
Definition: nvdsmeta.h:301
NvDsPersonPoseExt::num_poses
gint num_poses
Definition: post_processor_bodypose.h:109
nvdsmeta_schema.h
BodyPoseModelPostProcessor::BodyPoseModelPostProcessor
BodyPoseModelPostProcessor(int id, int gpuId=0)
Definition: post_processor_bodypose.h:117
_NvDsObjectMeta
Holds metadata for an object in the frame.
Definition: nvdsmeta.h:360