human/src/util/draw.ts

523 lines
22 KiB
TypeScript
Raw Normal View History

2021-05-25 14:58:20 +02:00
/**
* Module that implements helper draw functions, exposed as human.draw
*/
2021-09-28 18:01:48 +02:00
import { TRI468 as triangulation } from '../face/facemeshcoords';
2021-11-14 17:22:52 +01:00
import { mergeDeep, now, log } from './util';
2021-10-23 15:38:52 +02:00
import { env } from './env';
import type { Result, FaceResult, BodyResult, HandResult, ObjectResult, GestureResult, PersonResult, Point } from '../result';
2021-10-25 19:09:00 +02:00
import type { AnyCanvas } from '../exports';
2021-03-05 17:43:50 +01:00
2021-10-25 19:09:00 +02:00
/** Draw Options
* - Accessed via `human.draw.options` or provided per each draw method as the drawOptions optional parameter
2021-04-13 17:05:52 +02:00
*/
2021-10-25 19:09:00 +02:00
export type DrawOptions = {
/** draw line color */
2021-04-13 17:05:52 +02:00
color: string,
2021-10-25 19:09:00 +02:00
/** label color */
2021-04-13 17:05:52 +02:00
labelColor: string,
2021-10-25 19:09:00 +02:00
/** label shadow color */
2021-04-13 17:05:52 +02:00
shadowColor: string,
2021-10-25 19:09:00 +02:00
/** label font */
2021-04-13 17:05:52 +02:00
font: string,
2021-10-25 19:09:00 +02:00
/** line spacing between labels */
2021-04-13 17:05:52 +02:00
lineHeight: number,
2021-10-25 19:09:00 +02:00
/** line width for drawn lines */
2021-04-13 17:05:52 +02:00
lineWidth: number,
2021-10-25 19:09:00 +02:00
/** size of drawn points */
2021-04-13 17:05:52 +02:00
pointSize: number,
2021-10-25 19:09:00 +02:00
/** draw rounded boxes by n pixels */
2021-04-13 17:05:52 +02:00
roundRect: number,
2021-10-25 19:09:00 +02:00
/** should points be drawn? */
drawPoints: boolean,
2021-10-25 19:09:00 +02:00
/** should labels be drawn? */
drawLabels: boolean,
2021-10-25 19:09:00 +02:00
/** should detected gestures be drawn? */
2021-10-21 16:54:51 +02:00
drawGestures: boolean,
2021-10-25 19:09:00 +02:00
/** should draw boxes around detection results? */
drawBoxes: boolean,
2021-10-25 19:09:00 +02:00
/** should draw polygons from detection points? */
drawPolygons: boolean,
2021-10-25 19:09:00 +02:00
/** should draw gaze arrows? */
drawGaze: boolean,
2021-10-25 19:09:00 +02:00
/** should fill polygons? */
fillPolygons: boolean,
2021-10-25 19:09:00 +02:00
/** use z-coordinate when available */
useDepth: boolean,
2021-10-25 19:09:00 +02:00
/** should lines be curved? */
useCurves: boolean,
2021-04-13 17:05:52 +02:00
}
export const options: DrawOptions = {
color: <string>'rgba(173, 216, 230, 0.6)', // 'lightblue' with light alpha channel
2021-03-14 04:31:09 +01:00
labelColor: <string>'rgba(173, 216, 230, 1)', // 'lightblue' with dark alpha channel
shadowColor: <string>'black',
2021-11-05 16:28:06 +01:00
font: <string>'small-caps 16px "Segoe UI"',
2021-06-14 16:23:06 +02:00
lineHeight: <number>18,
lineWidth: <number>4,
2021-03-14 04:31:09 +01:00
pointSize: <number>2,
2021-06-14 16:23:06 +02:00
roundRect: <number>8,
drawPoints: <boolean>false,
drawLabels: <boolean>true,
drawBoxes: <boolean>true,
2021-10-21 16:54:51 +02:00
drawGestures: <boolean>true,
drawPolygons: <boolean>true,
drawGaze: <boolean>true,
fillPolygons: <boolean>false,
useDepth: <boolean>true,
useCurves: <boolean>false,
2021-03-05 17:43:50 +01:00
};
2021-10-27 15:45:38 +02:00
let drawTime = 0;
2021-09-01 00:22:16 +02:00
const getCanvasContext = (input) => {
2021-11-14 17:22:52 +01:00
if (!input) log('draw error: invalid canvas');
else if (!input.getContext) log('draw error: canvas context not defined');
else {
const ctx = input.getContext('2d');
if (!ctx) log('draw error: cannot get canvas context');
else return ctx;
}
return null;
2021-08-31 20:48:55 +02:00
};
2021-05-30 23:56:40 +02:00
const rad2deg = (theta) => Math.round((theta * 180) / Math.PI);
2021-11-10 18:21:45 +01:00
function point(ctx: CanvasRenderingContext2D, x, y, z, localOptions) {
z = z || 0;
2021-04-13 17:05:52 +02:00
ctx.fillStyle = localOptions.useDepth && z ? `rgba(${127.5 + (2 * z)}, ${127.5 - (2 * z)}, 255, 0.3)` : localOptions.color;
2021-03-05 17:43:50 +01:00
ctx.beginPath();
2021-04-13 17:05:52 +02:00
ctx.arc(x, y, localOptions.pointSize, 0, 2 * Math.PI);
2021-03-05 17:43:50 +01:00
ctx.fill();
}
function rect(ctx: CanvasRenderingContext2D, x, y, width, height, localOptions) {
2021-03-06 16:38:04 +01:00
ctx.beginPath();
2021-04-13 17:05:52 +02:00
if (localOptions.useCurves) {
2021-03-06 16:38:04 +01:00
const cx = (x + x + width) / 2;
const cy = (y + y + height) / 2;
ctx.ellipse(cx, cy, width / 2, height / 2, 0, 0, 2 * Math.PI);
} else {
2021-04-13 17:05:52 +02:00
ctx.lineWidth = localOptions.lineWidth;
ctx.moveTo(x + localOptions.roundRect, y);
ctx.lineTo(x + width - localOptions.roundRect, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + localOptions.roundRect);
ctx.lineTo(x + width, y + height - localOptions.roundRect);
ctx.quadraticCurveTo(x + width, y + height, x + width - localOptions.roundRect, y + height);
ctx.lineTo(x + localOptions.roundRect, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - localOptions.roundRect);
ctx.lineTo(x, y + localOptions.roundRect);
ctx.quadraticCurveTo(x, y, x + localOptions.roundRect, y);
2021-03-05 17:43:50 +01:00
ctx.closePath();
}
2021-03-06 16:38:04 +01:00
ctx.stroke();
2021-03-05 17:43:50 +01:00
}
2021-11-05 16:28:06 +01:00
function lines(ctx: CanvasRenderingContext2D, points: Point[], localOptions) {
if (points.length < 2) return;
2021-03-05 20:30:09 +01:00
ctx.beginPath();
2021-03-06 16:38:04 +01:00
ctx.moveTo(points[0][0], points[0][1]);
2021-03-12 22:43:36 +01:00
for (const pt of points) {
2021-05-22 20:53:51 +02:00
const z = pt[2] || 0;
2021-11-05 16:28:06 +01:00
ctx.strokeStyle = localOptions.useDepth && z !== 0 ? `rgba(${127.5 + (2 * z)}, ${127.5 - (2 * z)}, 255, 0.3)` : localOptions.color;
ctx.fillStyle = localOptions.useDepth && z !== 0 ? `rgba(${127.5 + (2 * z)}, ${127.5 - (2 * z)}, 255, 0.3)` : localOptions.color;
2021-05-22 18:33:19 +02:00
ctx.lineTo(pt[0], Math.round(pt[1]));
2021-03-12 22:43:36 +01:00
}
2021-03-06 16:38:04 +01:00
ctx.stroke();
2021-04-13 17:05:52 +02:00
if (localOptions.fillPolygons) {
2021-03-05 20:30:09 +01:00
ctx.closePath();
2021-03-06 16:38:04 +01:00
ctx.fill();
2021-03-05 20:30:09 +01:00
}
}
2021-11-05 16:28:06 +01:00
function curves(ctx: CanvasRenderingContext2D, points: Point[], localOptions) {
if (points.length < 2) return;
2021-04-13 17:05:52 +02:00
if (!localOptions.useCurves || points.length <= 2) {
lines(ctx, points, localOptions);
2021-03-06 16:38:04 +01:00
return;
2021-03-05 17:43:50 +01:00
}
ctx.moveTo(points[0][0], points[0][1]);
2021-03-06 16:38:04 +01:00
for (let i = 0; i < points.length - 2; i++) {
const xc = (points[i][0] + points[i + 1][0]) / 2;
const yc = (points[i][1] + points[i + 1][1]) / 2;
ctx.quadraticCurveTo(points[i][0], points[i][1], xc, yc);
2021-03-05 17:43:50 +01:00
}
2021-03-06 16:38:04 +01:00
ctx.quadraticCurveTo(points[points.length - 2][0], points[points.length - 2][1], points[points.length - 1][0], points[points.length - 1][1]);
2021-03-05 17:43:50 +01:00
ctx.stroke();
2021-04-13 17:05:52 +02:00
if (localOptions.fillPolygons) {
2021-03-06 16:38:04 +01:00
ctx.closePath();
ctx.fill();
2021-03-05 17:43:50 +01:00
}
}
function arrow(ctx: CanvasRenderingContext2D, from: Point, to: Point, radius = 5) {
let angle;
let x;
let y;
ctx.beginPath();
ctx.moveTo(from[0], from[1]);
ctx.lineTo(to[0], to[1]);
angle = Math.atan2(to[1] - from[1], to[0] - from[0]);
x = radius * Math.cos(angle) + to[0];
y = radius * Math.sin(angle) + to[1];
ctx.moveTo(x, y);
angle += (1.0 / 3.0) * (2 * Math.PI);
x = radius * Math.cos(angle) + to[0];
y = radius * Math.sin(angle) + to[1];
ctx.lineTo(x, y);
angle += (1.0 / 3.0) * (2 * Math.PI);
x = radius * Math.cos(angle) + to[0];
y = radius * Math.sin(angle) + to[1];
ctx.lineTo(x, y);
ctx.closePath();
ctx.stroke();
ctx.fill();
}
2021-10-25 19:09:00 +02:00
/** draw detected gestures */
export async function gesture(inCanvas: AnyCanvas, result: Array<GestureResult>, drawOptions?: Partial<DrawOptions>) {
2021-04-13 17:05:52 +02:00
const localOptions = mergeDeep(options, drawOptions);
2021-03-05 17:43:50 +01:00
if (!result || !inCanvas) return;
2021-10-21 16:54:51 +02:00
if (localOptions.drawGestures) {
const ctx = getCanvasContext(inCanvas);
2021-11-14 17:22:52 +01:00
if (!ctx) return;
2021-10-21 16:54:51 +02:00
ctx.font = localOptions.font;
ctx.fillStyle = localOptions.color;
let i = 1;
for (let j = 0; j < result.length; j++) {
let where: unknown[] = []; // what&where is a record
let what: unknown[] = []; // what&where is a record
[where, what] = Object.entries(result[j]);
if ((what.length > 1) && ((what[1] as string).length > 0)) {
const who = where[1] as number > 0 ? `#${where[1]}` : '';
const label = `${where[0]} ${who}: ${what[1]}`;
if (localOptions.shadowColor && localOptions.shadowColor !== '') {
ctx.fillStyle = localOptions.shadowColor;
ctx.fillText(label, 8, 2 + (i * localOptions.lineHeight));
}
ctx.fillStyle = localOptions.labelColor;
ctx.fillText(label, 6, 0 + (i * localOptions.lineHeight));
i += 1;
2021-03-05 17:43:50 +01:00
}
}
}
}
2021-10-25 19:09:00 +02:00
/** draw detected faces */
export async function face(inCanvas: AnyCanvas, result: Array<FaceResult>, drawOptions?: Partial<DrawOptions>) {
2021-04-13 17:05:52 +02:00
const localOptions = mergeDeep(options, drawOptions);
2021-03-05 17:43:50 +01:00
if (!result || !inCanvas) return;
2021-09-01 00:22:16 +02:00
const ctx = getCanvasContext(inCanvas);
2021-11-14 17:22:52 +01:00
if (!ctx) return;
2021-03-05 17:43:50 +01:00
for (const f of result) {
2021-04-13 17:05:52 +02:00
ctx.font = localOptions.font;
ctx.strokeStyle = localOptions.color;
ctx.fillStyle = localOptions.color;
2021-05-25 14:58:20 +02:00
if (localOptions.drawBoxes) rect(ctx, f.box[0], f.box[1], f.box[2], f.box[3], localOptions);
2021-10-21 16:54:51 +02:00
if (localOptions.drawLabels) {
// silly hack since fillText does not suport new line
const labels:string[] = [];
labels.push(`face: ${Math.trunc(100 * f.score)}%`);
if (f.genderScore) labels.push(`${f.gender || ''} ${Math.trunc(100 * f.genderScore)}%`);
if (f.age) labels.push(`age: ${f.age || ''}`);
if (f.iris) labels.push(`distance: ${f.iris}`);
if (f.real) labels.push(`real: ${Math.trunc(100 * f.real)}%`);
if (f.live) labels.push(`live: ${Math.trunc(100 * f.live)}%`);
2021-10-21 16:54:51 +02:00
if (f.emotion && f.emotion.length > 0) {
const emotion = f.emotion.map((a) => `${Math.trunc(100 * a.score)}% ${a.emotion}`);
if (emotion.length > 3) emotion.length = 3;
labels.push(emotion.join(' '));
}
if (f.rotation && f.rotation.angle && f.rotation.gaze) {
if (f.rotation.angle.roll) labels.push(`roll: ${rad2deg(f.rotation.angle.roll)}° yaw:${rad2deg(f.rotation.angle.yaw)}° pitch:${rad2deg(f.rotation.angle.pitch)}°`);
if (f.rotation.gaze.bearing) labels.push(`gaze: ${rad2deg(f.rotation.gaze.bearing)}°`);
}
if (labels.length === 0) labels.push('face');
ctx.fillStyle = localOptions.color;
for (let i = labels.length - 1; i >= 0; i--) {
const x = Math.max(f.box[0], 0);
const y = i * localOptions.lineHeight + f.box[1];
if (localOptions.shadowColor && localOptions.shadowColor !== '') {
ctx.fillStyle = localOptions.shadowColor;
ctx.fillText(labels[i], x + 5, y + 16);
}
ctx.fillStyle = localOptions.labelColor;
ctx.fillText(labels[i], x + 4, y + 15);
2021-03-05 17:43:50 +01:00
}
}
ctx.lineWidth = 1;
if (f.mesh && f.mesh.length > 0) {
2021-04-13 17:05:52 +02:00
if (localOptions.drawPoints) {
for (const pt of f.mesh) point(ctx, pt[0], pt[1], pt[2], localOptions);
2021-03-05 17:43:50 +01:00
}
2021-04-13 17:05:52 +02:00
if (localOptions.drawPolygons) {
2021-03-12 22:43:36 +01:00
ctx.lineWidth = 1;
2021-09-28 18:01:48 +02:00
if (f.mesh.length > 450) {
for (let i = 0; i < triangulation.length / 3; i++) {
const points = [
triangulation[i * 3 + 0],
triangulation[i * 3 + 1],
triangulation[i * 3 + 2],
].map((index) => f.mesh[index]);
lines(ctx, points, localOptions);
}
2021-03-05 17:43:50 +01:00
}
// iris: array[center, left, top, right, bottom]
2021-09-28 18:01:48 +02:00
if (f.annotations && f.annotations['leftEyeIris'] && f.annotations['leftEyeIris'][0]) {
2021-04-13 17:05:52 +02:00
ctx.strokeStyle = localOptions.useDepth ? 'rgba(255, 200, 255, 0.3)' : localOptions.color;
2021-03-05 17:43:50 +01:00
ctx.beginPath();
2021-05-22 18:33:19 +02:00
const sizeX = Math.abs(f.annotations['leftEyeIris'][3][0] - f.annotations['leftEyeIris'][1][0]) / 2;
const sizeY = Math.abs(f.annotations['leftEyeIris'][4][1] - f.annotations['leftEyeIris'][2][1]) / 2;
ctx.ellipse(f.annotations['leftEyeIris'][0][0], f.annotations['leftEyeIris'][0][1], sizeX, sizeY, 0, 0, 2 * Math.PI);
2021-03-05 17:43:50 +01:00
ctx.stroke();
2021-04-13 17:05:52 +02:00
if (localOptions.fillPolygons) {
ctx.fillStyle = localOptions.useDepth ? 'rgba(255, 255, 200, 0.3)' : localOptions.color;
2021-03-05 17:43:50 +01:00
ctx.fill();
}
}
2021-09-28 18:01:48 +02:00
if (f.annotations && f.annotations['rightEyeIris'] && f.annotations['rightEyeIris'][0]) {
2021-04-13 17:05:52 +02:00
ctx.strokeStyle = localOptions.useDepth ? 'rgba(255, 200, 255, 0.3)' : localOptions.color;
2021-03-05 17:43:50 +01:00
ctx.beginPath();
2021-05-22 18:33:19 +02:00
const sizeX = Math.abs(f.annotations['rightEyeIris'][3][0] - f.annotations['rightEyeIris'][1][0]) / 2;
const sizeY = Math.abs(f.annotations['rightEyeIris'][4][1] - f.annotations['rightEyeIris'][2][1]) / 2;
ctx.ellipse(f.annotations['rightEyeIris'][0][0], f.annotations['rightEyeIris'][0][1], sizeX, sizeY, 0, 0, 2 * Math.PI);
2021-03-05 17:43:50 +01:00
ctx.stroke();
2021-04-13 17:05:52 +02:00
if (localOptions.fillPolygons) {
ctx.fillStyle = localOptions.useDepth ? 'rgba(255, 255, 200, 0.3)' : localOptions.color;
2021-03-05 17:43:50 +01:00
ctx.fill();
}
}
2021-11-10 00:10:54 +01:00
if (localOptions.drawGaze && f.rotation?.angle && typeof Path2D !== 'undefined') {
ctx.strokeStyle = 'pink';
const valX = (f.box[0] + f.box[2] / 2) - (f.box[3] * rad2deg(f.rotation.angle.yaw) / 90);
const valY = (f.box[1] + f.box[3] / 2) + (f.box[2] * rad2deg(f.rotation.angle.pitch) / 90);
const pathV = new Path2D(`
M ${f.box[0] + f.box[2] / 2} ${f.box[1]}
C
${valX} ${f.box[1]},
${valX} ${f.box[1] + f.box[3]},
${f.box[0] + f.box[2] / 2} ${f.box[1] + f.box[3]}
`);
const pathH = new Path2D(`
M ${f.box[0]} ${f.box[1] + f.box[3] / 2}
C
${f.box[0]} ${valY},
${f.box[0] + f.box[2]} ${valY},
${f.box[0] + f.box[2]} ${f.box[1] + f.box[3] / 2}
`);
ctx.stroke(pathH);
ctx.stroke(pathV);
}
if (localOptions.drawGaze && f.rotation?.gaze?.strength && f.rotation?.gaze?.bearing && f.annotations['leftEyeIris'] && f.annotations['rightEyeIris'] && f.annotations['leftEyeIris'][0] && f.annotations['rightEyeIris'][0]) {
ctx.strokeStyle = 'pink';
ctx.fillStyle = 'pink';
2021-05-28 21:53:51 +02:00
const leftGaze = [
f.annotations['leftEyeIris'][0][0] + (Math.sin(f.rotation.gaze.bearing) * f.rotation.gaze.strength * f.box[3]),
f.annotations['leftEyeIris'][0][1] + (Math.cos(f.rotation.gaze.bearing) * f.rotation.gaze.strength * f.box[2]),
2021-05-28 21:53:51 +02:00
];
arrow(ctx, [f.annotations['leftEyeIris'][0][0], f.annotations['leftEyeIris'][0][1]], [leftGaze[0], leftGaze[1]], 4);
2021-05-28 21:53:51 +02:00
const rightGaze = [
f.annotations['rightEyeIris'][0][0] + (Math.sin(f.rotation.gaze.bearing) * f.rotation.gaze.strength * f.box[3]),
f.annotations['rightEyeIris'][0][1] + (Math.cos(f.rotation.gaze.bearing) * f.rotation.gaze.strength * f.box[2]),
2021-05-28 21:53:51 +02:00
];
arrow(ctx, [f.annotations['rightEyeIris'][0][0], f.annotations['rightEyeIris'][0][1]], [rightGaze[0], rightGaze[1]], 4);
2021-05-28 21:53:51 +02:00
}
2021-03-05 17:43:50 +01:00
}
}
}
}
2021-10-25 19:09:00 +02:00
/** draw detected bodies */
export async function body(inCanvas: AnyCanvas, result: Array<BodyResult>, drawOptions?: Partial<DrawOptions>) {
2021-04-13 17:05:52 +02:00
const localOptions = mergeDeep(options, drawOptions);
2021-03-05 17:43:50 +01:00
if (!result || !inCanvas) return;
2021-09-01 00:22:16 +02:00
const ctx = getCanvasContext(inCanvas);
2021-11-14 17:22:52 +01:00
if (!ctx) return;
2021-03-05 17:43:50 +01:00
ctx.lineJoin = 'round';
for (let i = 0; i < result.length; i++) {
2021-04-13 17:05:52 +02:00
ctx.strokeStyle = localOptions.color;
2021-04-24 17:49:26 +02:00
ctx.fillStyle = localOptions.color;
2021-04-13 17:05:52 +02:00
ctx.lineWidth = localOptions.lineWidth;
2021-04-24 17:49:26 +02:00
ctx.font = localOptions.font;
2021-05-22 18:33:19 +02:00
if (localOptions.drawBoxes && result[i].box && result[i].box?.length === 4) {
2021-04-24 17:49:26 +02:00
rect(ctx, result[i].box[0], result[i].box[1], result[i].box[2], result[i].box[3], localOptions);
if (localOptions.drawLabels) {
if (localOptions.shadowColor && localOptions.shadowColor !== '') {
ctx.fillStyle = localOptions.shadowColor;
ctx.fillText(`body ${100 * result[i].score}%`, result[i].box[0] + 3, 1 + result[i].box[1] + localOptions.lineHeight, result[i].box[2]);
}
ctx.fillStyle = localOptions.labelColor;
ctx.fillText(`body ${100 * result[i].score}%`, result[i].box[0] + 2, 0 + result[i].box[1] + localOptions.lineHeight, result[i].box[2]);
}
}
if (localOptions.drawPoints && result[i].keypoints) {
2021-03-05 17:43:50 +01:00
for (let pt = 0; pt < result[i].keypoints.length; pt++) {
ctx.fillStyle = localOptions.useDepth && result[i].keypoints[pt].position[2] ? `rgba(${127.5 + (2 * (result[i].keypoints[pt].position[2] || 0))}, ${127.5 - (2 * (result[i].keypoints[pt].position[2] || 0))}, 255, 0.5)` : localOptions.color;
point(ctx, result[i].keypoints[pt].position[0], result[i].keypoints[pt].position[1], 0, localOptions);
2021-03-05 20:30:09 +01:00
}
}
if (localOptions.drawLabels && result[i].keypoints) {
2021-04-13 17:05:52 +02:00
ctx.font = localOptions.font;
for (const pt of result[i].keypoints) {
ctx.fillStyle = localOptions.useDepth && pt.position[2] ? `rgba(${127.5 + (2 * pt.position[2])}, ${127.5 - (2 * pt.position[2])}, 255, 0.5)` : localOptions.color;
ctx.fillText(`${pt.part} ${Math.trunc(100 * pt.score)}%`, pt.position[0] + 4, pt.position[1] + 4);
2021-03-05 17:43:50 +01:00
}
}
if (localOptions.drawPolygons && result[i].keypoints && result[i].annotations) {
for (const part of Object.values(result[i].annotations)) {
for (const connected of part) curves(ctx, connected, localOptions);
}
2021-03-05 17:43:50 +01:00
}
}
}
2021-10-25 19:09:00 +02:00
/** draw detected hands */
export async function hand(inCanvas: AnyCanvas, result: Array<HandResult>, drawOptions?: Partial<DrawOptions>) {
2021-04-13 17:05:52 +02:00
const localOptions = mergeDeep(options, drawOptions);
2021-03-05 17:43:50 +01:00
if (!result || !inCanvas) return;
2021-09-01 00:22:16 +02:00
const ctx = getCanvasContext(inCanvas);
2021-11-14 17:22:52 +01:00
if (!ctx) return;
2021-03-05 17:43:50 +01:00
ctx.lineJoin = 'round';
2021-04-13 17:05:52 +02:00
ctx.font = localOptions.font;
2021-03-05 17:43:50 +01:00
for (const h of result) {
2021-04-13 17:05:52 +02:00
if (localOptions.drawBoxes) {
ctx.strokeStyle = localOptions.color;
ctx.fillStyle = localOptions.color;
2021-05-25 14:58:20 +02:00
rect(ctx, h.box[0], h.box[1], h.box[2], h.box[3], localOptions);
2021-04-13 17:05:52 +02:00
if (localOptions.drawLabels) {
if (localOptions.shadowColor && localOptions.shadowColor !== '') {
ctx.fillStyle = localOptions.shadowColor;
2021-09-28 19:48:29 +02:00
ctx.fillText(`hand:${Math.trunc(100 * h.score)}%`, h.box[0] + 3, 1 + h.box[1] + localOptions.lineHeight, h.box[2]); // can use h.label
}
2021-04-13 17:05:52 +02:00
ctx.fillStyle = localOptions.labelColor;
2021-09-28 19:48:29 +02:00
ctx.fillText(`hand:${Math.trunc(100 * h.score)}%`, h.box[0] + 2, 0 + h.box[1] + localOptions.lineHeight, h.box[2]); // can use h.label
2021-03-05 17:43:50 +01:00
}
ctx.stroke();
}
2021-04-13 17:05:52 +02:00
if (localOptions.drawPoints) {
if (h.keypoints && h.keypoints.length > 0) {
for (const pt of h.keypoints) {
2021-09-27 15:19:43 +02:00
ctx.fillStyle = localOptions.useDepth ? `rgba(${127.5 + (2 * (pt[2] || 0))}, ${127.5 - (2 * (pt[2] || 0))}, 255, 0.5)` : localOptions.color;
2021-04-13 17:05:52 +02:00
point(ctx, pt[0], pt[1], 0, localOptions);
2021-03-05 17:43:50 +01:00
}
}
}
2021-09-21 22:48:16 +02:00
if (localOptions.drawLabels && h.annotations) {
2021-04-26 13:37:29 +02:00
const addHandLabel = (part, title) => {
2021-09-21 22:48:16 +02:00
if (!part || part.length === 0 || !part[0]) return;
2021-04-26 13:37:29 +02:00
ctx.fillStyle = localOptions.useDepth ? `rgba(${127.5 + (2 * part[part.length - 1][2])}, ${127.5 - (2 * part[part.length - 1][2])}, 255, 0.5)` : localOptions.color;
ctx.fillText(title, part[part.length - 1][0] + 4, part[part.length - 1][1] + 4);
};
ctx.font = localOptions.font;
addHandLabel(h.annotations['index'], 'index');
addHandLabel(h.annotations['middle'], 'middle');
addHandLabel(h.annotations['ring'], 'ring');
2021-05-22 18:33:19 +02:00
addHandLabel(h.annotations['pinky'], 'pinky');
addHandLabel(h.annotations['thumb'], 'thumb');
addHandLabel(h.annotations['palm'], 'palm');
2021-04-26 13:37:29 +02:00
}
2021-09-21 22:48:16 +02:00
if (localOptions.drawPolygons && h.annotations) {
2021-04-26 13:37:29 +02:00
const addHandLine = (part) => {
2021-09-21 22:48:16 +02:00
if (!part || part.length === 0 || !part[0]) return;
2021-03-05 17:43:50 +01:00
for (let i = 0; i < part.length; i++) {
ctx.beginPath();
2021-10-31 14:06:33 +01:00
ctx.strokeStyle = localOptions.useDepth ? `rgba(${127.5 + (i * part[i][2])}, ${127.5 - (i * part[i][2])}, 255, 0.5)` : localOptions.color;
2021-03-05 17:43:50 +01:00
ctx.moveTo(part[i > 0 ? i - 1 : 0][0], part[i > 0 ? i - 1 : 0][1]);
ctx.lineTo(part[i][0], part[i][1]);
ctx.stroke();
}
};
2021-04-26 13:37:29 +02:00
ctx.lineWidth = localOptions.lineWidth;
addHandLine(h.annotations['index']);
addHandLine(h.annotations['middle']);
addHandLine(h.annotations['ring']);
2021-05-22 18:33:19 +02:00
addHandLine(h.annotations['pinky']);
addHandLine(h.annotations['thumb']);
// addPart(h.annotations.palm);
2021-03-05 17:43:50 +01:00
}
}
}
2021-10-25 19:09:00 +02:00
/** draw detected objects */
export async function object(inCanvas: AnyCanvas, result: Array<ObjectResult>, drawOptions?: Partial<DrawOptions>) {
2021-04-13 17:05:52 +02:00
const localOptions = mergeDeep(options, drawOptions);
if (!result || !inCanvas) return;
2021-09-01 00:22:16 +02:00
const ctx = getCanvasContext(inCanvas);
2021-11-14 17:22:52 +01:00
if (!ctx) return;
ctx.lineJoin = 'round';
2021-04-13 17:05:52 +02:00
ctx.font = localOptions.font;
for (const h of result) {
2021-04-13 17:05:52 +02:00
if (localOptions.drawBoxes) {
ctx.strokeStyle = localOptions.color;
ctx.fillStyle = localOptions.color;
2021-05-25 14:58:20 +02:00
rect(ctx, h.box[0], h.box[1], h.box[2], h.box[3], localOptions);
2021-04-13 17:05:52 +02:00
if (localOptions.drawLabels) {
2021-06-14 16:23:06 +02:00
const label = `${h.label} ${Math.round(100 * h.score)}%`;
2021-04-13 17:05:52 +02:00
if (localOptions.shadowColor && localOptions.shadowColor !== '') {
ctx.fillStyle = localOptions.shadowColor;
ctx.fillText(label, h.box[0] + 3, 1 + h.box[1] + localOptions.lineHeight, h.box[2]);
}
2021-04-13 17:05:52 +02:00
ctx.fillStyle = localOptions.labelColor;
ctx.fillText(label, h.box[0] + 2, 0 + h.box[1] + localOptions.lineHeight, h.box[2]);
}
ctx.stroke();
}
}
}
2021-10-25 19:09:00 +02:00
/** draw combined person results instead of individual detection result objects */
export async function person(inCanvas: AnyCanvas, result: Array<PersonResult>, drawOptions?: Partial<DrawOptions>) {
const localOptions = mergeDeep(options, drawOptions);
if (!result || !inCanvas) return;
2021-09-01 00:22:16 +02:00
const ctx = getCanvasContext(inCanvas);
2021-11-14 17:22:52 +01:00
if (!ctx) return;
ctx.lineJoin = 'round';
ctx.font = localOptions.font;
2021-05-25 14:58:20 +02:00
for (let i = 0; i < result.length; i++) {
if (localOptions.drawBoxes) {
ctx.strokeStyle = localOptions.color;
ctx.fillStyle = localOptions.color;
rect(ctx, result[i].box[0], result[i].box[1], result[i].box[2], result[i].box[3], localOptions);
if (localOptions.drawLabels) {
const label = `person #${i}`;
if (localOptions.shadowColor && localOptions.shadowColor !== '') {
ctx.fillStyle = localOptions.shadowColor;
ctx.fillText(label, result[i].box[0] + 3, 1 + result[i].box[1] + localOptions.lineHeight, result[i].box[2]);
}
ctx.fillStyle = localOptions.labelColor;
ctx.fillText(label, result[i].box[0] + 2, 0 + result[i].box[1] + localOptions.lineHeight, result[i].box[2]);
}
ctx.stroke();
}
}
}
2021-10-25 19:09:00 +02:00
/** draw processed canvas */
2021-11-14 17:22:52 +01:00
export async function canvas(input: AnyCanvas | HTMLImageElement | HTMLMediaElement | HTMLVideoElement, output: AnyCanvas) {
2021-09-15 19:59:18 +02:00
if (!input || !output) return;
const ctx = getCanvasContext(output);
2021-11-14 17:22:52 +01:00
if (!ctx) return;
2021-09-15 19:59:18 +02:00
ctx.drawImage(input, 0, 0);
2021-03-05 17:43:50 +01:00
}
2021-10-25 19:09:00 +02:00
/** meta-function that performs draw for: canvas, face, body, hand
* @returns {Promise}
*/
export async function all(inCanvas: AnyCanvas, result: Result, drawOptions?: Partial<DrawOptions>) {
2021-09-15 19:59:18 +02:00
if (!result || !result.performance || !result || !inCanvas) return null;
2021-10-23 15:38:52 +02:00
const timeStamp = now();
2021-04-13 17:05:52 +02:00
const localOptions = mergeDeep(options, drawOptions);
2021-08-15 00:00:26 +02:00
const promise = Promise.all([
face(inCanvas, result.face, localOptions),
body(inCanvas, result.body, localOptions),
hand(inCanvas, result.hand, localOptions),
object(inCanvas, result.object, localOptions),
gesture(inCanvas, result.gesture, localOptions), // gestures do not have buffering
2021-09-16 00:58:54 +02:00
// person(inCanvas, result.persons, localOptions); // already included above
2021-08-15 00:00:26 +02:00
]);
2021-10-27 15:45:38 +02:00
drawTime = env.perfadd ? drawTime + Math.round(now() - timeStamp) : Math.round(now() - timeStamp);
result.performance.draw = drawTime;
2021-08-15 00:00:26 +02:00
return promise;
2021-03-05 17:43:50 +01:00
}