xxxxxxxxxx
92
let bodypix;
let video;
let segmentation;
const options = {
outputStride: 16, // 8, 16, or 32, default is 16
segmentationThreshold: 0.5, // 0 - 1, defaults to 0.5
};
function preload() {
bodypix = ml5.bodyPix(options);
}
function setup() {
createCanvas(320, 240);
// load up your video
video = createCapture(VIDEO);
// segment();
bodypix.segment(video, function (result) {
segmentation = result;
});
video.hide();
}
function segment() {
// bodypix.segment(video, gotResults);
bodypix.segmentWithParts(video, gotResults, options)
}
function gotResults(err, result) {
if (err) {
console.log(err);
return;
}
segmentation = result;
// background(0);
// image(segmentation.backgroundMask, 0, 0, width, height);
segment();
}
function draw() {
// image(video, 0, 0, width, height);
// 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 < segmentation.length; i++) {
// For each pose detected, loop through all the keypoints
for (let j = 0; j < segmentation[i].pose.keypoints.length; j++) {
// A keypoint is an object describing a body part (like rightArm or leftShoulder)
let keypoint = segmentation[i].pose.keypoints[j];
console.log(keypoint);
// Only draw an ellipse is 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);
}
}
}
}
*/
/*
// A function to draw the skeletons
function drawSkeleton() {
// Loop through all the skeletons detected
for (let i = 0; i < segmentation.length; i++) {
// For every skeleton, loop through all body connections
for (let j = 0; j < segmentation[i].skeleton.length; j++) {
let partA = segmentation[i].skeleton[j][0];
let partB = segmentation[i].skeleton[j][1];
stroke(255, 0, 0);
line(partA.position.x, partA.position.y, partB.position.x, partB.position.y);
}
}
}
*/