human/src/draw/hand.ts

75 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-04-11 17:45:24 +02:00
import { mergeDeep } from '../util/util';
import { getCanvasContext, rect, point, colorDepth } from './primitives';
import { options } from './options';
import type { HandResult } from '../result';
import type { AnyCanvas, DrawOptions, Point } from '../exports';
/** draw detected hands */
2022-08-21 19:34:51 +02:00
export async function hand(inCanvas: AnyCanvas, result: HandResult[], drawOptions?: Partial<DrawOptions>) {
2022-04-11 17:45:24 +02:00
const localOptions = mergeDeep(options, drawOptions);
if (!result || !inCanvas) return;
const ctx = getCanvasContext(inCanvas);
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);
if (localOptions.drawLabels) {
if (localOptions.shadowColor && localOptions.shadowColor !== '') {
ctx.fillStyle = localOptions.shadowColor;
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
}
ctx.fillStyle = localOptions.labelColor;
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
}
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);
}
}
}
if (localOptions.drawLabels && h.annotations) {
2022-08-21 19:34:51 +02:00
const addHandLabel = (part: Point[], title: string) => {
2022-04-11 17:45:24 +02:00
if (!part || part.length === 0 || !part[0]) return;
2022-04-18 17:29:45 +02:00
const z = part[part.length - 1][2] || -256;
ctx.fillStyle = colorDepth(z, localOptions);
2022-04-11 17:45:24 +02:00
ctx.fillText(title, part[part.length - 1][0] + 4, part[part.length - 1][1] + 4);
};
ctx.font = localOptions.font;
2022-08-21 19:34:51 +02:00
addHandLabel(h.annotations.index, 'index');
addHandLabel(h.annotations.middle, 'middle');
addHandLabel(h.annotations.ring, 'ring');
addHandLabel(h.annotations.pinky, 'pinky');
addHandLabel(h.annotations.thumb, 'thumb');
addHandLabel(h.annotations.palm, 'palm');
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);
}
}
}