NVIDIA DeepStream SDK API Reference

9.1 Release
sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-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 __DEEPSTREAM_ACTION_H__
19 #define __DEEPSTREAM_ACTION_H__
20 
21 #include <cuda_runtime_api.h>
22 
23 #include <fstream>
24 #include <functional>
25 #include <gst/gst.h>
26 #include <glib.h>
27 #include <inttypes.h>
28 #include <iostream>
29 #include <math.h>
30 #include <memory>
31 #include <queue>
32 #include <sys/time.h>
33 #include <stdint.h>
34 #include <stdio.h>
35 #include <string>
36 #include <string.h>
37 #include <unordered_map>
38 #include <map>
39 #include <vector>
40 
41 #include "gstnvdsmeta.h"
42 #include "nvdspreprocess_meta.h"
43 #include "gstnvdsinfer.h"
44 
45 #ifndef PLATFORM_TEGRA
46 #include "gst-nvmessage.h"
47 #endif
48 
49 /* Print log message*/
50 #define LOG(out, level, fmt, ...) \
51  fprintf(out, "[%s: DS_3DAR] " fmt "\n", #level, ##__VA_ARGS__)
52 
53 /* Print Debug message if ENABLE_DEBUG not zero */
54 #define LOG_DEBUG(fmt, ...) \
55  if (gActionConfig.debug >= kDebugEnable) { \
56  LOG(stdout, DEBUG, fmt, ##__VA_ARGS__); \
57  }
58 
59 /* Print Error message*/
60 #define LOG_ERROR(fmt, ...) \
61  LOG(stderr, ERROR, fmt, ##__VA_ARGS__)
62 
63 template<typename T>
64 class SafePtr: public std::unique_ptr<T, std::function<void(T*)>> {
65 public:
66  SafePtr(T* p, std::function<void(T*)> freefn):
67  std::unique_ptr<T, std::function<void(T*)>>(p, freefn)
68  {}
69 };
70 
71 enum DebugLevel {
75 };
76 
77 // FPS calculation for each source stream
79  struct FpsStats{
80  double startTime = 0;
81  uint64_t sumFrames = 0;
82  float curFps = 0;
83  float avgFps = 0;
84  };
85 public:
86  FpsCalculation(uint32_t interval)
87  : _max_frame_nums(interval)
88  {}
89  float updateFps(uint32_t source_id) {
90  struct timeval time_now;
91  gettimeofday(&time_now, nullptr);
92  double now = (double)time_now.tv_sec + time_now.tv_usec / (double)1000000; // second
93  float fps = -1.0f;
94  auto iSrc = _timestamps.find(source_id);
95  if (iSrc != _timestamps.end()) {
96  auto & tms = iSrc->second;
97  fps = tms.size() / (now - tms.front());
98  while (tms.size() >= _max_frame_nums) {
99  tms.pop();
100  }
101  auto & stats = _fpsStats[source_id];
102  stats.curFps = fps;
103  stats.avgFps = stats.sumFrames / (now - stats.startTime);
104  stats.sumFrames++;
105  } else {
106  iSrc = _timestamps.emplace(source_id, std::queue<double>()).first;
107  _fpsStats.emplace(source_id, FpsStats{now, 1, 0, 0});
108  }
109  iSrc->second.push(now);
110 
111  return fps;
112  }
113 
114  // get dataset of current fps and average fps
115  void getAllFps(std::vector<std::pair<float, float>>& fps) {
116  for (auto& s: _fpsStats) {
117  fps.emplace_back(std::make_pair(s.second.curFps, s.second.avgFps));
118  }
119  }
120 private:
121  std::unordered_map<uint32_t, std::queue<double>> _timestamps;
122  uint32_t _max_frame_nums = 50;
123  std::map<uint32_t, FpsStats> _fpsStats;
124 };
125 
127 {
128  // stream source list
129  std::vector<std::string> uri_list;
130 
131  // display sink settings
132  gboolean display_sync = true;
133 
134  // nvdspreprocess plugin config file path
135  std::string preprocess_config;
136  // nvinfer plugin config file path
137  std::string infer_config;
138  // nvinferserver(ds-triton) plugin config file path
139  std::string triton_infer_config;
140 
141  // nvstreammux settings
142  uint32_t muxer_height = 720;
143  uint32_t muxer_width = 1280;
144  // batched-push-timeout in usec, default value 40ms
145  int32_t muxer_batch_timeout = 40000;
146 
147  // tiler settings
148  uint32_t tiler_height = 720;
149  uint32_t tiler_width = 1280;
150 
151  // debug level, disabled by default
153 
154  // enable fps print on screen. enabled by default
155  gboolean enableFps = TRUE;
156 
157  // enable to use fakesink
158  gboolean useFakeSink = FALSE;
159 };
160 
161 // parse action recognition config into NvDsARConfig
162 bool parse_action_config(const char* action_config_path, NvDsARConfig& config);
163 
164 #endif
FpsCalculation::updateFps
float updateFps(uint32_t source_id)
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:89
NvDsARConfig::infer_config
std::string infer_config
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:137
FpsCalculation
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:78
FpsCalculation::getAllFps
void getAllFps(std::vector< std::pair< float, float >> &fps)
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:115
NvDsARConfig::muxer_width
uint32_t muxer_width
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:143
NvDsARConfig::triton_infer_config
std::string triton_infer_config
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:139
NvDsARConfig::tiler_height
uint32_t tiler_height
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:148
NvDsARConfig::uri_list
std::vector< std::string > uri_list
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:129
NvDsARConfig::tiler_width
uint32_t tiler_width
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:149
SafePtr::SafePtr
SafePtr(T *p, std::function< void(T *)> freefn)
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:66
NvDsARConfig::muxer_height
uint32_t muxer_height
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:142
NvDsARConfig::preprocess_config
std::string preprocess_config
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:135
NvDsARConfig::enableFps
gboolean enableFps
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:155
kDebugDisable
@ kDebugDisable
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:72
NvDsARConfig::display_sync
gboolean display_sync
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:132
SafePtr
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:64
kDebugVerbose
@ kDebugVerbose
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:74
NvDsARConfig
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:126
FpsCalculation::FpsCalculation
FpsCalculation(uint32_t interval)
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:86
NvDsARConfig::debug
DebugLevel debug
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:152
parse_action_config
bool parse_action_config(const char *action_config_path, NvDsARConfig &config)
NvDsARConfig::muxer_batch_timeout
int32_t muxer_batch_timeout
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:145
kDebugEnable
@ kDebugEnable
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:73
DebugLevel
DebugLevel
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:71
NvDsARConfig::useFakeSink
gboolean useFakeSink
Definition: sources/apps/sample_apps/deepstream-3d-action-recognition/deepstream_action.h:158