NVIDIA DeepStream SDK API Reference

9.2 Release
sources/libs/nvmultiobjecttracker/include/modules/NvMultiObjectTracker/NvTrackerParams.hpp
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 #ifndef NVTRACKERPARAMS_HPP_
18 #define NVTRACKERPARAMS_HPP_
19 
20 #include <cerrno>
21 #include <cstdlib>
22 #include <limits.h>
23 #include <string>
24 
25 #ifdef _WIN32
26 #include <filesystem>
27 namespace NvTrackerFilesystem = std::filesystem;
28 #else
29 #include <experimental/filesystem>
30 #endif
31 #include <yaml-cpp/yaml.h>
32 #include "modules/utility/debug_common.hpp"
33 
36 {
37 public:
40 
45  virtual void read(const YAML::Node& fn) = 0;
46 
55  static std::string resolvePathRelativeToConfig(const std::string &configFilePath, const std::string &filePath)
56  {
57  if (filePath.empty() || configFilePath.empty())
58  {
59  return filePath;
60  }
61 
62 #ifdef _WIN32
63  std::filesystem::path requestedPath(filePath);
64  if (requestedPath.is_absolute()) {
65  return requestedPath.string();
66  }
67  return (std::filesystem::absolute(std::filesystem::path(configFilePath)).parent_path() /
68  requestedPath).string();
69 #else
70  char resolved[PATH_MAX + 1];
71 
72  if (filePath[0] == '/')
73  {
74  if (realpath(filePath.c_str(), resolved) != nullptr)
75  {
76  return std::string(resolved);
77  }
78  if (errno == ENOENT)
79  {
80  return filePath;
81  }
82  return filePath;
83  }
84 
85  char absCfg[PATH_MAX + 1];
86  if (realpath(configFilePath.c_str(), absCfg) == nullptr)
87  {
88  return filePath;
89  }
90 
91  std::string cfgDir(absCfg);
92  const size_t last = cfgDir.find_last_of('/');
93  if (last != std::string::npos)
94  {
95  cfgDir.resize(last + 1);
96  }
97  else
98  {
99  cfgDir.clear();
100  }
101 
102  const std::string combined = cfgDir + filePath;
103  if (realpath(combined.c_str(), resolved) != nullptr)
104  {
105  return std::string(resolved);
106  }
107  if (errno == ENOENT)
108  {
109  return combined;
110  }
111  return combined;
112 #endif
113  }
114 
121  static const YAML::Node getConfigRoot(const std::string &configFilePath, const bool printMessage = false)
122  {
123 
124  if(configFilePath.size() == 0)
125  {
126  if (printMessage)
127  {
128  std::cout << "[NvTrackerParams::getConfigRoot()] !!![WARNING] Empty config file path is provided. Will go ahead with default values" << std::endl;
129  }
130  }
131  else if(!
132 #ifdef _WIN32
133  std::filesystem::exists(configFilePath)
134 #else
135  std::experimental::filesystem::exists(configFilePath)
136 #endif
137  )
138  {
139  if (printMessage)
140  {
141  std::cout << "[NvTrackerParams::getConfigRoot()] !!![WARNING] File doesn't exist. Will go ahead with default values" << std::endl;
142  }
143  }
144  else
145  {
146  try
147  {
148  YAML::Node configFn = YAML::LoadFile(configFilePath);
149 
150  if(configFn.size() > 0)
151  {
152  return configFn;
153  }
154  else
155  {
156  if (printMessage)
157  {
158  std::cout << "[NvTrackerParams::getConfigRoot()] !!![WARNING] Can't open config file (" << configFilePath << "). Will go ahead with default values" << std::endl;
159  }
160  }
161  }
162  catch (std::exception& e)
163  {
164  if (printMessage)
165  {
166  CLOG(
167  std::string( "[NvTrackerParams::getConfigRoot()] !!![WARNING] Invalid low-level config file caused an exception, but will go ahead with the default config values" ).c_str()
168  );
169  CLOG(e.what());
170  }
171  }
172  }
173 
174  return YAML::Node();
175  }
176 };
177 
180 public:
185  static bool isValidName(const std::string& name)
186  {
187  if (name.size() <= 5)
188  {
189  return false;
190  }
191  else if (name.substr(0, 5) != "model")
192  {
193  return false;
194  }
195  else
196  {
197  for (uint ind = 5; ind < name.size(); ind++)
198  {
199  if (!std::isdigit(name.at(ind)) )
200  {
201  return false;
202  }
203  }
204  }
205  return true;
206  }
207 
212  static std::string generateName(const int index)
213  {
214  return "model" + std::to_string(index);
215  }
216 };
217 
218 #endif // NVTRACKERPARAMS_HPP_
NvTrackerParams::read
virtual void read(const YAML::Node &fn)=0
Read tracker parameters from a YAML node.
NvTrackerParams::resolvePathRelativeToConfig
static std::string resolvePathRelativeToConfig(const std::string &configFilePath, const std::string &filePath)
Resolve a configured path relative to the tracker configuration file.
Definition: sources/libs/nvmultiobjecttracker/include/modules/NvMultiObjectTracker/NvTrackerParams.hpp:55
NvTrackerParams::getConfigRoot
static const YAML::Node getConfigRoot(const std::string &configFilePath, const bool printMessage=false)
Load the root YAML node from a tracker configuration file.
Definition: sources/libs/nvmultiobjecttracker/include/modules/NvMultiObjectTracker/NvTrackerParams.hpp:121
uint
unsigned int uint
Definition: sources/libs/nvmultiobjecttracker/include/modules/cuDCFv2/helper_math.h:25
ReIDModelNameParams::generateName
static std::string generateName(const int index)
Generate a model name from a numeric index.
Definition: sources/libs/nvmultiobjecttracker/include/modules/NvMultiObjectTracker/NvTrackerParams.hpp:212
NvTrackerParams
Provides common loading helpers for tracker configuration parameters.
Definition: sources/libs/nvmultiobjecttracker/include/modules/NvMultiObjectTracker/NvTrackerParams.hpp:35
CLOG
#define CLOG(message)
Definition: sources/libs/nvmultiobjecttracker/include/modules/utility/debug_common.hpp:26
ReIDModelNameParams::isValidName
static bool isValidName(const std::string &name)
Check whether a model name has the form "model" followed by an integer.
Definition: sources/libs/nvmultiobjecttracker/include/modules/NvMultiObjectTracker/NvTrackerParams.hpp:185
NvTrackerParams::NvTrackerParams
NvTrackerParams()
Initialize the tracker parameter interface.
Definition: sources/libs/nvmultiobjecttracker/include/modules/NvMultiObjectTracker/NvTrackerParams.hpp:39
ReIDModelNameParams
Reid model name format needed by NvTrackerParamsQuery and ReIDConfigParams classes.
Definition: sources/libs/nvmultiobjecttracker/include/modules/NvMultiObjectTracker/NvTrackerParams.hpp:179