From 90a21de40e8110aa2134db89b76827dd24df004b Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Tue, 31 Aug 2021 18:22:16 -0400 Subject: [PATCH] simplify canvas handling in nodejs --- demo/nodejs/node-canvas.js | 2 +- src/draw/draw.ts | 45 ++++++++++---------------------------- src/human.ts | 7 ------ 3 files changed, 13 insertions(+), 41 deletions(-) diff --git a/demo/nodejs/node-canvas.js b/demo/nodejs/node-canvas.js index 789e770f..f95873f8 100644 --- a/demo/nodejs/node-canvas.js +++ b/demo/nodejs/node-canvas.js @@ -114,7 +114,7 @@ async function detect(input, output) { ctx.drawImage(original, 0, 0, outputCanvas.width, outputCanvas.height); // draw original to new canvas // draw human results on canvas - human.setCanvas(outputCanvas); // tell human to use this canvas + // human.setCanvas(outputCanvas); // tell human to use this canvas human.draw.all(outputCanvas, result); // human will draw results as overlays on canvas // write canvas to new image file diff --git a/src/draw/draw.ts b/src/draw/draw.ts index 460d39ee..0a496cf2 100644 --- a/src/draw/draw.ts +++ b/src/draw/draw.ts @@ -66,17 +66,9 @@ export const options: DrawOptions = { bufferedOutput: true, }; -let Canvas; - -export function setCanvas(obj) { - if (obj.getContext) Canvas = obj; - else throw new Error('Human: Canvas is not functional'); -} - -const checkCanvas = (input) => { - if ((typeof HTMLCanvasElement !== 'undefined') && (input instanceof HTMLCanvasElement)) return true; - if (typeof Canvas !== 'undefined') return true; - return false; +const getCanvasContext = (input) => { + if (input && input.getContext) return input.getContext('2d'); + throw new Error('Human: Invalid Canvas'); }; const rad2deg = (theta) => Math.round((theta * 180) / Math.PI); @@ -150,9 +142,7 @@ function curves(ctx, points: [number, number, number?][] = [], localOptions) { export async function gesture(inCanvas: HTMLCanvasElement, result: Array, drawOptions?: DrawOptions) { const localOptions = mergeDeep(options, drawOptions); if (!result || !inCanvas) return; - if (!checkCanvas(inCanvas)) return; - const ctx = inCanvas.getContext('2d'); - if (!ctx) return; + const ctx = getCanvasContext(inCanvas); ctx.font = localOptions.font; ctx.fillStyle = localOptions.color; let i = 1; @@ -177,9 +167,7 @@ export async function gesture(inCanvas: HTMLCanvasElement, result: Array, drawOptions?: DrawOptions) { const localOptions = mergeDeep(options, drawOptions); if (!result || !inCanvas) return; - if (!checkCanvas(inCanvas)) return; - const ctx = inCanvas.getContext('2d'); - if (!ctx) return; + const ctx = getCanvasContext(inCanvas); for (const f of result) { ctx.font = localOptions.font; ctx.strokeStyle = localOptions.color; @@ -281,9 +269,7 @@ export async function face(inCanvas: HTMLCanvasElement, result: Array, dra export async function body(inCanvas: HTMLCanvasElement, result: Array, drawOptions?: DrawOptions) { const localOptions = mergeDeep(options, drawOptions); if (!result || !inCanvas) return; - if (!checkCanvas(inCanvas)) return; - const ctx = inCanvas.getContext('2d'); - if (!ctx) return; + const ctx = getCanvasContext(inCanvas); ctx.lineJoin = 'round'; for (let i = 0; i < result.length; i++) { ctx.strokeStyle = localOptions.color; @@ -393,9 +379,7 @@ export async function body(inCanvas: HTMLCanvasElement, result: Array, dra export async function hand(inCanvas: HTMLCanvasElement, result: Array, drawOptions?: DrawOptions) { const localOptions = mergeDeep(options, drawOptions); if (!result || !inCanvas) return; - if (!checkCanvas(inCanvas)) return; - const ctx = inCanvas.getContext('2d'); - if (!ctx) return; + const ctx = getCanvasContext(inCanvas); ctx.lineJoin = 'round'; ctx.font = localOptions.font; for (const h of result) { @@ -459,9 +443,7 @@ export async function hand(inCanvas: HTMLCanvasElement, result: Array, dra export async function object(inCanvas: HTMLCanvasElement, result: Array, drawOptions?: DrawOptions) { const localOptions = mergeDeep(options, drawOptions); if (!result || !inCanvas) return; - if (!checkCanvas(inCanvas)) return; - const ctx = inCanvas.getContext('2d'); - if (!ctx) return; + const ctx = getCanvasContext(inCanvas); ctx.lineJoin = 'round'; ctx.font = localOptions.font; for (const h of result) { @@ -486,9 +468,7 @@ export async function object(inCanvas: HTMLCanvasElement, result: Array, d export async function person(inCanvas: HTMLCanvasElement, result: Array, drawOptions?: DrawOptions) { const localOptions = mergeDeep(options, drawOptions); if (!result || !inCanvas) return; - if (!checkCanvas(inCanvas)) return; - const ctx = inCanvas.getContext('2d'); - if (!ctx) return; + const ctx = getCanvasContext(inCanvas); ctx.lineJoin = 'round'; ctx.font = localOptions.font; @@ -513,16 +493,15 @@ export async function person(inCanvas: HTMLCanvasElement, result: Array, export async function canvas(inCanvas: HTMLCanvasElement, outCanvas: HTMLCanvasElement) { if (!inCanvas || !outCanvas) return; - if (!checkCanvas(inCanvas) || !checkCanvas(outCanvas)) return; - const outCtx = inCanvas.getContext('2d'); - outCtx?.drawImage(inCanvas, 0, 0); + getCanvasContext(outCanvas); + const ctx = getCanvasContext(inCanvas); + ctx.drawImage(inCanvas, 0, 0); } export async function all(inCanvas: HTMLCanvasElement, result: Result, drawOptions?: DrawOptions) { const timestamp = now(); const localOptions = mergeDeep(options, drawOptions); if (!result || !inCanvas) return null; - if (!checkCanvas(inCanvas)) return null; const promise = Promise.all([ face(inCanvas, result.face, localOptions), diff --git a/src/human.ts b/src/human.ts index 24668883..7445c08a 100644 --- a/src/human.ts +++ b/src/human.ts @@ -190,13 +190,6 @@ export class Human { this.#lastInputSum = 1; } - /** - * Sets internal canvas methods - * - * @param canvas - */ - setCanvas = (canvas) => draw.setCanvas(canvas); - // helper function: measure tensor leak /** @hidden */ analyze = (...msg: string[]) => {