human/src/posenet/vectors.ts

46 lines
1.0 KiB
TypeScript
Raw Normal View History

2020-11-10 02:13:38 +01:00
import * as kpt from './keypoints';
2020-10-12 01:22:43 +02:00
2021-02-08 17:39:09 +01:00
export function getOffsetPoint(y, x, keypoint, offsets) {
2020-10-12 01:22:43 +02:00
return {
y: offsets.get(y, x, keypoint),
x: offsets.get(y, x, keypoint + kpt.NUM_KEYPOINTS),
};
}
2021-02-08 17:39:09 +01:00
export function getImageCoords(part, outputStride, offsets) {
2020-10-12 01:22:43 +02:00
const { heatmapY, heatmapX, id: keypoint } = part;
const { y, x } = getOffsetPoint(heatmapY, heatmapX, keypoint, offsets);
return {
x: part.heatmapX * outputStride + x,
y: part.heatmapY * outputStride + y,
};
}
2021-02-08 17:39:09 +01:00
export function fillArray(element, size) {
2020-10-12 01:22:43 +02:00
const result = new Array(size);
for (let i = 0; i < size; i++) {
result[i] = element;
}
return result;
}
2021-02-08 17:39:09 +01:00
export function clamp(a, min, max) {
2020-10-12 01:22:43 +02:00
if (a < min) return min;
if (a > max) return max;
return a;
}
2021-02-08 17:39:09 +01:00
export function squaredDistance(y1, x1, y2, x2) {
2020-10-12 01:22:43 +02:00
const dy = y2 - y1;
const dx = x2 - x1;
return dy * dy + dx * dx;
}
2021-02-08 17:39:09 +01:00
export function addVectors(a, b) {
2020-10-12 01:22:43 +02:00
return { x: a.x + b.x, y: a.y + b.y };
}
2021-02-08 17:39:09 +01:00
export function clampVector(a, min, max) {
2020-10-12 01:22:43 +02:00
return { y: clamp(a.y, min, max), x: clamp(a.x, min, max) };
}