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);
59 return static_cast<Detector*
>(algorithm)->run(&img);
71 if (algorithm ==
nullptr || encoder ==
nullptr) {
79 const auto& results =
static_cast<PointDetector*
>(algorithm)->getResults();
86 auto results =
static_cast<Classifier*
>(algorithm)->getResults();
87 for (
auto& result : results) {
90 ret = encoder->
write(results);
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) {
110 ret = encoder->
write(results);
117 auto results =
static_cast<PoseDetector*
>(algorithm)->getResults();
118 for (
auto& result : results) {
119 auto& box = result.box;
125 for (
auto& pt : result.pts) {
130 ret = encoder->
write(results);
140 MA_LOGD(
MA_TAG,
"Failed to serialize algorithm output: %d", ret);
147 static std::shared_ptr<TriggerRule>
create(
const std::string& rule_str) {
148 auto rule = std::make_shared<TriggerRule>();
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) {
156 tokens.push_back(rule_str.substr(i, j - i));
158 if (tokens.size() < 6) {
162 rule->class_id = std::atoi(tokens[0].c_str());
163 if (rule->class_id < 0) {
167 switch (std::atoi(tokens[1].c_str())) {
169 rule->comp = std::greater<float>();
172 rule->comp = std::less<float>();
175 rule->comp = std::greater_equal<float>();
178 rule->comp = std::less_equal<float>();
181 rule->comp = std::equal_to<float>();
184 rule->comp = std::not_equal_to<float>();
190 rule->threshold = std::atoi(tokens[2].c_str()) / 100.0;
191 if (rule->threshold < 0 || rule->threshold > 1) {
195 rule->gpio_pin = std::atoi(tokens[3].c_str());
196 if (!__ma_is_trigger_gpio(rule->gpio_pin)) {
200 rule->default_level = std::atoi(tokens[4].c_str());
201 if (rule->default_level != 0 && rule->default_level != 1) {
204 rule->trigger_level = std::atoi(tokens[5].c_str());
205 if (rule->trigger_level != 0 && rule->trigger_level != 1) {
209 __ma_init_gpio_level(rule->gpio_pin, rule->default_level);
217 if (algorithm ==
nullptr) {
223 switch (algorithm->
getType()) {
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)) {
236 auto results =
static_cast<Classifier*
>(algorithm)->getResults();
237 for (
auto& result : results) {
238 if (result.target == class_id && comp(result.score, threshold)) {
251 auto results =
static_cast<Detector*
>(algorithm)->getResults();
252 for (
auto& result : results) {
253 if (result.target == class_id && comp(result.score, threshold)) {
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)) {
279 __ma_set_gpio_level(gpio_pin, trigger_level);
281 __ma_set_gpio_level(gpio_pin, default_level);
289 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: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
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
void operator()(Model *algorithm)
Definition: refactor_required.hpp:216
Definition: ma_types.h:124