diff --git a/CHANGELOG.md b/CHANGELOG.md index 5abe371d..fcc05e74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,9 @@ ## Changelog -### **HEAD -> main** 2021/11/10 mandic00@live.com +### **HEAD -> main** 2021/11/11 mandic00@live.com +- documentation overhaul - auto tensor shape and channels handling - disable use of path2d in node - add liveness module and facerecognition demo diff --git a/demo/faceid/index.html b/demo/faceid/index.html index 8409ab7c..0367f77e 100644 --- a/demo/faceid/index.html +++ b/demo/faceid/index.html @@ -18,7 +18,7 @@ html { font-family: 'Lato', 'Segoe UI'; font-size: 16px; font-variant: small-caps; } body { margin: 0; padding: 16px; background: black; color: white; overflow-x: hidden; width: 100vw; height: 100vh; } body::-webkit-scrollbar { display: none; } - .button { padding: 2px; cursor: pointer; box-shadow: 2px 2px black; width: 64px; text-align: center; margin-left: 16px; height: 16px } + .button { padding: 2px; cursor: pointer; box-shadow: 2px 2px black; width: 64px; text-align: center; margin-left: 16px; height: 16px; display: none }
diff --git a/demo/faceid/index.js b/demo/faceid/index.js index 76b80bc4..d48ffc3f 100644 --- a/demo/faceid/index.js +++ b/demo/faceid/index.js @@ -225,19 +225,13 @@ async function deleteRecord() { } } async function detectFace() { - var _a; + var _a, _b; + (_a = dom.canvas.getContext("2d")) == null ? void 0 : _a.clearRect(0, 0, options.minSize, options.minSize); if (!face || !face.tensor || !face.embedding) return 0; - dom.canvas.width = face.tensor.shape[1] || 0; - dom.canvas.height = face.tensor.shape[0] || 0; - dom.source.width = dom.canvas.width; - dom.source.height = dom.canvas.height; - dom.canvas.style.width = ""; human.tf.browser.toPixels(face.tensor, dom.canvas); const descriptors = db2.map((rec) => rec.descriptor); const res = await human.match(face.embedding, descriptors); - dom.match.style.display = "flex"; - dom.retry.style.display = "block"; if (res.index === -1) { log2("no matches"); dom.delete.style.display = "none"; @@ -248,11 +242,12 @@ async function detectFace() { dom.delete.style.display = ""; dom.name.value = current.name; dom.source.style.display = ""; - (_a = dom.source.getContext("2d")) == null ? void 0 : _a.putImageData(current.image, 0, 0); + (_b = dom.source.getContext("2d")) == null ? void 0 : _b.putImageData(current.image, 0, 0); } return res.similarity > options.threshold; } async function main() { + var _a, _b; ok.faceCount = false; ok.faceConfidence = false; ok.facingCenter = false; @@ -269,9 +264,16 @@ async function main() { startTime = human.now(); face = await validationLoop(); dom.fps.style.display = "none"; + dom.canvas.width = ((_a = face == null ? void 0 : face.tensor) == null ? void 0 : _a.shape[1]) || options.minSize; + dom.canvas.height = ((_b = face == null ? void 0 : face.tensor) == null ? void 0 : _b.shape[0]) || options.minSize; + dom.source.width = dom.canvas.width; + dom.source.height = dom.canvas.height; + dom.canvas.style.width = ""; + dom.match.style.display = "flex"; + dom.retry.style.display = "block"; if (!allOk()) { - log2("did not find valid input", face); - return 0; + log2("did not find valid face"); + return false; } else { const res = await detectFace(); document.body.style.background = res ? "darkgreen" : "maroon"; diff --git a/demo/faceid/index.ts b/demo/faceid/index.ts index 5f706d64..c4311b0b 100644 --- a/demo/faceid/index.ts +++ b/demo/faceid/index.ts @@ -179,18 +179,11 @@ async function deleteRecord() { } async function detectFace() { - // draw face and dispose face tensor immediatey afterwards + dom.canvas.getContext('2d')?.clearRect(0, 0, options.minSize, options.minSize); if (!face || !face.tensor || !face.embedding) return 0; - dom.canvas.width = face.tensor.shape[1] || 0; - dom.canvas.height = face.tensor.shape[0] || 0; - dom.source.width = dom.canvas.width; - dom.source.height = dom.canvas.height; - dom.canvas.style.width = ''; human.tf.browser.toPixels(face.tensor as unknown as TensorLike, dom.canvas); const descriptors = db.map((rec) => rec.descriptor); const res = await human.match(face.embedding, descriptors); - dom.match.style.display = 'flex'; - dom.retry.style.display = 'block'; if (res.index === -1) { log('no matches'); dom.delete.style.display = 'none'; @@ -223,9 +216,16 @@ async function main() { // main entry point startTime = human.now(); face = await validationLoop(); // start validation loop dom.fps.style.display = 'none'; + dom.canvas.width = face?.tensor?.shape[1] || options.minSize; + dom.canvas.height = face?.tensor?.shape[0] || options.minSize; + dom.source.width = dom.canvas.width; + dom.source.height = dom.canvas.height; + dom.canvas.style.width = ''; + dom.match.style.display = 'flex'; + dom.retry.style.display = 'block'; if (!allOk()) { - log('did not find valid input', face); - return 0; + log('did not find valid face'); + return false; } else { // log('found valid face'); const res = await detectFace(); diff --git a/demo/facematch/facematch.js b/demo/facematch/facematch.js index 09cdc089..3338eca7 100644 --- a/demo/facematch/facematch.js +++ b/demo/facematch/facematch.js @@ -184,9 +184,8 @@ async function AddImageElement(index, image, length) { return new Promise((resolve) => { const img = new Image(128, 128); img.onload = () => { // must wait until image is loaded - human.detect(img, userConfig).then(async (res) => { + human.detect(img, userConfig).then((res) => { const ok = AddFaceCanvas(index, res, image); // then wait until image is analyzed - // log('Add image:', index + 1, image, 'faces:', res.face.length); if (ok) document.getElementById('images').appendChild(img); // and finally we can add it resolve(true); }); @@ -250,11 +249,13 @@ async function main() { // const promises = []; // for (let i = 0; i < images.length; i++) promises.push(AddImageElement(i, images[i], images.length)); // await Promise.all(promises); + const t0 = human.now(); for (let i = 0; i < images.length; i++) await AddImageElement(i, images[i], images.length); + const t1 = human.now(); // print stats const num = all.reduce((prev, cur) => prev += cur.length, 0); - log('Extracted faces:', num, 'from images:', all.length); + log('Extracted faces:', num, 'from images:', all.length, 'time:', Math.round(t1 - t0)); log(human.tf.engine().memory()); // if we didn't download db, generate it from current faces diff --git a/src/face/match.ts b/src/face/match.ts index 40d6d090..b96a67ec 100644 --- a/src/face/match.ts +++ b/src/face/match.ts @@ -1,6 +1,6 @@ /** Face descriptor type as number array */ export type Descriptor = Array