@@ -4,43 +4,40 @@ let hands = [];
4
4
5
5
function setup ( ) {
6
6
createCanvas ( 640 , 480 ) ;
7
+
8
+ // Create the video and hide it
7
9
video = createCapture ( VIDEO ) ;
8
10
video . size ( width , height ) ;
11
+ video . hide ( ) ;
9
12
10
- const options = { } ;
11
- handpose = ml5 . handpose ( video , options , modelReady ) ;
12
-
13
- // This sets up an event that fills the global variable "predictions"
14
- // with an array every time new hand poses are detected
15
- handpose . on ( "hand" , ( results ) => {
16
- hands = results ;
17
- } ) ;
13
+ // Load the model and attach an event
14
+ handpose = ml5 . handpose ( video , modelReady ) ;
15
+ handpose . on ( "hand" , gotHands ) ;
16
+ }
18
17
19
- // Hide the video element, and just show the canvas
20
- video . hide ( ) ;
18
+ // Event for hand detection
19
+ function gotHands ( results ) {
20
+ // Always save the latest output from the model in global variable "hands"
21
+ hands = results ;
21
22
}
22
23
24
+ // Event for when model loaded
23
25
function modelReady ( ) {
24
26
console . log ( "Model ready!" ) ;
25
27
}
26
28
27
29
function draw ( ) {
30
+ // Draw the video
28
31
image ( video , 0 , 0 , width , height ) ;
29
32
30
- // We can call the drawKeypoints function to draw all keypoints
31
-
32
- drawKeypoints ( ) ;
33
- }
34
-
35
- // A function to draw ellipses over the detected keypoints
36
- function drawKeypoints ( ) {
37
- for ( let i = 0 ; i < hands . length ; i += 1 ) {
38
- const hand = hands [ i ] ;
39
- for ( let j = 0 ; j < hand . keypoints . length ; j += 1 ) {
40
- const keypoint = hand . keypoints [ j ] ;
33
+ // Draw all the tracked hand points
34
+ for ( let i = 0 ; i < hands . length ; i ++ ) {
35
+ let hand = hands [ i ] ;
36
+ for ( let j = 0 ; j < hand . keypoints . length ; j ++ ) {
37
+ let keypoint = hand . keypoints [ j ] ;
41
38
fill ( 0 , 255 , 0 ) ;
42
39
noStroke ( ) ;
43
- ellipse ( keypoint . x , keypoint . y , 10 , 10 ) ;
40
+ circle ( keypoint . x , keypoint . y , 10 ) ;
44
41
}
45
42
}
46
43
}
0 commit comments