SSCMA-Micro CPP SDK  v2.0.0
SSCMA-Micro is a cross-platform machine learning inference framework designed for embedded devices.
refactor_required.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <memory>
5 #include <string>
6 #include <vector>
7 #include <forward_list>
8 
9 #include "core/ma_core.h"
10 #include "core/ma_debug.h"
11 #include "core/utils/ma_base64.h"
12 #include "porting/ma_porting.h"
13 #include "resource.hpp"
15 
16 namespace ma {
17 
18 #if MA_TRIGGER_USE_REAL_GPIO
19 extern bool __ma_is_trigger_gpio(int pin);
20 extern void __ma_init_gpio_level(int pin, int level);
21 extern void __ma_set_gpio_level(int pin, int level);
22 #else
23 static bool __ma_is_trigger_gpio(int pin) {
24  (void)pin;
25  MA_LOGD(MA_TAG, "GPIO pin: %d", pin);
26  return true;
27 }
28 static void __ma_init_gpio_level(int pin, int level) {
29  (void)pin;
30  (void)level;
31  MA_LOGD(MA_TAG, "GPIO pin: %d, level: %d", pin, level);
32 }
33 static void __ma_set_gpio_level(int pin, int level) {
34  (void)pin;
35  (void)level;
36  MA_LOGD(MA_TAG, "GPIO pin: %d, level: %d", pin, level);
37 }
38 #endif
39 
40 using namespace ma::model;
41 
43  if (algorithm == nullptr) {
44  return MA_EINVAL;
45  }
46 
47  switch (algorithm->getType()) {
48  case MA_MODEL_TYPE_PFLD:
49  return static_cast<PointDetector*>(algorithm)->run(&img);
50 
52  return static_cast<Classifier*>(algorithm)->run(&img);
53 
54  case MA_MODEL_TYPE_FOMO:
59  return static_cast<Detector*>(algorithm)->run(&img);
60 
62  return static_cast<PoseDetector*>(algorithm)->run(&img);
63 
64  default:
65  return MA_ENOTSUP;
66  }
67 }
68 
69 ma_err_t serializeAlgorithmOutput(Model* algorithm, Encoder* encoder, int width, int height) {
70 
71  if (algorithm == nullptr || encoder == nullptr) {
72  return MA_EINVAL;
73  }
74 
75  auto ret = MA_OK;
76 
77  switch (algorithm->getType()) {
78  case MA_MODEL_TYPE_PFLD: {
79  const auto& results = static_cast<PointDetector*>(algorithm)->getResults();
80  // encoder->write(results);
81  break;
82  }
83 
84  case MA_MODEL_TYPE_IMCLS: {
85 
86  auto results = static_cast<Classifier*>(algorithm)->getResults();
87  for (auto& result : results) {
88  result.score *= 100;
89  }
90  ret = encoder->write(results);
91 
92  break;
93  }
94 
95  case MA_MODEL_TYPE_FOMO:
100 
101  auto results = static_cast<Detector*>(algorithm)->getResults();
102  MA_LOGD(MA_TAG, "Results size: %d", std::distance(results.begin(), results.end()));
103  for (auto& result : results) {
104  result.x *= width;
105  result.y *= height;
106  result.w *= width;
107  result.h *= height;
108  result.score *= 100;
109  }
110  ret = encoder->write(results);
111 
112  break;
113  }
114 
116 
117  auto results = static_cast<PoseDetector*>(algorithm)->getResults();
118  for (auto& result : results) {
119  auto& box = result.box;
120  box.x *= width;
121  box.y *= height;
122  box.w *= width;
123  box.h *= height;
124  box.score *= 100;
125  for (auto& pt : result.pts) {
126  pt.x *= width;
127  pt.y *= height;
128  }
129  }
130  ret = encoder->write(results);
131 
132  break;
133  }
134 
135  default:
136  ret = MA_ENOTSUP;
137  }
138 
139  if (ret != MA_OK) {
140  MA_LOGD(MA_TAG, "Failed to serialize algorithm output: %d", ret);
141  }
142 
143  return ret;
144 }
145 
146 struct TriggerRule {
147  static std::shared_ptr<TriggerRule> create(const std::string& rule_str) {
148  auto rule = std::make_shared<TriggerRule>();
149 
150  std::vector<std::string> tokens;
151  for (size_t i = 0, j = 0; i < rule_str.size(); i = j + 1) {
152  j = rule_str.find(',', i);
153  if (j == std::string::npos) {
154  j = rule_str.size();
155  }
156  tokens.push_back(rule_str.substr(i, j - i));
157  }
158  if (tokens.size() < 6) {
159  return nullptr;
160  }
161 
162  rule->class_id = std::atoi(tokens[0].c_str());
163  if (rule->class_id < 0) {
164  return nullptr;
165  }
166 
167  switch (std::atoi(tokens[1].c_str())) {
168  case 0:
169  rule->comp = std::greater<float>();
170  break;
171  case 1:
172  rule->comp = std::less<float>();
173  break;
174  case 2:
175  rule->comp = std::greater_equal<float>();
176  break;
177  case 3:
178  rule->comp = std::less_equal<float>();
179  break;
180  case 4:
181  rule->comp = std::equal_to<float>();
182  break;
183  case 5:
184  rule->comp = std::not_equal_to<float>();
185  break;
186  default:
187  return nullptr;
188  }
189 
190  rule->threshold = std::atoi(tokens[2].c_str()) / 100.0;
191  if (rule->threshold < 0 || rule->threshold > 1) {
192  return nullptr;
193  }
194 
195  rule->gpio_pin = std::atoi(tokens[3].c_str());
196  if (!__ma_is_trigger_gpio(rule->gpio_pin)) {
197  return nullptr;
198  }
199 
200  rule->default_level = std::atoi(tokens[4].c_str());
201  if (rule->default_level != 0 && rule->default_level != 1) {
202  return nullptr;
203  }
204  rule->trigger_level = std::atoi(tokens[5].c_str());
205  if (rule->trigger_level != 0 && rule->trigger_level != 1) {
206  return nullptr;
207  }
208 
209  __ma_init_gpio_level(rule->gpio_pin, rule->default_level);
210 
211  return rule;
212  }
213 
214  ~TriggerRule() = default;
215 
216  void operator()(Model* algorithm) {
217  if (algorithm == nullptr) {
218  return;
219  }
220 
221  bool fit = false;
222 
223  switch (algorithm->getType()) {
224  case MA_MODEL_TYPE_PFLD: {
225  const auto& results = static_cast<PointDetector*>(algorithm)->getResults();
226  for (const auto& result : results) {
227  if (result.target == class_id && comp(result.score, threshold)) {
228  fit = true;
229  break;
230  }
231  }
232  break;
233  }
234 
235  case MA_MODEL_TYPE_IMCLS: {
236  auto results = static_cast<Classifier*>(algorithm)->getResults();
237  for (auto& result : results) {
238  if (result.target == class_id && comp(result.score, threshold)) {
239  fit = true;
240  break;
241  }
242  }
243  break;
244  }
245 
246  case MA_MODEL_TYPE_FOMO:
251  auto results = static_cast<Detector*>(algorithm)->getResults();
252  for (auto& result : results) {
253  if (result.target == class_id && comp(result.score, threshold)) {
254  fit = true;
255  break;
256  }
257  }
258 
259  break;
260  }
261 
263  auto results = static_cast<PoseDetector*>(algorithm)->getResults();
264  for (auto& result : results) {
265  if (result.box.target == class_id && comp(result.box.score, threshold)) {
266  fit = true;
267  break;
268  }
269  }
270 
271  break;
272  }
273 
274  default:
275  break;
276  }
277 
278  if (fit) {
279  __ma_set_gpio_level(gpio_pin, trigger_level);
280  } else {
281  __ma_set_gpio_level(gpio_pin, default_level);
282  }
283  }
284 
285  TriggerRule() = default;
286 
287 private:
288  int class_id;
289  std::function<bool(float, float)> comp;
290  float threshold;
291  int gpio_pin;
292  int default_level;
293  int trigger_level;
294 };
295 
296 std::forward_list<std::shared_ptr<TriggerRule>> trigger_rules;
298 
299 } // namespace ma
Definition: ma_codec_base.h:14
virtual ma_err_t write(const std::string &key, int8_t value)=0
Encoder type for write int8_t value.
Definition: ma_model_base.h:14
ma_model_type_t getType() const
Definition: ma_model_base.cpp:73
Definition: ma_osal.h:80
Definition: ma_model_classifier.h:12
Definition: ma_model_detector.h:12
Definition: ma_model_point_detector.h:10
Definition: ma_model_pose_detector.h:10
#define MA_LOGD(TAG,...)
Definition: ma_debug.h:79
#define MA_TAG
Definition: ma_debug.h:9
@ MA_MODEL_TYPE_YOLOV5
Definition: ma_types.h:278
@ MA_MODEL_TYPE_PFLD
Definition: ma_types.h:277
@ MA_MODEL_TYPE_YOLOV8_POSE
Definition: ma_types.h:280
@ MA_MODEL_TYPE_IMCLS
Definition: ma_types.h:279
@ MA_MODEL_TYPE_FOMO
Definition: ma_types.h:276
@ MA_MODEL_TYPE_NVIDIA_DET
Definition: ma_types.h:282
@ MA_MODEL_TYPE_YOLO_WORLD
Definition: ma_types.h:283
@ MA_MODEL_TYPE_YOLOV8
Definition: ma_types.h:281
ma_err_t
Definition: ma_types.h:21
@ MA_ENOTSUP
Definition: ma_types.h:31
@ MA_OK
Definition: ma_types.h:23
@ MA_EINVAL
Definition: ma_types.h:28
Definition: ma_model_classifier.cpp:5
Definition: ma_cv.cpp:7
ma_err_t serializeAlgorithmOutput(Model *algorithm, Encoder *encoder, int width, int height)
Definition: refactor_required.hpp:69
ma_err_t setAlgorithmInput(Model *algorithm, ma_img_t &img)
Definition: refactor_required.hpp:42
std::forward_list< std::shared_ptr< TriggerRule > > trigger_rules
Definition: refactor_required.hpp:296
ma::Mutex trigger_rules_mutex
Definition: refactor_required.hpp:297
Definition: refactor_required.hpp:146
static std::shared_ptr< TriggerRule > create(const std::string &rule_str)
Definition: refactor_required.hpp:147
TriggerRule()=default
void operator()(Model *algorithm)
Definition: refactor_required.hpp:216
~TriggerRule()=default
Definition: ma_types.h:124