human/src/draw/hand.ts

66 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-04-11 17:45:24 +02:00
import { mergeDeep } from '../util/util';
2022-10-18 16:18:40 +02:00
import { getCanvasContext, rect, point, colorDepth, replace, labels } from './primitives';
2022-04-11 17:45:24 +02:00
import { options } from './options';
import type { HandResult } from '../result';
import type { AnyCanvas, DrawOptions, Point } from '../exports';
/** draw detected hands */
2022-08-21 21:23:03 +02:00
export function hand(inCanvas: AnyCanvas, result: HandResult[], drawOptions?: Partial<DrawOptions>) {
const localOptions: DrawOptions = mergeDeep(options, drawOptions);
2022-04-11 17:45:24 +02:00
if (!result || !inCanvas) return;
2022-11-04 18:20:56 +01:00
const ctx = getCanvasContext(inCanvas) as CanvasRenderingContext2D;
2022-04-11 17:45:24 +02:00
if (!ctx) return;
ctx.lineJoin = 'round';
ctx.font = localOptions.font;
for (const h of result) {
if (localOptions.drawBoxes) {
ctx.strokeStyle = localOptions.color;
ctx.fillStyle = localOptions.color;
rect(ctx, h.box[0], h.box[1], h.box[2], h.box[3], localOptions);
2022-10-18 16:18:40 +02:00
if (localOptions.drawLabels && (localOptions.handLabels?.length > 0)) {
let l = localOptions.handLabels.slice();
2023-04-03 16:36:01 +02:00
l = replace(l, '[id]', h.id.toFixed(0));
2022-10-18 16:18:40 +02:00
l = replace(l, '[label]', h.label);
l = replace(l, '[score]', 100 * h.score);
labels(ctx, l, h.box[0], h.box[1], localOptions);
2022-04-11 17:45:24 +02:00
}
ctx.stroke();
}
if (localOptions.drawPoints) {
if (h.keypoints && h.keypoints.length > 0) {
for (const pt of h.keypoints) {
2022-04-18 17:29:45 +02:00
ctx.fillStyle = colorDepth(pt[2], localOptions);
2022-04-11 17:45:24 +02:00
point(ctx, pt[0], pt[1], 0, localOptions);
}
}
}
2022-10-18 16:18:40 +02:00
if (localOptions.drawLabels && h.annotations && (localOptions.fingerLabels?.length > 0)) {
for (const [part, pt] of Object.entries(h.annotations)) {
let l = localOptions.fingerLabels.slice();
l = replace(l, '[label]', part);
labels(ctx, l, pt[pt.length - 1][0], pt[pt.length - 1][1], localOptions);
}
2022-04-11 17:45:24 +02:00
}
if (localOptions.drawPolygons && h.annotations) {
2022-08-21 19:34:51 +02:00
const addHandLine = (part: Point[]) => {
2022-04-11 17:45:24 +02:00
if (!part || part.length === 0 || !part[0]) return;
for (let i = 0; i < part.length; i++) {
ctx.beginPath();
const z = part[i][2] || 0;
2022-04-18 17:29:45 +02:00
ctx.strokeStyle = colorDepth(i * z, localOptions);
2022-04-11 17:45:24 +02: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();
}
};
ctx.lineWidth = localOptions.lineWidth;
2022-08-21 19:34:51 +02:00
addHandLine(h.annotations.index);
addHandLine(h.annotations.middle);
addHandLine(h.annotations.ring);
addHandLine(h.annotations.pinky);
addHandLine(h.annotations.thumb);
2022-04-11 17:45:24 +02:00
// addPart(h.annotations.palm);
}
}
}