Skip to content

Commit

Permalink
Merge pull request #1321 from lindapaiste/fix/facemesh-missing-input
Browse files Browse the repository at this point in the history
Fix issues #1224 & #1213: Don't call `this.predict()` unless there is a video.
  • Loading branch information
joeyklee authored Apr 2, 2022
2 parents f93d898 + 0561b5a commit 2371388
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
23 changes: 13 additions & 10 deletions src/Facemesh/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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<facemeshCore.AnnotatedPrediction[]>} 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;
Expand Down
21 changes: 11 additions & 10 deletions src/Handpose/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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<handposeCore.AnnotatedPrediction[]>} 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);
Expand Down

0 comments on commit 2371388

Please sign in to comment.