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 eitherPointDoesntMeetConfidence(a, b, minConfidence) {
|
2020-10-12 01:22:43 +02:00
|
|
|
return (a < minConfidence || b < minConfidence);
|
|
|
|
}
|
|
|
|
|
2021-02-08 17:39:09 +01:00
|
|
|
export function getAdjacentKeyPoints(keypoints, minConfidence) {
|
2020-10-12 01:22:43 +02:00
|
|
|
return kpt.connectedPartIndices.reduce((result, [leftJoint, rightJoint]) => {
|
|
|
|
if (eitherPointDoesntMeetConfidence(keypoints[leftJoint].score, keypoints[rightJoint].score, minConfidence)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
result.push([keypoints[leftJoint], keypoints[rightJoint]]);
|
|
|
|
return result;
|
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
const { NEGATIVE_INFINITY, POSITIVE_INFINITY } = Number;
|
2021-02-08 17:39:09 +01:00
|
|
|
export function getBoundingBox(keypoints) {
|
2020-10-12 01:22:43 +02:00
|
|
|
return keypoints.reduce(({ maxX, maxY, minX, minY }, { position: { x, y } }) => ({
|
|
|
|
maxX: Math.max(maxX, x),
|
|
|
|
maxY: Math.max(maxY, y),
|
|
|
|
minX: Math.min(minX, x),
|
|
|
|
minY: Math.min(minY, y),
|
|
|
|
}), {
|
|
|
|
maxX: NEGATIVE_INFINITY,
|
|
|
|
maxY: NEGATIVE_INFINITY,
|
|
|
|
minX: POSITIVE_INFINITY,
|
|
|
|
minY: POSITIVE_INFINITY,
|
|
|
|
});
|
|
|
|
}
|
2020-11-08 18:26:45 +01:00
|
|
|
|
2021-02-08 17:39:09 +01:00
|
|
|
export function getBoundingBoxPoints(keypoints) {
|
2020-10-12 01:22:43 +02:00
|
|
|
const { minX, minY, maxX, maxY } = getBoundingBox(keypoints);
|
|
|
|
return [{ x: minX, y: minY }, { x: maxX, y: minY }, { x: maxX, y: maxY }, { x: minX, y: maxY }];
|
|
|
|
}
|
2020-11-08 18:26:45 +01:00
|
|
|
|
2021-02-08 17:39:09 +01:00
|
|
|
export async function toTensorBuffers3D(tensors) {
|
2020-10-12 01:22:43 +02:00
|
|
|
return Promise.all(tensors.map((tensor) => tensor.buffer()));
|
|
|
|
}
|
|
|
|
|
2021-02-08 17:39:09 +01:00
|
|
|
export function scalePose(pose, scaleY, scaleX) {
|
2020-10-12 01:22:43 +02:00
|
|
|
return {
|
|
|
|
score: pose.score,
|
|
|
|
keypoints: pose.keypoints.map(({ score, part, position }) => ({
|
|
|
|
score,
|
|
|
|
part,
|
2021-03-06 16:38:04 +01:00
|
|
|
position: { x: Math.trunc(position.x * scaleX), y: Math.trunc(position.y * scaleY) },
|
2020-10-12 01:22:43 +02:00
|
|
|
})),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-08 17:39:09 +01:00
|
|
|
export function resizeTo(image, [targetH, targetW]) {
|
2020-10-18 15:21:53 +02:00
|
|
|
const input = image.squeeze(0);
|
|
|
|
const resized = input.resizeBilinear([targetH, targetW]);
|
|
|
|
input.dispose();
|
|
|
|
return resized;
|
2020-10-12 01:22:43 +02:00
|
|
|
}
|
|
|
|
|
2021-02-08 17:39:09 +01:00
|
|
|
export function scaleAndFlipPoses(poses, [height, width], [inputResolutionHeight, inputResolutionWidth]) {
|
2020-10-18 15:21:53 +02:00
|
|
|
const scaledPoses = poses.map((pose) => scalePose(pose, height / inputResolutionHeight, width / inputResolutionWidth));
|
2020-10-12 01:22:43 +02:00
|
|
|
return scaledPoses;
|
|
|
|
}
|