src/services/deepLearning/bodyPix/demo_util.js
// @flow
/**
* @license
* Copyright 2019 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import * as posenet from "@tensorflow-models/posenet";
import * as tf from "@tensorflow/tfjs-core";
import { canvas } from "../faceApi/commons";
const COLOR = "aqua";
const LINE_WIDTH = 2;
const boundingBoxColor = "red";
export const TRY_RESNET_BUTTON_NAME = "tryResNetButton";
export const TRY_RESNET_BUTTON_TEXT = "[New] Try ResNet50";
export function drawPoint(
ctx: any,
y: number,
x: number,
r: number,
color: string
) {
ctx.beginPath();
ctx.arc(x, y, r, 0, 2 * Math.PI);
ctx.fillStyle = color;
ctx.fill();
}
/**
* Draws a line on a canvas, i.e. a joint
*/
export function drawSegment(
[ay, ax]: [number, number],
[by, bx]: [number, number],
color: string,
scale: number,
ctx: any
) {
ctx.beginPath();
ctx.moveTo(ax * scale, ay * scale);
ctx.lineTo(bx * scale, by * scale);
ctx.lineWidth = LINE_WIDTH;
ctx.strokeStyle = color;
ctx.stroke();
}
/**
* Draws a pose skeleton by looking up all adjacent keypoints/joints
*/
export function drawSkeleton(
keypoints: any,
minConfidence: number,
ctx: any,
scale: number = 1
) {
const adjacentKeyPoints = posenet.getAdjacentKeyPoints(
keypoints,
minConfidence
);
function toTuple({ y, x }): [number, number] {
return [y, x];
}
// eslint-disable-next-line no-shadow
adjacentKeyPoints.forEach(keypoints => {
drawSegment(
toTuple(keypoints[0].position),
toTuple(keypoints[1].position),
COLOR,
scale,
ctx
);
});
}
/**
* Draw pose keypoints onto a canvas
*/
export function drawKeypoints(
keypoints: any,
minConfidence: number,
ctx: any,
scale: number = 1
) {
for (let i = 0; i < keypoints.length; i += 1) {
const keypoint = keypoints[i];
if (keypoint.score < minConfidence) {
// eslint-disable-next-line no-continue
continue;
}
const { y, x } = keypoint.position;
drawPoint(ctx, y * scale, x * scale, 3, COLOR);
}
}
/**
* Draw the bounding box of a pose. For example, for a whole person standing
* in an image, the bounding box will begin at the nose and extend to one of
* ankles
*/
export function drawBoundingBox(keypoints: any, ctx: any) {
const boundingBox = posenet.getBoundingBox(keypoints);
ctx.rect(
boundingBox.minX,
boundingBox.minY,
boundingBox.maxX - boundingBox.minX,
boundingBox.maxY - boundingBox.minY
);
ctx.strokeStyle = boundingBoxColor;
ctx.stroke();
}
/**
* Converts an array of pixel data into an ImageData object
*/
export async function renderToCanvas(a: any, ctx: any) {
const [height, width] = a.shape;
const imageData = canvas.createImageData(width, height);
const data = await a.data();
for (let i = 0; i < height * width; i += 1) {
const j = i * 4;
const k = i * 3;
imageData.data[j + 0] = data[k + 0];
imageData.data[j + 1] = data[k + 1];
imageData.data[j + 2] = data[k + 2];
imageData.data[j + 3] = 255;
}
ctx.putImageData(imageData, 0, 0);
}
/**
* Draw an image on a canvas
*/
export function renderImageToCanvas(
image: any,
size: [number, number],
c: any
) {
// eslint-disable-next-line prefer-destructuring, no-param-reassign
canvas.width = size[0];
// eslint-disable-next-line prefer-destructuring, no-param-reassign
canvas.height = size[1];
const ctx = c.getContext("2d");
ctx.drawImage(image, 0, 0);
}
/**
* Used by the drawHeatMapValues method to draw heatmap points on to
* the canvas
*/
function drawPoints(ctx, points, radius, color) {
const data = points.buffer().values;
for (let i = 0; i < data.length; i += 2) {
const pointY = data[i];
const pointX = data[i + 1];
if (pointX !== 0 && pointY !== 0) {
ctx.beginPath();
ctx.arc(pointX, pointY, radius, 0, 2 * Math.PI);
ctx.fillStyle = color;
ctx.fill();
}
}
}
/**
* Draw heatmap values, one of the model outputs, on to the canvas
* Read our blog post for a description of PoseNet's heatmap outputs
* https://medium.com/tensorflow/real-time-human-pose-estimation-in-the-browser-with-tensorflow-js-7dd0bc881cd5
*/
export function drawHeatMapValues(
heatMapValues: any,
outputStride: number,
c: any
) {
const ctx = c.getContext("2d");
const radius = 5;
const scaledValues = heatMapValues.mul(tf.scalar(outputStride, "int32"));
drawPoints(ctx, scaledValues, radius, COLOR);
}