File size: 3,525 Bytes
ce7fc72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* 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 <stdint.h>

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<float>(static_cast<const int8_t *>(o.data)[at] - o.zero_point) *
		       o.scale;
	}
	return static_cast<const float *>(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<float>(best % kGrid) + 0.5f + dx) / kGrid;
		out[k].y = (static_cast<float>(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 */