xxxxxxxxxx
120
let video;
let poseNet;
let poses = [];
let skeletons = [];
let a = 0;
let fade = 1;
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();
}
function modelReady() {
// select('#status').html('Model Loaded');
console.log("model loaded");
}
function draw() {
push();
translate(width,0);
scale(-1,1)
image(video, 0, 0, width, height);
pop();
// We can call both functions to draw all keypoints and the skeletons
drawKeypoints();
drawSkeleton();
}
// 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
for (let j = 0; j < poses[i].pose.keypoints.length; j++) {
// A keypoint is an object describing a body part (like rightArm or leftShoulder)
let keypoint = poses[i].pose.keypoints[j];
// Only draw an ellipse is the pose probability is bigger than 0.2
if (keypoint.score > 0.6) {
stroke(0);
fill(0);
text(j, keypoint.position.x, keypoint.position.y - 15);
ellipse(keypoint.position.x, keypoint.position.y, 10, 10);
}
}
noFill();
noStroke();
beginShape();
// 3,1,2,4,0 => .5* dist(6&5) 6 ,5
// console.log(poses[i].pose.keypoints[0]);
if (poses[i].pose.keypoints[0].position.x) {
if (poses[i].pose.keypoints[0].score > 0.6) {
vertex(poses[i].pose.keypoints[0].position.x, poses[i].pose.keypoints[0].position.y)
}
}
if (poses[i].pose.keypoints[3].position.x) {
vertex(poses[i].pose.keypoints[3].position.x, poses[i].pose.keypoints[3].position.y)
}
if (poses[i].pose.keypoints[1].position.x) {
vertex(poses[i].pose.keypoints[1].position.x, poses[i].pose.keypoints[1].position.y)
}
if (poses[i].pose.keypoints[2].position.x) {
vertex(poses[i].pose.keypoints[2].position.x, poses[i].pose.keypoints[2].position.y)
}
if (poses[i].pose.keypoints[4].position.x) {
vertex(poses[i].pose.keypoints[4].position.x, poses[i].pose.keypoints[4].position.y)
}
vertex(poses[i].pose.keypoints[0].position.x, poses[i].pose.keypoints[0].position.y)
if (poses[i].pose.keypoints[6].position.x && poses[i].pose.keypoints[5].position.x) {
let dis = dist(poses[i].pose.keypoints[6].position.x, poses[i].pose.keypoints[6].position.y, poses[i].pose.keypoints[5].position.x, poses[i].pose.keypoints[5].position.y)
vertex(poses[i].pose.keypoints[6].position.x + (dis / 2), poses[i].pose.keypoints[6].position.y);
vertex(poses[i].pose.keypoints[6].position.x, poses[i].pose.keypoints[6].position.y);
vertex(poses[i].pose.keypoints[5].position.x, poses[i].pose.keypoints[5].position.y);
vertex(poses[i].pose.keypoints[5].position.x - (dis / 2), poses[i].pose.keypoints[5].position.y)
}
if (poses[i].pose.keypoints[11].position.x) {
vertex(poses[i].pose.keypoints[11].position.x, poses[i].pose.keypoints[11].position.y)
}
if (poses[i].pose.keypoints[12].position.x) {
vertex(poses[i].pose.keypoints[12].position.x, poses[i].pose.keypoints[12].position.y)
}
endShape();
}
}
// A function to draw the skeletons
function drawSkeleton() {
// Loop through all the skeletons detected
for (let i = 0; i < poses.length; i++) {
// For every skeleton, loop through all body connections
for (let j = 0; j < poses[i].skeleton.length; j++) {
let partA = poses[i].skeleton[j][0];
let partB = poses[i].skeleton[j][1];
stroke(255, 0, 0);
line(partA.position.x, partA.position.y, partB.position.x, partB.position.y);
}
}
}