From 53b4542140e3952310dfe5f995fb1674e1023e21 Mon Sep 17 00:00:00 2001 From: Vladimir Mandic Date: Tue, 10 Nov 2020 08:57:39 -0500 Subject: [PATCH] fix bug in async ops and change imports --- demo/browser.js | 6 +++--- src/age/age.js | 4 ++-- src/emotion/emotion.js | 4 ++-- src/gender/gender.js | 4 ++-- src/hand/box.js | 2 +- src/hand/handdetector.js | 2 +- src/hand/handpipeline.js | 2 +- src/hand/handpose.js | 6 +++--- src/human.js | 15 +++++++-------- src/image.js | 2 +- src/tf.js | 19 +++++++++++++++++++ 11 files changed, 42 insertions(+), 24 deletions(-) create mode 100644 src/tf.js diff --git a/demo/browser.js b/demo/browser.js index 1340e55d..228598fa 100644 --- a/demo/browser.js +++ b/demo/browser.js @@ -2,10 +2,10 @@ import Human from '../dist/human.esm.js'; import draw from './draw.js'; import Menu from './menu.js'; -const human = new Human(); - const userConfig = {}; // add any user configuration overrides +const human = new Human(userConfig); + // ui options const ui = { baseColor: 'rgba(173, 216, 230, 0.3)', // 'lightblue' with light alpha channel @@ -134,7 +134,7 @@ async function setupCamera() { const constraints = { audio: false, video: { - facingMode: (ui.facing ? 'user' : 'environment'), + facingMode: ui.facing ? 'user' : 'environment', resizeMode: ui.crop ? 'crop-and-scale' : 'none', width: { ideal: window.innerWidth }, height: { ideal: window.innerHeight }, diff --git a/src/age/age.js b/src/age/age.js index 406e342f..2e82c741 100644 --- a/src/age/age.js +++ b/src/age/age.js @@ -1,4 +1,4 @@ -import * as tf from '@tensorflow/tfjs/dist/tf.es2017.js'; +import { tf, loadGraphModel } from '../tf.js'; import * as profile from '../profile.js'; const models = {}; @@ -10,7 +10,7 @@ const zoom = [0, 0]; // 0..1 meaning 0%..100% async function load(config) { if (!models.age) { - models.age = await tf.loadGraphModel(config.face.age.modelPath); + models.age = await loadGraphModel(config.face.age.modelPath); // eslint-disable-next-line no-console console.log(`Human: load model: ${config.face.age.modelPath.match(/\/(.*)\./)[1]}`); } diff --git a/src/emotion/emotion.js b/src/emotion/emotion.js index 6d1e5c8c..f1c60c7a 100644 --- a/src/emotion/emotion.js +++ b/src/emotion/emotion.js @@ -1,4 +1,4 @@ -import * as tf from '@tensorflow/tfjs/dist/tf.es2017.js'; +import { tf, loadGraphModel } from '../tf.js'; import * as profile from '../profile.js'; const annotations = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surpise', 'neutral']; @@ -13,7 +13,7 @@ const scale = 1; // score multiplication factor async function load(config) { if (!models.emotion) { - models.emotion = await tf.loadGraphModel(config.face.emotion.modelPath); + models.emotion = await loadGraphModel(config.face.emotion.modelPath); // eslint-disable-next-line no-console console.log(`Human: load model: ${config.face.emotion.modelPath.match(/\/(.*)\./)[1]}`); } diff --git a/src/gender/gender.js b/src/gender/gender.js index 5419f7c1..a0677585 100644 --- a/src/gender/gender.js +++ b/src/gender/gender.js @@ -1,4 +1,4 @@ -import * as tf from '@tensorflow/tfjs/dist/tf.es2017.js'; +import { tf, loadGraphModel } from '../tf.js'; import * as profile from '../profile.js'; const models = {}; @@ -12,7 +12,7 @@ const rgb = [0.2989, 0.5870, 0.1140]; // factors for red/green/blue colors when async function load(config) { if (!models.gender) { - models.gender = await tf.loadGraphModel(config.face.gender.modelPath); + models.gender = await loadGraphModel(config.face.gender.modelPath); alternative = models.gender.inputs[0].shape[3] === 1; // eslint-disable-next-line no-console console.log(`Human: load model: ${config.face.gender.modelPath.match(/\/(.*)\./)[1]}`); diff --git a/src/hand/box.js b/src/hand/box.js index 2d2e075c..f3c8eacd 100644 --- a/src/hand/box.js +++ b/src/hand/box.js @@ -14,7 +14,7 @@ * limitations under the License. * ============================================================================= */ -import * as tf from '@tensorflow/tfjs/dist/tf.es2017.js'; +import { tf } from '../tf.js'; function getBoxSize(box) { return [ diff --git a/src/hand/handdetector.js b/src/hand/handdetector.js index 95e1ca78..6268418b 100644 --- a/src/hand/handdetector.js +++ b/src/hand/handdetector.js @@ -15,7 +15,7 @@ * ============================================================================= */ -import * as tf from '@tensorflow/tfjs/dist/tf.es2017.js'; +import { tf } from '../tf.js'; import * as box from './box'; class HandDetector { diff --git a/src/hand/handpipeline.js b/src/hand/handpipeline.js index 899123e4..f74639f3 100644 --- a/src/hand/handpipeline.js +++ b/src/hand/handpipeline.js @@ -15,7 +15,7 @@ * ============================================================================= */ -import * as tf from '@tensorflow/tfjs/dist/tf.es2017.js'; +import { tf } from '../tf.js'; import * as box from './box'; import * as util from './util'; diff --git a/src/hand/handpose.js b/src/hand/handpose.js index f1214a4a..2bb3c509 100644 --- a/src/hand/handpose.js +++ b/src/hand/handpose.js @@ -16,7 +16,7 @@ */ // https://storage.googleapis.com/tfjs-models/demos/handpose/index.html -import * as tf from '@tensorflow/tfjs/dist/tf.es2017.js'; +import { loadGraphModel } from '../tf.js'; import * as handdetector from './handdetector'; import * as pipeline from './handpipeline'; import * as anchors from './anchors'; @@ -69,8 +69,8 @@ exports.HandPose = HandPose; async function load(config) { const [handDetectorModel, handPoseModel] = await Promise.all([ - tf.loadGraphModel(config.detector.modelPath, { fromTFHub: config.detector.modelPath.includes('tfhub.dev') }), - tf.loadGraphModel(config.skeleton.modelPath, { fromTFHub: config.skeleton.modelPath.includes('tfhub.dev') }), + loadGraphModel(config.detector.modelPath, { fromTFHub: config.detector.modelPath.includes('tfhub.dev') }), + loadGraphModel(config.skeleton.modelPath, { fromTFHub: config.skeleton.modelPath.includes('tfhub.dev') }), ]); const detector = new handdetector.HandDetector(handDetectorModel, config.inputSize, anchors.anchors); const pipe = new pipeline.HandPipeline(detector, handPoseModel, config.inputSize); diff --git a/src/human.js b/src/human.js index 3f1e2a17..1134a574 100644 --- a/src/human.js +++ b/src/human.js @@ -1,5 +1,4 @@ -import * as tf from '@tensorflow/tfjs/dist/tf.es2017.js'; -import { setWasmPaths } from '@tensorflow/tfjs-backend-wasm/dist/index.js'; +import { tf, setWasmPaths } from './tf.js'; import * as facemesh from './face/facemesh.js'; import * as age from './age/age.js'; import * as gender from './gender/gender.js'; @@ -131,12 +130,12 @@ class Human { this.models.posenet, this.models.handpose, ] = await Promise.all([ - this.models.age || age.load(this.config), - this.models.gender || gender.load(this.config), - this.models.emotion || emotion.load(this.config), - this.models.facemesh || facemesh.load(this.config.face), - this.models.posenet || posenet.load(this.config), - this.models.handpose || handpose.load(this.config.hand), + this.config.face.age.enabled ? this.models.age || age.load(this.config) : null, + this.config.face.gender.enabled ? this.models.gender || gender.load(this.config) : null, + this.config.face.emotion.enabled ? this.models.emotion || emotion.load(this.config) : null, + this.config.face.enabled ? this.models.facemesh || facemesh.load(this.config.face) : null, + this.config.body.enabled ? this.models.posenet || posenet.load(this.config) : null, + this.config.hand.enabled ? this.models.handpose || handpose.load(this.config.hand) : null, ]); } else { if (this.config.face.enabled && !this.models.facemesh) this.models.facemesh = await facemesh.load(this.config.face); diff --git a/src/image.js b/src/image.js index 4c9bfd35..97a086c7 100644 --- a/src/image.js +++ b/src/image.js @@ -1,4 +1,4 @@ -import * as tf from '@tensorflow/tfjs/dist/tf.es2017.js'; +import { tf } from './tf.js'; import * as fxImage from './imagefx.js'; // internal temp canvases diff --git a/src/tf.js b/src/tf.js new file mode 100644 index 00000000..18a76c90 --- /dev/null +++ b/src/tf.js @@ -0,0 +1,19 @@ +// monolithic: bundle 3.4M +import * as tf from '@tensorflow/tfjs/dist/tf.es2017.js'; +import { setWasmPaths } from '@tensorflow/tfjs-backend-wasm/dist/index.js'; + +const loadGraphModel = tf.loadGraphModel; +export { tf, setWasmPaths, loadGraphModel }; + +// modular: bundle 4.2M +/* +import * as tf from '@tensorflow/tfjs-core/dist/tf-core.es2017.js'; +import { loadGraphModel } from '@tensorflow/tfjs-converter/dist/tf-converter.es2017.js'; +import * as tfCPU from '@tensorflow/tfjs-backend-cpu/dist/tf-backend-cpu.es2017.js'; +import * as tfWebGL from '@tensorflow/tfjs-backend-webgl/dist/tf-backend-webgl.es2017.js'; +import { setWasmPaths, version_wasm } from '@tensorflow/tfjs-backend-wasm/dist/index.js'; + +const version = { core: tf.version, cpu: tfCPU.version_cpu, webgl: tfWebGL.version_webgl, wasm: version_wasm }; + +export { tf, setWasmPaths, loadGraphModel, version }; +*/