/* Copyright (c) 2026 Eoin Jordan * SPDX-License-Identifier: Apache-2.0 * * PoseMicro's output to keypoints: the same as decode() in * train/pose_model.py. Keep the two in step, together with the constants. * * The network answers on a 12 x 12 grid with 51 numbers a cell: the heat of * each of the 17 keypoints, then where in the cell each one is, across and * down. Decoding is one argmax per keypoint and two additions. * * Plain C++ with no Zephyr and no ExecuTorch in it, so that * tests/test_pose_decode.py can run it on a PC against the Python decoder. */ #pragma once #include namespace pose { constexpr int kSide = 96; constexpr int kGrid = 12; constexpr int kCells = kGrid * kGrid; constexpr int kKeypoints = 17; constexpr int kChannels = 3 * kKeypoints; constexpr int kOutputs = kChannels * kCells; /* An offset output of 1.0 is this many cell widths. */ constexpr float kOffsetSpan = 2.0f; /* A keypoint counts as found when its heat reaches this. The export and the * self-test count with it; the camera loop has a threshold of its own * (CONFIG_APP_POSE_THRESHOLD_PERCENT, and '+' and '-' on the console). */ constexpr float kThreshold = 0.3f; /* COCO order; odd numbers are the person's left, even ones the right. */ constexpr const char *kNames[kKeypoints] = { "nose", "left_eye", "right_eye", "left_ear", "right_ear", "left_shoulder", "right_shoulder", "left_elbow", "right_elbow", "left_wrist", "right_wrist", "left_hip", "right_hip", "left_knee", "right_knee", "left_ankle", "right_ankle", }; constexpr int kLimbs = 16; constexpr uint8_t kSkeleton[kLimbs][2] = { {5, 7}, {7, 9}, {6, 8}, {8, 10}, {5, 6}, {5, 11}, {6, 12}, {11, 12}, {11, 13}, {13, 15}, {12, 14}, {14, 16}, {0, 1}, {0, 2}, {1, 3}, {2, 4}, }; struct Keypoint { float x; /* 0..1 from the left of the picture */ float y; /* 0..1 from the top */ float heat; /* about 1 when the network is sure, about 0 when it is not */ }; /* The output tensor as the runtime hands it over. */ struct Output { const void *data; bool is_int8; /* int8 with the scale and zero point below, or float */ bool channels_last; /* stored [cell][channel] (NHWC), or [channel][cell] (NCHW) */ float scale; int zero_point; }; /* One number of the output, as a float whatever it is stored as. */ inline float value(const Output &o, int channel, int cell) { const int at = o.channels_last ? cell * kChannels + channel : channel * kCells + cell; if (o.is_int8) { return static_cast(static_cast(o.data)[at] - o.zero_point) * o.scale; } return static_cast(o.data)[at]; } /* All 17 keypoints, found or not: check heat against a threshold. */ inline void decode(const Output &o, Keypoint *out) { for (int k = 0; k < kKeypoints; ++k) { int best = 0; float top = value(o, k, 0); for (int cell = 1; cell < kCells; ++cell) { const float v = value(o, k, cell); if (v > top) { top = v; best = cell; } } const float dx = value(o, kKeypoints + k, best) * kOffsetSpan; const float dy = value(o, 2 * kKeypoints + k, best) * kOffsetSpan; out[k].x = (static_cast(best % kGrid) + 0.5f + dx) / kGrid; out[k].y = (static_cast(best / kGrid) + 0.5f + dy) / kGrid; out[k].heat = top; } } inline int found(const Keypoint *k, float threshold = kThreshold) { int n = 0; for (int i = 0; i < kKeypoints; ++i) { n += k[i].heat >= threshold ? 1 : 0; } return n; } } /* namespace pose */