xxxxxxxxxx
77
let bodypix;
let segmentation;
let video;
function setup() {
createCanvas(320, 240);
// load up your video
video = createCapture(VIDEO);
video.size(width, height);
bodypix = ml5.bodyPix(video, modelReady)
}
function modelReady() {
console.log('ready!');
// Swap line 20 for line 19 to mask out entire background or body
// Look for what options are in the file options.js
//bodypix.segment(gotBody, options);
bodypix.segmentWithParts(gotParts, options);
}
function gotBody(err, result) {
if (err) {
console.log(err)
return
}
// console.log(result);
segmentation = result;
background(0);
// Mask the background
image(segmentation.maskBackground, 0, 0, width, height);
// Mask the body
//image(segmentation.maskPerson, 0, 0, width, height);
bodypix.segment(gotResults, options)
}
function gotParts(err, result) {
if (err) {
console.log(err)
return
}
// console.log(result);
segmentation = result;
// Color coded body parts
image(segmentation.image, 0, 0, width, height);
// Do color analysis
let segmented = segmentation.image;
// What body part do you want to compare?
let bodyPartColor = segmentation.bodyParts.leftFace.color;
// Loop through every pixel of the segmented image
// Compare its color to the color of the body part set in options.js
// If there's a match, draw the real RGB color of that pixel from the original video
for (let x = 0; x < width; x++) {
for (let y = 0; y < height; y++) {
let segmentedColor = segmented.get(x, y);
if (JSON.stringify(segmentedColor) == JSON.stringify(bodyPartColor)) {
// Get the corresponding color from the original video
let videoColor = video.get(x, y);
segmented.set(x, y, videoColor);
}
}
}
segmented.updatePixels();
// Draw the segmented image
image(segmented, 0, 0, width, height);
// Ask bodypix to segment the video feed again
bodypix.segmentWithParts(gotParts, options);
}