xxxxxxxxxx
let video;
let poseNet;
let poses = [];
const NOSE = 0;
const LEFT_EYE = 1;
const RIGHT_EYE = 2;
const LEFT_EAR = 3;
const RIGHT_EAR = 4;
const LEFT_SHOULDER = 5;
const RIGHT_SHOULDER = 6;
const LEFT_WRIST = 9;
const RIGHT_WRIST = 10;
// The setup function is called once initially
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();
textSize(15);
ellipseMode(CENTER);
}
function modelReady() {
select('#status').html('Model Loaded');
}
// The draw function loops continually
function draw() {
image(video, 0, 0, width, height);
// We can call both functions to draw all keypoints and the skeletons
//drawKeypoints();
//drawSkeleton();
drawClownNose();
}
//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 if the pose probability is bigger than 0.2
if (keypoint.score > 0.2) {
fill(255, 0, 0);
noStroke();
ellipse(keypoint.position.x, keypoint.position.y, 10, 10);
text(j, keypoint.position.x+5, keypoint.position.y-5);
}
}
}
}
function drawClownNose() {
for (let i = 0; i < poses.length; i++) {
// For each pose detected, loop through all the keypoints
let pose = poses[i].pose;
let nosePoint = pose.keypoints[NOSE];
// Only take action if our confidence > 30%
if (nosePoint.score > 0.3) {
let noseX = nosePoint.position.x;
let noseY = nosePoint.position.y;
fill(0);
stroke(0);
strokeWeight(14);
line(noseX - 50, noseY+15, noseX + 50, noseY+15);
strokeWeight(10);
line(noseX - 50, noseY+15, noseX - 75, noseY - 10);
line(noseX + 50, noseY + 15, noseX + 75, noseY - 10);
rect(noseX - 50, noseY+10, 100, 10);
fill(255, 0, 0);
noStroke();
ellipse(noseX, noseY, 35, 35);
}
}
}
function drawLight() {
for (let i = 0; i < poses.length; i++) {
// For each pose detected, loop through all the keypoints
let pose = poses[i].pose;
let wristPoint = pose.keypoints[RIGHT_WRIST];
if (wristPoint.score > 0.2) {
let wristX = wristPoint.position.x;
let wristY = wristPoint.position.y;
fill(255, 255, 0);
// rect(wristX - 50, wristY+10, 100, 10);
// fill(255, 0, 0);
noStroke();
ellipse(wristX, wristY-75, 75, 75);
}
}
}
// 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, 0, 0);
line(partA.position.x, partA.position.y, partB.position.x, partB.position.y);
}
}
}