NVIDIA DeepStream SDK API Reference

6.4 Release
deepstream_action.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2022, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __DEEPSTREAM_ACTION_H__
24 #define __DEEPSTREAM_ACTION_H__
25 
26 #include <cuda_runtime_api.h>
27 
28 #include <fstream>
29 #include <functional>
30 #include <gst/gst.h>
31 #include <glib.h>
32 #include <inttypes.h>
33 #include <iostream>
34 #include <math.h>
35 #include <memory>
36 #include <queue>
37 #include <sys/time.h>
38 #include <stdint.h>
39 #include <stdio.h>
40 #include <string>
41 #include <string.h>
42 #include <unordered_map>
43 #include <map>
44 #include <vector>
45 
46 #include "gstnvdsmeta.h"
47 #include "nvdspreprocess_meta.h"
48 #include "gstnvdsinfer.h"
49 
50 #ifndef PLATFORM_TEGRA
51 #include "gst-nvmessage.h"
52 #endif
53 
54 /* Print log message*/
55 #define LOG(out, level, fmt, ...) \
56  fprintf(out, "[%s: DS_3DAR] " fmt "\n", #level, ##__VA_ARGS__)
57 
58 /* Print Debug message if ENABLE_DEBUG not zero */
59 #define LOG_DEBUG(fmt, ...) \
60  if (gActionConfig.debug >= kDebugEnable) { \
61  LOG(stdout, DEBUG, fmt, ##__VA_ARGS__); \
62  }
63 
64 /* Print Error message*/
65 #define LOG_ERROR(fmt, ...) \
66  LOG(stderr, ERROR, fmt, ##__VA_ARGS__)
67 
68 template<typename T>
69 class SafePtr: public std::unique_ptr<T, std::function<void(T*)>> {
70 public:
71  SafePtr(T* p, std::function<void(T*)> freefn):
72  std::unique_ptr<T, std::function<void(T*)>>(p, freefn)
73  {}
74 };
75 
76 enum DebugLevel {
80 };
81 
82 // FPS calculation for each source stream
84  struct FpsStats{
85  double startTime = 0;
86  uint64_t sumFrames = 0;
87  float curFps = 0;
88  float avgFps = 0;
89  };
90 public:
91  FpsCalculation(uint32_t interval)
92  : _max_frame_nums(interval)
93  {}
94  float updateFps(uint32_t source_id) {
95  struct timeval time_now;
96  gettimeofday(&time_now, nullptr);
97  double now = (double)time_now.tv_sec + time_now.tv_usec / (double)1000000; // second
98  float fps = -1.0f;
99  auto iSrc = _timestamps.find(source_id);
100  if (iSrc != _timestamps.end()) {
101  auto & tms = iSrc->second;
102  fps = tms.size() / (now - tms.front());
103  while (tms.size() >= _max_frame_nums) {
104  tms.pop();
105  }
106  auto & stats = _fpsStats[source_id];
107  stats.curFps = fps;
108  stats.avgFps = stats.sumFrames / (now - stats.startTime);
109  stats.sumFrames++;
110  } else {
111  iSrc = _timestamps.emplace(source_id, std::queue<double>()).first;
112  _fpsStats.emplace(source_id, FpsStats{now, 1, 0, 0});
113  }
114  iSrc->second.push(now);
115 
116  return fps;
117  }
118 
119  // get dataset of current fps and average fps
120  void getAllFps(std::vector<std::pair<float, float>>& fps) {
121  for (auto& s: _fpsStats) {
122  fps.emplace_back(std::make_pair(s.second.curFps, s.second.avgFps));
123  }
124  }
125 private:
126  std::unordered_map<uint32_t, std::queue<double>> _timestamps;
127  uint32_t _max_frame_nums = 50;
128  std::map<uint32_t, FpsStats> _fpsStats;
129 };
130 
132 {
133  // stream source list
134  std::vector<std::string> uri_list;
135 
136  // display sink settings
137  gboolean display_sync = true;
138 
139  // nvdspreprocess plugin config file path
140  std::string preprocess_config;
141  // nvinfer plugin config file path
142  std::string infer_config;
143  // nvinferserver(ds-triton) plugin config file path
144  std::string triton_infer_config;
145 
146  // nvstreammux settings
147  uint32_t muxer_height = 720;
148  uint32_t muxer_width = 1280;
149  // batched-push-timeout in usec, default value 40ms
150  int32_t muxer_batch_timeout = 40000;
151 
152  // tiler settings
153  uint32_t tiler_height = 720;
154  uint32_t tiler_width = 1280;
155 
156  // debug level, disabled by default
158 
159  // enable fps print on screen. enabled by default
160  gboolean enableFps = TRUE;
161 
162  // enable to use fakesink
163  gboolean useFakeSink = FALSE;
164 };
165 
166 // parse action recognition config into NvDsARConfig
167 bool parse_action_config(const char* action_config_path, NvDsARConfig& config);
168 
169 #endif
kDebugDisable
@ kDebugDisable
Definition: deepstream_action.h:77
FpsCalculation::updateFps
float updateFps(uint32_t source_id)
Definition: deepstream_action.h:94
kDebugEnable
@ kDebugEnable
Definition: deepstream_action.h:78
NvDsARConfig::infer_config
std::string infer_config
Definition: deepstream_action.h:142
FpsCalculation
Definition: deepstream_action.h:83
NvDsARConfig::uri_list
std::vector< std::string > uri_list
Definition: deepstream_action.h:134
gstnvdsinfer.h
FpsCalculation::getAllFps
void getAllFps(std::vector< std::pair< float, float >> &fps)
Definition: deepstream_action.h:120
NvDsARConfig::muxer_width
uint32_t muxer_width
Definition: deepstream_action.h:148
DebugLevel
DebugLevel
Definition: deepstream_action.h:76
NvDsARConfig::triton_infer_config
std::string triton_infer_config
Definition: deepstream_action.h:144
NvDsARConfig::tiler_height
uint32_t tiler_height
Definition: deepstream_action.h:153
NvDsARConfig::tiler_width
uint32_t tiler_width
Definition: deepstream_action.h:154
SafePtr::SafePtr
SafePtr(T *p, std::function< void(T *)> freefn)
Definition: deepstream_action.h:71
NvDsARConfig::muxer_height
uint32_t muxer_height
Definition: deepstream_action.h:147
NvDsARConfig::preprocess_config
std::string preprocess_config
Definition: deepstream_action.h:140
gstnvdsmeta.h
kDebugVerbose
@ kDebugVerbose
Definition: deepstream_action.h:79
NvDsARConfig::enableFps
gboolean enableFps
Definition: deepstream_action.h:160
NvDsARConfig::display_sync
gboolean display_sync
Definition: deepstream_action.h:137
SafePtr
Definition: deepstream_action.h:69
NvDsARConfig
Definition: deepstream_action.h:131
FpsCalculation::FpsCalculation
FpsCalculation(uint32_t interval)
Definition: deepstream_action.h:91
parse_action_config
bool parse_action_config(const char *action_config_path, NvDsARConfig &config)
NvDsARConfig::debug
DebugLevel debug
Definition: deepstream_action.h:157
gst-nvmessage.h
NvDsARConfig::muxer_batch_timeout
int32_t muxer_batch_timeout
Definition: deepstream_action.h:150
NvDsARConfig::useFakeSink
gboolean useFakeSink
Definition: deepstream_action.h:163