Skip to content

Commit 2371388

Browse files
authored
Merge pull request #1321 from lindapaiste/fix/facemesh-missing-input
Fix issues #1224 & #1213: Don't call `this.predict()` unless there is a video.
2 parents f93d898 + 0561b5a commit 2371388

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

src/Facemesh/index.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
// This software is released under the MIT License.
44
// https://opensource.org/licenses/MIT
55

6-
/* eslint prefer-destructuring: ["error", {AssignmentExpression: {array: false}}] */
7-
/* eslint no-await-in-loop: "off" */
8-
96
/*
107
* Facemesh: Facial landmark detection in the browser
118
* 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";
1916
class Facemesh extends EventEmitter {
2017
/**
2118
* Create Facemesh.
22-
* @param {HTMLVideoElement} video - An HTMLVideoElement.
23-
* @param {object} options - An object with options.
24-
* @param {function} callback - A callback to be called when the model is ready.
19+
* @param {HTMLVideoElement} [video] - An HTMLVideoElement.
20+
* @param {object} [options] - An object with options.
21+
* @param {function} [callback] - A callback to be called when the model is ready.
2522
*/
2623
constructor(video, options, callback) {
2724
super();
2825

2926
this.video = video;
27+
/**
28+
* @type {null | facemeshCore.FaceMesh}
29+
*/
3030
this.model = null;
3131
this.modelReady = false;
3232
this.config = options;
@@ -49,18 +49,21 @@ class Facemesh extends EventEmitter {
4949
};
5050
});
5151
}
52-
53-
this.predict();
52+
if (this.video) {
53+
this.predict();
54+
}
5455

5556
return this;
5657
}
5758

5859
/**
59-
* Load the model and set it to this.model
60-
* @return {this} the Facemesh model.
60+
* @return {Promise<facemeshCore.AnnotatedPrediction[]>} an array of predictions.
6161
*/
6262
async predict(inputOr, callback) {
6363
const input = this.getInput(inputOr);
64+
if (!input) {
65+
throw new Error("No input image found.");
66+
}
6467
const { flipHorizontal } = this.config;
6568
const predictions = await this.model.estimateFaces(input, flipHorizontal);
6669
const result = predictions;

src/Handpose/index.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
// This software is released under the MIT License.
44
// https://opensource.org/licenses/MIT
55

6-
/* eslint prefer-destructuring: ["error", {AssignmentExpression: {array: false}}] */
7-
/* eslint no-await-in-loop: "off" */
8-
96
/*
107
* Handpose: Palm detector and hand-skeleton finger tracking in the browser
118
* 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";
1916
class Handpose extends EventEmitter {
2017
/**
2118
* Create Handpose.
22-
* @param {HTMLVideoElement} video - An HTMLVideoElement.
23-
* @param {object} options - An object with options.
24-
* @param {function} callback - A callback to be called when the model is ready.
19+
* @param {HTMLVideoElement} [video] - An HTMLVideoElement.
20+
* @param {object} [options] - An object with options.
21+
* @param {function} [callback] - A callback to be called when the model is ready.
2522
*/
2623
constructor(video, options, callback) {
2724
super();
2825

2926
this.video = video;
27+
/**
28+
* @type {null|handposeCore.HandPose}
29+
*/
3030
this.model = null;
3131
this.modelReady = false;
3232
this.config = options;
@@ -50,19 +50,20 @@ class Handpose extends EventEmitter {
5050
});
5151
}
5252

53-
this.predict();
53+
if (this.video) {
54+
this.predict();
55+
}
5456

5557
return this;
5658
}
5759

5860
/**
59-
* Load the model and set it to this.model
60-
* @return {this} the Handpose model.
61+
* @return {Promise<handposeCore.AnnotatedPrediction[]>} an array of predictions.
6162
*/
6263
async predict(inputOr, callback) {
6364
const input = this.getInput(inputOr);
6465
if (!input) {
65-
return [];
66+
throw new Error("No input image found.");
6667
}
6768
const { flipHorizontal } = this.config;
6869
const predictions = await this.model.estimateHands(input, flipHorizontal);

0 commit comments

Comments
 (0)