human/src/body/util.js

70 lines
2.2 KiB
JavaScript
Raw Normal View History

2020-10-12 01:22:43 +02:00
const kpt = require('./keypoints');
function eitherPointDoesntMeetConfidence(a, b, minConfidence) {
return (a < minConfidence || b < minConfidence);
}
function getAdjacentKeyPoints(keypoints, minConfidence) {
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;
}, []);
}
exports.getAdjacentKeyPoints = getAdjacentKeyPoints;
const { NEGATIVE_INFINITY, POSITIVE_INFINITY } = Number;
function getBoundingBox(keypoints) {
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,
});
}
exports.getBoundingBox = getBoundingBox;
2020-11-08 18:26:45 +01:00
2020-10-12 01:22:43 +02:00
function getBoundingBoxPoints(keypoints) {
const { minX, minY, maxX, maxY } = getBoundingBox(keypoints);
return [{ x: minX, y: minY }, { x: maxX, y: minY }, { x: maxX, y: maxY }, { x: minX, y: maxY }];
}
exports.getBoundingBoxPoints = getBoundingBoxPoints;
2020-11-08 18:26:45 +01:00
2020-10-12 01:22:43 +02:00
async function toTensorBuffers3D(tensors) {
return Promise.all(tensors.map((tensor) => tensor.buffer()));
}
exports.toTensorBuffers3D = toTensorBuffers3D;
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,
position: { x: position.x * scaleX, y: position.y * scaleY },
2020-10-12 01:22:43 +02:00
})),
};
}
exports.scalePose = scalePose;
function resizeTo(image, [targetH, targetW]) {
const input = image.squeeze(0);
const resized = input.resizeBilinear([targetH, targetW]);
input.dispose();
return resized;
2020-10-12 01:22:43 +02:00
}
exports.resizeTo = resizeTo;
2020-10-12 01:22:43 +02:00
function scaleAndFlipPoses(poses, [height, width], [inputResolutionHeight, inputResolutionWidth]) {
const scaledPoses = poses.map((pose) => scalePose(pose, height / inputResolutionHeight, width / inputResolutionWidth));
2020-10-12 01:22:43 +02:00
return scaledPoses;
}
exports.scaleAndFlipPoses = scaleAndFlipPoses;