@@ -14,42 +14,41 @@ let poses = [];
14
14
15
15
function setup ( ) {
16
16
createCanvas ( 640 , 480 ) ;
17
+
18
+ // Create the video and hide it
17
19
video = createCapture ( VIDEO ) ;
18
20
video . size ( width , height ) ;
19
-
20
- // Create a new poseNet method with a single detection
21
- poseNet = ml5 . poseDetection ( video , modelReady ) ;
22
- // This sets up an event that fills the global variable "poses"
23
- // with an array every time new poses are detected
24
- poseNet . on ( "pose" , function ( results ) {
25
- poses = results ;
26
- } ) ;
27
- // Hide the video element, and just show the canvas
28
21
video . hide ( ) ;
22
+
23
+ // Load the model and attach an event
24
+ poseDetector = ml5 . poseDetection ( video , modelReady ) ;
25
+ poseDetector . on ( "pose" , gotPoses ) ;
29
26
}
30
27
28
+ // Event for pose detection
29
+ function gotPoses ( results ) {
30
+ // Always save the latest output from the model in global variable "poses"
31
+ poses = results ;
32
+ }
33
+
34
+ // Event for when model loaded
31
35
function modelReady ( ) {
32
- console . log ( "Model Loaded !" ) ;
36
+ console . log ( "Model ready !" ) ;
33
37
}
34
38
35
39
function draw ( ) {
40
+ console . log ( poses ) ;
41
+ // Draw the video
36
42
image ( video , 0 , 0 , width , height ) ;
37
- //console.log(poses);
38
- // We can call both functions to draw all keypoints and the skeletons
39
- drawKeypoints ( ) ;
40
- //drawSkeleton();
41
- }
42
43
43
- // A function to draw ellipses over the detected keypoints
44
- function drawKeypoints ( ) {
45
- // Loop through all the poses detected
44
+ // Draw all the tracked landmark points
45
+ // for each individual pose detected
46
46
for ( let i = 0 ; i < poses . length ; i ++ ) {
47
- // For each pose detected, loop through all the keypoints
48
47
let pose = poses [ i ] ;
48
+ // for each keypoint in the pose
49
49
for ( let j = 0 ; j < pose . keypoints . length ; j ++ ) {
50
- // A keypoint is an object describing a body part (like rightArm or leftShoulder)
51
50
let keypoint = pose . keypoints [ j ] ;
52
- // Only draw an ellipse is the pose probability is bigger than 0.2
51
+ // Only draw an ellipse if the confidence score of the keypoint is bigger than 0.2
53
52
if ( keypoint . score > 0.2 ) {
54
53
fill ( 255 , 0 , 0 ) ;
55
54
noStroke ( ) ;
@@ -58,23 +57,3 @@ function drawKeypoints() {
58
57
}
59
58
}
60
59
}
61
-
62
- // A function to draw the skeletons
63
- function drawSkeleton ( ) {
64
- // Loop through all the skeletons detected
65
- for ( let i = 0 ; i < poses . length ; i ++ ) {
66
- let skeleton = poses [ i ] . skeleton ;
67
- // For every skeleton, loop through all body connections
68
- for ( let j = 0 ; j < skeleton . length ; j ++ ) {
69
- let partA = skeleton [ j ] [ 0 ] ;
70
- let partB = skeleton [ j ] [ 1 ] ;
71
- stroke ( 255 , 0 , 0 ) ;
72
- line (
73
- partA . position . x ,
74
- partA . position . y ,
75
- partB . position . x ,
76
- partB . position . y
77
- ) ;
78
- }
79
- }
80
- }
0 commit comments