Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<body>
  <canvas id="myCanvas"></canvas>
  <script src="sketch.js"></script>
  
</body>

              
            
!

CSS

              
                body {
  padding: 0px;
  margin: 0px;
}

              
            
!

JS

              
                //from https://github.com/ml5js/ml5-examples/blob/master/p5js/PoseNet/PoseNet_webcam/sketch.js
let video;
let poseNet;
let poses = [];
let gStatus = "Loading model";

function setup() {
  createCanvas(640, 480);
  video = createCapture(VIDEO);
  video.size(width, height);
  // Create a new poseNet method with a single detection
  poseNet = ml5.poseNet(video, modelReady);
  // This sets up an event that fills the global variable "poses"
  // with an array every time new poses are detected
  poseNet.on("pose", function (results) {
    poses = results;
  });
  // Hide the video element, and just show the canvas
  video.hide();
  setInterval(toggleVideo, 3000);
}

function modelReady() {
  gStatus = "Model Loaded";
}

function drawEye(keypoint) {
  fill("white");
  circle(keypoint.position.x, keypoint.position.y, 40);
  fill("black");
  circle(keypoint.position.x, keypoint.position.y, 10);
}

// A function to draw ellipses over the detected keypoints
function drawKeypoints() {
  // Loop through all the poses detected
  for (let i = 0; i < poses.length; i++) {
    // For each pose detected, loop through all the keypoints
    let pose = poses[i].pose;
    for (let j = 0; j < pose.keypoints.length; j++) {
      // A keypoint is an object describing a body part (like rightArm or leftShoulder)
      let keypoint = pose.keypoints[j];
      // Only draw an ellipse is the pose probability is bigger than 0.2
      if (keypoint.score > 0.2) {
        //color by confidence (score)
        var score255 = map(keypoint.score, 0, 1, 0, 255);
        fill(0, score255, score255);
        noStroke();
        ellipse(keypoint.position.x, keypoint.position.y, 10, 10);
        fill("white");
        textSize(12);

        text(keypoint.part, keypoint.position.x, keypoint.position.y);
        if (keypoint.part == "leftEye" || keypoint.part === "rightEye") {
          drawEye(keypoint);
        }
      }
    }
  }
}

// A function to draw the skeletons
function drawSkeleton() {
  // Loop through all the skeletons detected
  for (let i = 0; i < poses.length; i++) {
    let skeleton = poses[i].skeleton;
    // For every skeleton, loop through all body connections
    for (let j = 0; j < skeleton.length; j++) {
      let partA = skeleton[j][0];
      let partB = skeleton[j][1];
      stroke(255, 105, 180);
      strokeWeight(5);
      line(
        partA.position.x,
        partA.position.y,
        partB.position.x,
        partB.position.y
      );
    }
  }
}
let shouldShowVideo = true;
function toggleVideo() {
  shouldShowVideo = !shouldShowVideo;
}
function draw() {
  background(0);
  if (shouldShowVideo) {
    image(video, 0, 0, width, height);
    text("showing original video", 30, height - 100);
  } else {
    text("hiding original video", 30, height - 100);
  }
  // We can call both functions to draw all keypoints and the skeletons
  drawKeypoints();
  drawSkeleton();
  noStroke();
  text(gStatus, 50, height - 120);
}

              
            
!
999px

Console