Skip to content

Commit

Permalink
refactor: result json for keypoints
Browse files Browse the repository at this point in the history
* refactor: result json for keypoints

* feat: send resolution when send image disabled
  • Loading branch information
iChizer0 authored Jan 11, 2024
1 parent c00a00b commit 0945801
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 89 deletions.
8 changes: 3 additions & 5 deletions core/algorithm/el_algorithm_yolo_pose.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ el_err_code_t AlgorithmYOLOPOSE::postprocess() {
std::vector<types::pt3_t<float>> 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;
Expand Down Expand Up @@ -399,9 +398,10 @@ el_err_code_t AlgorithmYOLOPOSE::postprocess() {
.w = static_cast<decltype(KeyPointType::box.w)>(std::round(w)),
.h = static_cast<decltype(KeyPointType::box.h)>(std::round(h)),
.score = static_cast<decltype(KeyPointType::box.score)>(std::round(s)),
.target = static_cast<decltype(KeyPointType::box.target)>(target),
.target = static_cast<decltype(KeyPointType::box.target)>(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;
Expand All @@ -410,14 +410,12 @@ el_err_code_t AlgorithmYOLOPOSE::postprocess() {
.x = static_cast<decltype(el_point_t::x)>(std::round(x)),
.y = static_cast<decltype(el_point_t::y)>(std::round(y)),
.score = static_cast<decltype(el_point_t::score)>(std::round(z)),
.target = static_cast<decltype(el_point_t::target)>(target),
.target = static_cast<decltype(el_point_t::target)>(target++),
});
}
keypoint.score = keypoint.box.score;
keypoint.target = keypoint.box.target;
_results.emplace_front(std::move(keypoint));

++target;
}

return EL_OK;
Expand Down
2 changes: 1 addition & 1 deletion sscma/callback/invoke.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ class Invoke final : public std::enable_shared_from_this<Invoke> {

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)));
Expand Down
189 changes: 106 additions & 83 deletions sscma/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cctype>
#include <cstdint>
#include <cstring>
#include <forward_list>
#include <memory>
#include <string>
Expand Down Expand Up @@ -122,99 +123,121 @@ decltype(auto) sensor_info_2_json_str(const el_sensor_info_t& sensor_info) {
"}");
}

template <typename T> constexpr decltype(auto) results_2_json_str(const std::forward_list<T>& results) {
decltype(auto) results_2_json_str(const std::forward_list<el_box_t>& results) {
std::string ss;
const char* delim = "";

if constexpr (std::is_same<T, el_box_t>::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<T, el_point_t>::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<T, el_class_t>::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<T, el_keypoint_t>::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<el_point_t>& 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<el_class_t>& 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<el_keypoint_t>& 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\": \"\"");
Expand Down

0 comments on commit 0945801

Please sign in to comment.