From 0561b5abd1502d2f7619663ea44f2f866761f426 Mon Sep 17 00:00:00 2001 From: Linda Paiste Date: Sun, 6 Mar 2022 15:24:57 -0600 Subject: [PATCH] Don't call `this.predict()` unless there is a video. --- src/Facemesh/index.js | 23 +++++++++++++---------- src/Handpose/index.js | 21 +++++++++++---------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/Facemesh/index.js b/src/Facemesh/index.js index ee0c35c37..905c4bd0d 100644 --- a/src/Facemesh/index.js +++ b/src/Facemesh/index.js @@ -3,9 +3,6 @@ // This software is released under the MIT License. // https://opensource.org/licenses/MIT -/* eslint prefer-destructuring: ["error", {AssignmentExpression: {array: false}}] */ -/* eslint no-await-in-loop: "off" */ - /* * Facemesh: Facial landmark detection in the browser * Ported and integrated from all the hard work by: https://github.com/tensorflow/tfjs-models/tree/master/facemesh @@ -19,14 +16,17 @@ import callCallback from "../utils/callcallback"; class Facemesh extends EventEmitter { /** * Create Facemesh. - * @param {HTMLVideoElement} video - An HTMLVideoElement. - * @param {object} options - An object with options. - * @param {function} callback - A callback to be called when the model is ready. + * @param {HTMLVideoElement} [video] - An HTMLVideoElement. + * @param {object} [options] - An object with options. + * @param {function} [callback] - A callback to be called when the model is ready. */ constructor(video, options, callback) { super(); this.video = video; + /** + * @type {null | facemeshCore.FaceMesh} + */ this.model = null; this.modelReady = false; this.config = options; @@ -49,18 +49,21 @@ class Facemesh extends EventEmitter { }; }); } - - this.predict(); + if (this.video) { + this.predict(); + } return this; } /** - * Load the model and set it to this.model - * @return {this} the Facemesh model. + * @return {Promise} an array of predictions. */ async predict(inputOr, callback) { const input = this.getInput(inputOr); + if (!input) { + throw new Error("No input image found."); + } const { flipHorizontal } = this.config; const predictions = await this.model.estimateFaces(input, flipHorizontal); const result = predictions; diff --git a/src/Handpose/index.js b/src/Handpose/index.js index 341a08cf7..029b43d62 100644 --- a/src/Handpose/index.js +++ b/src/Handpose/index.js @@ -3,9 +3,6 @@ // This software is released under the MIT License. // https://opensource.org/licenses/MIT -/* eslint prefer-destructuring: ["error", {AssignmentExpression: {array: false}}] */ -/* eslint no-await-in-loop: "off" */ - /* * Handpose: Palm detector and hand-skeleton finger tracking in the browser * Ported and integrated from all the hard work by: https://github.com/tensorflow/tfjs-models/tree/master/handpose @@ -19,14 +16,17 @@ import callCallback from "../utils/callcallback"; class Handpose extends EventEmitter { /** * Create Handpose. - * @param {HTMLVideoElement} video - An HTMLVideoElement. - * @param {object} options - An object with options. - * @param {function} callback - A callback to be called when the model is ready. + * @param {HTMLVideoElement} [video] - An HTMLVideoElement. + * @param {object} [options] - An object with options. + * @param {function} [callback] - A callback to be called when the model is ready. */ constructor(video, options, callback) { super(); this.video = video; + /** + * @type {null|handposeCore.HandPose} + */ this.model = null; this.modelReady = false; this.config = options; @@ -50,19 +50,20 @@ class Handpose extends EventEmitter { }); } - this.predict(); + if (this.video) { + this.predict(); + } return this; } /** - * Load the model and set it to this.model - * @return {this} the Handpose model. + * @return {Promise} an array of predictions. */ async predict(inputOr, callback) { const input = this.getInput(inputOr); if (!input) { - return []; + throw new Error("No input image found."); } const { flipHorizontal } = this.config; const predictions = await this.model.estimateHands(input, flipHorizontal);