xxxxxxxxxx
85
// ml5.js Body Pose Detection Example
// Based on https://thecodingtrain.com/tracks/ml5js-beginners-guide/ml5/7-bodypose/pose-detection
let img;
let bodyPose;
let connections;
let poses = [];
function preload() {
// Initialize MoveNet model for body pose detection
//bodyPose = ml5.bodyPose("MoveNet", { flipped: true });
bodyPose = ml5.bodyPose("BlazePose", { flipped: true });
// Load an image to analyze
// img = loadImage("embarrassing.jpg");
}
function setup() {
// Create canvas matching the image dimensions
// createCanvas(img.width, img.height);
createCanvas(640, 480);
video = createCapture(VIDEO, { flipped: true });
video.hide();
// Start detecting poses in the loaded image
bodyPose.detectStart(video, gotPoses);
// Retrieve the skeleton structure used by the model
connections = bodyPose.getSkeleton();
console.log(connections);
}
function gotPoses(results) {
// Store detected poses in the global array
poses = results;
}
function mousePressed() {
console.log(poses);
}
function draw() {
// Display the image as the background
image(video, 0, 0);
if (poses.length > 0) {
let pose = poses[0];
let x = pose.nose.x;
let y = pose.nose.y;
fill(255, 0, 0);
circle(x, y, 20);
for (let i = 0; i < pose.keypoints.length; i++) {
let keypoint = pose.keypoints[i];
fill(0, 0, 255);
noStroke();
// Only draw keypoints with sufficient confidence
if (keypoint.confidence > 0.1) {
circle(keypoint.x, keypoint.y, 8);
}
}
for (let i = 0; i < connections.length; i++) {
let connection = connections[i];
let a = connection[0];
let b = connection[1];
let keyPointA = pose.keypoints[a];
let keyPointB = pose.keypoints[b];
stroke(0, 255, 0);
strokeWeight(8);
if (keyPointA.confidence > 0.1 & keyPointB.confidence > 0.1) {
line(keyPointA.x, keyPointA.y, keyPointB.x, keyPointB.y);
}
}
let rx = pose.right_wrist.x;
let ry = pose.right_wrist.y;
let lx = pose.left_wrist.x;
let ly = pose.left_wrist.y;
noStroke();
fill(0,125,255);
circle(rx, ry, 46);
fill(0,255,125);
circle(lx, ly, 46);
}
}