11let img ;
22let poseNet ;
3- let poses = [ ] ;
43
5- function setup ( ) {
6- createCanvas ( 640 , 360 ) ;
7-
8- // create an image using the p5 dom library
9- // call modelReady() when it is loaded
10- img = createImg ( "data/runner.jpg" , imageReady ) ;
11- // set the image size to the size of the canvas
12- img . size ( width , height ) ;
13-
14- img . hide ( ) ; // hide the image in the browser
15- frameRate ( 1 ) ; // set the frameRate to 1 since we don't need it to be running quickly in this case
4+ function preload ( ) {
5+ // load an image for pose detection
6+ img = loadImage ( 'data/runner.jpg' ) ;
167}
178
18- // when the image is ready, then load up poseNet
19- function imageReady ( ) {
20- // set some options
21- const options = {
22- minConfidence : 0.1 ,
23- inputResolution : { width, height } ,
24- } ;
25-
26- // assign poseNet
27- poseNet = ml5 . poseNet ( modelReady , options ) ;
28- // This sets up an event that listens to 'pose' events
29- poseNet . on ( "pose" , function ( results ) {
30- poses = results ;
31- } ) ;
9+ function setup ( ) {
10+ createCanvas ( 640 , 360 ) ;
11+ image ( img , 0 , 0 ) ;
12+ poseNet = ml5 . poseNet ( modelReady ) ;
3213}
3314
3415// when poseNet is ready, do the detection
3516function modelReady ( ) {
36- select ( "#status" ) . html ( "Model Loaded" ) ;
37-
17+ select ( '#status' ) . html ( 'Model Loaded' ) ;
18+ // If/When a pose is detected, poseNet.on('pose', ...) will be listening for the detection results
19+ poseNet . on ( 'pose' , function ( poses ) {
20+ if ( poses . length > 0 ) {
21+ drawSkeleton ( poses ) ;
22+ drawKeypoints ( poses ) ;
23+ }
24+ } ) ;
3825 // When the model is ready, run the singlePose() function...
39- // If/When a pose is detected, poseNet.on('pose', ...) will be listening for the detection results
40- // in the draw() loop, if there are any poses, then carry out the draw commands
4126 poseNet . singlePose ( img ) ;
4227}
4328
44- // draw() will not show anything until poses are found
45- function draw ( ) {
46- if ( poses . length > 0 ) {
47- image ( img , 0 , 0 , width , height ) ;
48- drawSkeleton ( poses ) ;
49- drawKeypoints ( poses ) ;
50- noLoop ( ) ; // stop looping when the poses are estimated
51- }
52- }
53-
5429// The following comes from https://ml5js.org/docs/posenet-webcam
5530// A function to draw ellipses over the detected keypoints
56- function drawKeypoints ( ) {
31+ function drawKeypoints ( poses ) {
5732 // Loop through all the poses detected
58- for ( let i = 0 ; i < poses . length ; i += 1 ) {
33+ for ( let i = 0 ; i < poses . length ; i ++ ) {
5934 // For each pose detected, loop through all the keypoints
60- const pose = poses [ i ] . pose ;
61- for ( let j = 0 ; j < pose . keypoints . length ; j += 1 ) {
35+ let pose = poses [ i ] . pose ;
36+ for ( let j = 0 ; j < pose . keypoints . length ; j ++ ) {
6237 // A keypoint is an object describing a body part (like rightArm or leftShoulder)
63- const keypoint = pose . keypoints [ j ] ;
38+ let keypoint = pose . keypoints [ j ] ;
6439 // Only draw an ellipse is the pose probability is bigger than 0.2
6540 if ( keypoint . score > 0.2 ) {
6641 fill ( 255 ) ;
@@ -73,17 +48,17 @@ function drawKeypoints() {
7348}
7449
7550// A function to draw the skeletons
76- function drawSkeleton ( ) {
51+ function drawSkeleton ( poses ) {
7752 // Loop through all the skeletons detected
78- for ( let i = 0 ; i < poses . length ; i += 1 ) {
79- const skeleton = poses [ i ] . skeleton ;
53+ for ( let i = 0 ; i < poses . length ; i ++ ) {
54+ let skeleton = poses [ i ] . skeleton ;
8055 // For every skeleton, loop through all body connections
81- for ( let j = 0 ; j < skeleton . length ; j += 1 ) {
82- const partA = skeleton [ j ] [ 0 ] ;
83- const partB = skeleton [ j ] [ 1 ] ;
56+ for ( let j = 0 ; j < skeleton . length ; j ++ ) {
57+ let partA = skeleton [ j ] [ 0 ] ;
58+ let partB = skeleton [ j ] [ 1 ] ;
8459 stroke ( 255 ) ;
8560 strokeWeight ( 1 ) ;
8661 line ( partA . position . x , partA . position . y , partB . position . x , partB . position . y ) ;
8762 }
8863 }
89- }
64+ }
0 commit comments