From c099c1006f303f9dd8925cf11855ee3ea4f0feff Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Mon, 11 Jan 2021 09:02:02 -0500 Subject: [PATCH] added iris gesture --- demo/browser.js | 2 +- package.json | 26 +++++++++++++------------- src/gesture/gesture.js | 19 +++++++++++++++++++ src/human.js | 2 +- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/demo/browser.js b/demo/browser.js index 17de5bf3..437bbdbe 100644 --- a/demo/browser.js +++ b/demo/browser.js @@ -219,7 +219,7 @@ async function setupCamera() { // silly font resizing for paint-on-canvas since viewport can be zoomed const size = Math.trunc(window.devicePixelRatio * (8 + (4 * canvas.width / window.innerWidth))); ui.baseFont = ui.baseFontProto.replace(/{size}/, `${size}px`); - ui.baseLineHeight = size + 4; + ui.baseLineHeight = size + 2; if (live) video.play(); // eslint-disable-next-line no-use-before-define if (live && !ui.detectThread) runHumanDetect(video, canvas); diff --git a/package.json b/package.json index 325b3b79..bdc56bfd 100644 --- a/package.json +++ b/package.json @@ -22,20 +22,20 @@ "dependencies": {}, "peerDependencies": {}, "devDependencies": { - "@tensorflow/tfjs": "^2.8.2", - "@tensorflow/tfjs-backend-cpu": ">=2.8.2", - "@tensorflow/tfjs-backend-wasm": "^2.8.2", - "@tensorflow/tfjs-backend-webgl": "^2.8.2", - "@tensorflow/tfjs-converter": "^2.8.2", - "@tensorflow/tfjs-core": "^2.8.2", - "@tensorflow/tfjs-data": "^2.8.2", - "@tensorflow/tfjs-layers": "^2.8.2", - "@tensorflow/tfjs-node": "^2.8.2", - "@tensorflow/tfjs-node-gpu": "^2.8.2", + "@tensorflow/tfjs": "^2.8.3", + "@tensorflow/tfjs-backend-cpu": "^2.8.3", + "@tensorflow/tfjs-backend-wasm": "^2.8.3", + "@tensorflow/tfjs-backend-webgl": "^2.8.3", + "@tensorflow/tfjs-converter": "^2.8.3", + "@tensorflow/tfjs-core": "^2.8.3", + "@tensorflow/tfjs-data": "^2.8.3", + "@tensorflow/tfjs-layers": "^2.8.3", + "@tensorflow/tfjs-node": "^2.8.3", + "@tensorflow/tfjs-node-gpu": "^2.8.3", "@vladmandic/pilogger": "^0.2.11", - "chokidar": "^3.4.3", - "dayjs": "^1.10.1", - "esbuild": "^0.8.29", + "chokidar": "^3.5.0", + "dayjs": "^1.10.3", + "esbuild": "^0.8.31", "eslint": "^7.17.0", "eslint-config-airbnb-base": "^14.2.1", "eslint-plugin-import": "^2.22.1", diff --git a/src/gesture/gesture.js b/src/gesture/gesture.js index f2308878..243aa999 100644 --- a/src/gesture/gesture.js +++ b/src/gesture/gesture.js @@ -39,6 +39,25 @@ exports.face = (res) => { return gestures; }; +exports.iris = (res) => { + if (!res) return []; + const gestures = []; + for (let i = 0; i < res.length; i++) { + if (!res[i].annotations || !res[i].annotations.leftEyeIris || !res[i].annotations.rightEyeIris) continue; + const sizeXLeft = res[i].annotations.leftEyeIris[3][0] - res[i].annotations.leftEyeIris[1][0]; + const sizeYLeft = res[i].annotations.leftEyeIris[4][1] - res[i].annotations.leftEyeIris[2][1]; + const areaLeft = Math.abs(sizeXLeft * sizeYLeft); + + const sizeXRight = res[i].annotations.rightEyeIris[3][0] - res[i].annotations.rightEyeIris[1][0]; + const sizeYRight = res[i].annotations.rightEyeIris[4][1] - res[i].annotations.rightEyeIris[2][1]; + const areaRight = Math.abs(sizeXRight * sizeYRight); + + const difference = Math.abs(areaLeft - areaRight) / Math.max(areaLeft, areaRight); + if (difference < 0.25) gestures.push({ iris: i, gesture: 'looking at camera' }); + } + return gestures; +}; + exports.hand = (res) => { if (!res) return []; const gestures = []; diff --git a/src/human.js b/src/human.js index feab8a18..0f20c5cb 100644 --- a/src/human.js +++ b/src/human.js @@ -449,7 +449,7 @@ class Human { let gestureRes = []; if (this.config.gesture.enabled) { timeStamp = now(); - gestureRes = [...gesture.face(faceRes), ...gesture.body(poseRes), ...gesture.hand(handRes)]; + gestureRes = [...gesture.face(faceRes), ...gesture.body(poseRes), ...gesture.hand(handRes), ...gesture.iris(faceRes)]; if (!this.config.async) this.perf.gesture = Math.trunc(now() - timeStamp); else if (this.perf.gesture) delete this.perf.gesture; }