7 #include <forward_list>
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);
23 static bool __ma_is_trigger_gpio(
int pin) {
28 static void __ma_init_gpio_level(
int pin,
int level) {
33 static void __ma_set_gpio_level(
int pin,
int level) {
43 if (algorithm ==
nullptr) {
52 return static_cast<Classifier*
>(algorithm)->run(&img);
60 return static_cast<Detector*
>(algorithm)->run(&img);
73 if (algorithm ==
nullptr || encoder ==
nullptr) {
81 auto results =
static_cast<PointDetector*
>(algorithm)->getResults();
82 for (
auto& result : results) {
83 result.x =
static_cast<int>(std::round(result.x * width));
84 result.y =
static_cast<int>(std::round(result.y * height));
85 result.score =
static_cast<int>(std::round(result.score * 100));
87 ret = encoder->
write(results);
94 auto results =
static_cast<Classifier*
>(algorithm)->getResults();
95 for (
auto& result : results) {
96 result.score =
static_cast<int>(std::round(result.score * 100));
98 ret = encoder->
write(results);
111 auto results =
static_cast<Detector*
>(algorithm)->getResults();
112 MA_LOGD(
MA_TAG,
"Results size: %d", std::distance(results.begin(), results.end()));
113 for (
auto& result : results) {
114 result.x =
static_cast<int>(std::round(result.x * width));
115 result.y =
static_cast<int>(std::round(result.y * height));
116 result.w =
static_cast<int>(std::round(result.w * width));
117 result.h =
static_cast<int>(std::round(result.h * height));
118 result.score =
static_cast<int>(std::round(result.score * 100));
120 ret = encoder->
write(results);
128 auto results =
static_cast<PoseDetector*
>(algorithm)->getResults();
129 for (
auto& result : results) {
130 auto& box = result.box;
131 box.x =
static_cast<int>(std::round(box.x * width));
132 box.y =
static_cast<int>(std::round(box.y * height));
133 box.w =
static_cast<int>(std::round(box.w * width));
134 box.h =
static_cast<int>(std::round(box.h * height));
135 box.score =
static_cast<int>(std::round(box.score * 100));
136 for (
auto& pt : result.pts) {
137 pt.x =
static_cast<int>(std::round(pt.x * width));
138 pt.y =
static_cast<int>(std::round(pt.y * height));
141 ret = encoder->
write(results);
151 MA_LOGD(
MA_TAG,
"Failed to serialize algorithm output: %d", ret);
158 static std::shared_ptr<TriggerRule>
create(
const std::string& rule_str) {
159 auto rule = std::make_shared<TriggerRule>();
161 std::vector<std::string> tokens;
162 for (
size_t i = 0, j = 0; i < rule_str.size(); i = j + 1) {
163 j = rule_str.find(
',', i);
164 if (j == std::string::npos) {
167 tokens.push_back(rule_str.substr(i, j - i));
169 if (tokens.size() < 6) {
173 rule->class_id = std::atoi(tokens[0].c_str());
174 if (rule->class_id < 0) {
178 switch (std::atoi(tokens[1].c_str())) {
180 rule->comp = std::greater<float>();
183 rule->comp = std::less<float>();
186 rule->comp = std::greater_equal<float>();
189 rule->comp = std::less_equal<float>();
192 rule->comp = std::equal_to<float>();
195 rule->comp = std::not_equal_to<float>();
201 rule->threshold = std::atoi(tokens[2].c_str()) / 100.0;
202 if (rule->threshold < 0 || rule->threshold > 1) {
206 rule->gpio_pin = std::atoi(tokens[3].c_str());
207 if (!__ma_is_trigger_gpio(rule->gpio_pin)) {
211 rule->default_level = std::atoi(tokens[4].c_str());
212 if (rule->default_level != 0 && rule->default_level != 1) {
215 rule->trigger_level = std::atoi(tokens[5].c_str());
216 if (rule->trigger_level != 0 && rule->trigger_level != 1) {
220 __ma_init_gpio_level(rule->gpio_pin, rule->default_level);
228 if (algorithm ==
nullptr) {
234 switch (algorithm->
getType()) {
236 const auto& results =
static_cast<PointDetector*
>(algorithm)->getResults();
237 for (
const auto& result : results) {
238 if (result.target == class_id && comp(result.score, threshold)) {
247 auto results =
static_cast<Classifier*
>(algorithm)->getResults();
248 for (
auto& result : results) {
249 if (result.target == class_id && comp(result.score, threshold)) {
264 auto results =
static_cast<Detector*
>(algorithm)->getResults();
265 for (
auto& result : results) {
266 if (result.target == class_id && comp(result.score, threshold)) {
277 auto results =
static_cast<PoseDetector*
>(algorithm)->getResults();
278 for (
auto& result : results) {
279 if (result.box.target == class_id && comp(result.box.score, threshold)) {
293 __ma_set_gpio_level(gpio_pin, trigger_level);
295 __ma_set_gpio_level(gpio_pin, default_level);
303 std::function<bool(
float,
float)> comp;
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_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:279
@ MA_MODEL_TYPE_PFLD
Definition: ma_types.h:278
@ MA_MODEL_TYPE_YOLOV8_POSE
Definition: ma_types.h:281
@ MA_MODEL_TYPE_IMCLS
Definition: ma_types.h:280
@ MA_MODEL_TYPE_FOMO
Definition: ma_types.h:277
@ MA_MODEL_TYPE_NVIDIA_DET
Definition: ma_types.h:283
@ MA_MODEL_TYPE_YOLO11_POSE
Definition: ma_types.h:286
@ MA_MODEL_TYPE_YOLO_WORLD
Definition: ma_types.h:284
@ MA_MODEL_TYPE_YOLO11
Definition: ma_types.h:285
@ MA_MODEL_TYPE_RTMDET
Definition: ma_types.h:289
@ MA_MODEL_TYPE_YOLOV8
Definition: ma_types.h:282
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
ma_err_t serializeAlgorithmOutput(Model *algorithm, Encoder *encoder, int width, int height)
Definition: refactor_required.hpp:71
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:310
ma::Mutex trigger_rules_mutex
Definition: refactor_required.hpp:311
Definition: refactor_required.hpp:157
static std::shared_ptr< TriggerRule > create(const std::string &rule_str)
Definition: refactor_required.hpp:158
void operator()(Model *algorithm)
Definition: refactor_required.hpp:227
Definition: ma_types.h:124