5
5
6
6
/*
7
7
* Handpose: Palm detector and hand-skeleton finger tracking in the browser
8
- * Ported and integrated from all the hard work by: https://github.com/tensorflow/tfjs-models/tree/master/handpose
8
+ * Ported and integrated from all the hard work by: https://github.com/tensorflow/tfjs-models/tree/master/hand-pose-detection
9
9
*/
10
10
import * as tf from "@tensorflow/tfjs" ;
11
11
import * as handPoseDetection from "@tensorflow-models/hand-pose-detection" ;
@@ -18,16 +18,13 @@ class Handpose extends EventEmitter {
18
18
/**
19
19
* Create Handpose.
20
20
* @param {HTMLVideoElement } [video] - An HTMLVideoElement.
21
- * @param {object } [options] - An object with options.
21
+ * @param {Object } [options] - An object with options.
22
22
* @param {function } [callback] - A callback to be called when the model is ready.
23
23
*/
24
24
constructor ( video , options , callback ) {
25
25
super ( ) ;
26
26
27
27
this . video = video ;
28
- /**
29
- * @type {null|handposeCore.HandPose }
30
- */
31
28
this . model = null ;
32
29
this . modelReady = false ;
33
30
this . config = options ;
@@ -40,17 +37,15 @@ class Handpose extends EventEmitter {
40
37
* @return {this } the Handpose model.
41
38
*/
42
39
async loadModel ( ) {
43
- const mediaPipeHands = handPoseDetection . SupportedModels . MediaPipeHands ;
40
+ const pipeline = handPoseDetection . SupportedModels . MediaPipeHands ;
44
41
const modelConfig = {
42
+ runtime : "mediapipe" , // use MediaPipe runtime by default
43
+ solutionPath : "https://cdn.jsdelivr.net/npm/@mediapipe/hands" , // fetch model from mediapipe server
45
44
...this . config ,
46
- runtime : "mediapipe" ,
47
- solutionPath : "https://cdn.jsdelivr.net/npm/@mediapipe/hands" ,
48
45
} ;
49
46
50
- this . model = await handPoseDetection . createDetector (
51
- mediaPipeHands ,
52
- modelConfig
53
- ) ;
47
+ this . model = await handPoseDetection . createDetector ( pipeline , modelConfig ) ;
48
+
54
49
this . modelReady = true ;
55
50
56
51
if ( this . video ) {
@@ -61,6 +56,8 @@ class Handpose extends EventEmitter {
61
56
}
62
57
63
58
/**
59
+ * @param {* } [inputOr] - An HMTL or p5.js image, video, or canvas element to run the prediction on.
60
+ * @param {function } [cb] - A callback function to handle the predictions.
64
61
* @return {Promise<handposeCore.AnnotatedPrediction[]> } an array of predictions.
65
62
*/
66
63
async predict ( inputOr , cb ) {
@@ -72,9 +69,7 @@ class Handpose extends EventEmitter {
72
69
const { flipHorizontal } = this . config ;
73
70
const predictions = await this . model . estimateHands ( image , flipHorizontal ) ;
74
71
const result = predictions ;
75
- // Soon, we will remove the 'predict' event and prefer the 'hand' event. During
76
- // the interim period, we will both events.
77
- this . emit ( "predict" , result ) ;
72
+
78
73
this . emit ( "hand" , result ) ;
79
74
80
75
if ( this . video ) {
@@ -89,6 +84,10 @@ class Handpose extends EventEmitter {
89
84
}
90
85
}
91
86
87
+ /**
88
+ * exposes handpose class through function
89
+ * @returns {Object|Promise<Boolean> } A new handpose instance
90
+ */
92
91
const handpose = ( ...inputs ) => {
93
92
const { video, options = { } , callback } = handleArguments ( ...inputs ) ;
94
93
const instance = new Handpose ( video , options , callback ) ;
0 commit comments