From 09458015fd35314729056f261d4e56fe93aed01a Mon Sep 17 00:00:00 2001 From: iChizer0 <62390647+iChizer0@users.noreply.github.com> Date: Thu, 11 Jan 2024 19:33:26 +0800 Subject: [PATCH] refactor: result json for keypoints * refactor: result json for keypoints * feat: send resolution when send image disabled --- core/algorithm/el_algorithm_yolo_pose.cpp | 8 +- sscma/callback/invoke.hpp | 2 +- sscma/utility.hpp | 189 ++++++++++++---------- 3 files changed, 110 insertions(+), 89 deletions(-) diff --git a/core/algorithm/el_algorithm_yolo_pose.cpp b/core/algorithm/el_algorithm_yolo_pose.cpp index 2e861071..2e339f84 100644 --- a/core/algorithm/el_algorithm_yolo_pose.cpp +++ b/core/algorithm/el_algorithm_yolo_pose.cpp @@ -354,7 +354,6 @@ el_err_code_t AlgorithmYOLOPOSE::postprocess() { std::vector> n_keypoint(keypoint_nums); // extract keypoints from outputs and store all results - size_t target = 0; for (const auto& anchor_bbox : anchor_bboxes) { const auto pre = (_anchor_strides[anchor_bbox.anchor_class].start + anchor_bbox.anchor_index) * output_keypoints_dims_2; @@ -399,9 +398,10 @@ el_err_code_t AlgorithmYOLOPOSE::postprocess() { .w = static_cast(std::round(w)), .h = static_cast(std::round(h)), .score = static_cast(std::round(s)), - .target = static_cast(target), + .target = static_cast(0), }; keypoint.pts.reserve(keypoint_nums); + size_t target = 0; for (const auto& kp : n_keypoint) { float x = kp.x * scale_w; float y = kp.y * scale_h; @@ -410,14 +410,12 @@ el_err_code_t AlgorithmYOLOPOSE::postprocess() { .x = static_cast(std::round(x)), .y = static_cast(std::round(y)), .score = static_cast(std::round(z)), - .target = static_cast(target), + .target = static_cast(target++), }); } keypoint.score = keypoint.box.score; keypoint.target = keypoint.box.target; _results.emplace_front(std::move(keypoint)); - - ++target; } return EL_OK; diff --git a/sscma/callback/invoke.hpp b/sscma/callback/invoke.hpp index fdf9a699..8d3123ae 100644 --- a/sscma/callback/invoke.hpp +++ b/sscma/callback/invoke.hpp @@ -377,7 +377,7 @@ class Invoke final : public std::enable_shared_from_this { if (!_differed || results_filter.compare_and_update(algorithm->get_results())) { if (_results_only) - event_reply(concat_strings(", ", algorithm_results_2_json_str(algorithm))); + event_reply(concat_strings(", ", algorithm_results_2_json_str(algorithm), ", ", img_res_2_json_str(&frame))); else { event_reply( concat_strings(", ", algorithm_results_2_json_str(algorithm), ", ", std::move(encoded_frame_str))); diff --git a/sscma/utility.hpp b/sscma/utility.hpp index ae80a22c..09aba88c 100644 --- a/sscma/utility.hpp +++ b/sscma/utility.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -122,99 +123,121 @@ decltype(auto) sensor_info_2_json_str(const el_sensor_info_t& sensor_info) { "}"); } -template constexpr decltype(auto) results_2_json_str(const std::forward_list& results) { +decltype(auto) results_2_json_str(const std::forward_list& results) { std::string ss; const char* delim = ""; - if constexpr (std::is_same::value) { - ss = "\"boxes\": ["; - for (const auto& box : results) { - ss += concat_strings(delim, - "[", - std::to_string(box.x), - ", ", - std::to_string(box.y), - ", ", - std::to_string(box.w), - ", ", - std::to_string(box.h), - ", ", - std::to_string(box.score), - ", ", - std::to_string(box.target), - "]"); - delim = ", "; - } - ss += "]"; - } else if constexpr (std::is_same::value) { - ss = "\"points\": ["; - for (const auto& point : results) { - ss += concat_strings(delim, - "[", - std::to_string(point.x), - ", ", - std::to_string(point.y), - ", ", - std::to_string(point.score), - ", ", - std::to_string(point.target), - "]"); - delim = ", "; - } - ss += "]"; - } else if constexpr (std::is_same::value) { - ss = "\"classes\": ["; - for (const auto& cls : results) { - ss += concat_strings(delim, "[", std::to_string(cls.score), ", ", std::to_string(cls.target), "]"); - delim = ", "; - } - ss += "]"; - } else if constexpr (std::is_same::value) { - std::string boxes_str = "\"boxes\": ["; - std::string pts_str = "\"points\": ["; - - for (const auto& kps : results) { - boxes_str += concat_strings(delim, - "[", - std::to_string(kps.box.x), - ", ", - std::to_string(kps.box.y), - ", ", - std::to_string(kps.box.w), - ", ", - std::to_string(kps.box.h), - ", ", - std::to_string(kps.box.score), - ", ", - std::to_string(kps.box.target), - "]"); - pts_str += delim; - delim = ""; - for (const auto& pt : kps.pts) { - pts_str += concat_strings(delim, - "[", - std::to_string(pt.x), - ", ", - std::to_string(pt.y), - ", ", - std::to_string(pt.score), - ", ", - std::to_string(pt.target), - "]"); - delim = ", "; - } - delim = ", "; - } + ss = "\"boxes\": ["; + for (const auto& box : results) { + ss += concat_strings(delim, + "[", + std::to_string(box.x), + ", ", + std::to_string(box.y), + ", ", + std::to_string(box.w), + ", ", + std::to_string(box.h), + ", ", + std::to_string(box.score), + ", ", + std::to_string(box.target), + "]"); + delim = ", "; + } + ss += "]"; - boxes_str += "]"; - pts_str += "]"; + return ss; +} + +decltype(auto) results_2_json_str(const std::forward_list& results) { + std::string ss; + const char* delim = ""; - ss = concat_strings(std::move(boxes_str), ", ", std::move(pts_str)); + ss = "\"points\": ["; + for (const auto& point : results) { + ss += concat_strings(delim, + "[", + std::to_string(point.x), + ", ", + std::to_string(point.y), + ", ", + std::to_string(point.score), + ", ", + std::to_string(point.target), + "]"); + delim = ", "; } + ss += "]"; return ss; } +decltype(auto) results_2_json_str(const std::forward_list& results) { + std::string ss; + const char* delim = ""; + + ss = "\"classes\": ["; + for (const auto& cls : results) { + ss += concat_strings(delim, "[", std::to_string(cls.score), ", ", std::to_string(cls.target), "]"); + delim = ", "; + } + ss += "]"; + + return ss; +} + +decltype(auto) results_2_json_str(const std::forward_list& results) { + std::string ss; + const char* delim = ""; + + ss = "\"keypoints\": ["; + for (const auto& kp : results) { + std::string pts_str{"["}; + const char* pts_delim = ""; + for (const auto& pt : kp.pts) { + pts_str += concat_strings(pts_delim, + "[", + std::to_string(pt.x), + ", ", + std::to_string(pt.y), + ", ", + std::to_string(pt.score), + ", ", + std::to_string(pt.target), + "]"); + pts_delim = ", "; + } + pts_str += "]"; + ss += concat_strings(delim, + "[", + "[", + std::to_string(kp.box.x), + ", ", + std::to_string(kp.box.y), + ", ", + std::to_string(kp.box.w), + ", ", + std::to_string(kp.box.h), + ", ", + std::to_string(kp.box.score), + ", ", + std::to_string(kp.box.target), + "]", + ", ", + std::move(pts_str), + "]"); + delim = ", "; + } + ss += "]"; + + return ss; +} + +inline decltype(auto) img_res_2_json_str(const el_img_t* img) { + return concat_strings("\"resolution\": [", std::to_string(img->width), ", ", std::to_string(img->height), "]"); +} + inline decltype(auto) img_2_json_str(const el_img_t* img) { if (!img || !img->data || !img->size) [[unlikely]] return std::string("\"image\": \"\"");