From 34f01f20539bceabeb9aa81a41c1ac8bfb37066c Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Fri, 2 Apr 2021 08:37:35 -0400 Subject: [PATCH] input type validation --- CHANGELOG.md | 4 ++++ demo/facematch.js | 30 +++++++++++++++++------------- src/human.ts | 2 +- src/image/image.ts | 11 ++++++++++- 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6dc7739e..ed2d6efe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ Repository: **** ## Changelog +### **HEAD -> main** 2021/04/02 mandic00@live.com + +- normalize all scores + ### **1.3.1** 2021/03/30 mandic00@live.com - added face3d demo diff --git a/demo/facematch.js b/demo/facematch.js index af94d832..c1233c5c 100644 --- a/demo/facematch.js +++ b/demo/facematch.js @@ -43,7 +43,8 @@ function log(...msg) { async function getFaceDB() { // download db with known faces try { - const res = await fetch('/demo/facematch-faces.json'); + let res = await fetch('/demo/facematch-faces.json'); + if (!res || !res.ok) res = await fetch('/human/demo/facematch-faces.json'); db = (res && res.ok) ? await res.json() : []; for (const rec of db) { rec.embedding = rec.embedding.map((a) => parseFloat(a.toFixed(4))); @@ -171,7 +172,9 @@ async function main() { let res; let images = []; let dir = []; + // load face descriptor database await getFaceDB(); + // enumerate all sample images in /assets res = await fetch('/assets'); dir = (res && res.ok) ? await res.json() : []; @@ -185,24 +188,25 @@ async function main() { res = await fetch('/private/err'); dir = (res && res.ok) ? await res.json() : []; images = images.concat(dir.filter((img) => (img.endsWith('.jpg')))); - log('Enumerated:', images.length, 'images'); // could not dynamically enumerate images so using static list if (images.length === 0) { images = [ - '/human/assets/sample1.jpg', - '/human/assets/sample2.jpg', - '/human/assets/sample3.jpg', - '/human/assets/sample4.jpg', - '/human/assets/sample5.jpg', - '/human/assets/sample6.jpg', - '/human/assets/sample6.jpg', - '/human/assets/sample-me.jpg', - '/human/assets/human-sample-face.jpg', - '/human/assets/human-sample-upper.jpg', - '/human/assets/human-sample-body.jpg', + 'sample1.jpg', + 'sample2.jpg', + 'sample3.jpg', + 'sample4.jpg', + 'sample5.jpg', + 'sample6.jpg', + 'sample6.jpg', + 'sample-me.jpg', + 'human-sample-face.jpg', + 'human-sample-upper.jpg', + 'human-sample-body.jpg', ]; + // add prefix for gitpages + images = images.map((a) => `/human/assets/${a}`); log('Adding static image list:', images.length, 'images'); } diff --git a/src/human.ts b/src/human.ts index 1daece57..e0f3abf7 100644 --- a/src/human.ts +++ b/src/human.ts @@ -28,7 +28,7 @@ export type Tensor = typeof tf.Tensor; export type { Config } from './config'; export type { Result } from './result'; /** Defines all possible input types for **Human** detection */ -export type Input = Tensor | ImageData | ImageBitmap | HTMLVideoElement | HTMLCanvasElement | OffscreenCanvas; +export type Input = Tensor | ImageData | ImageBitmap | HTMLImageElement | HTMLVideoElement | HTMLCanvasElement | OffscreenCanvas; /** Error message */ export type Error = { error: string }; /** Instance of TensorFlow/JS */ diff --git a/src/image/image.ts b/src/image/image.ts index e09c4a3e..13248b14 100644 --- a/src/image/image.ts +++ b/src/image/image.ts @@ -16,7 +16,16 @@ let fx = null; export function process(input, config): { tensor: tf.Tensor, canvas: OffscreenCanvas | HTMLCanvasElement } { let tensor; if (!input) throw new Error('Human: Input is missing'); - if (!(input instanceof tf.Tensor) && !(input instanceof ImageData) && !(input instanceof ImageBitmap) && !(input instanceof HTMLVideoElement) && !(input instanceof HTMLCanvasElement) && !(input instanceof OffscreenCanvas)) { + if ( + !(input instanceof tf.Tensor) + && !(input instanceof Image) + && !(input instanceof ImageData) + && !(input instanceof ImageBitmap) + && !(input instanceof HTMLImageElement) + && !(input instanceof HTMLVideoElement) + && !(input instanceof HTMLCanvasElement) + && !(input instanceof OffscreenCanvas) + ) { throw new Error('Human: Input type is not recognized'); } if (input instanceof tf.Tensor) {