Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions examples/BodySegmentation-maskbackground/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.7.0/p5.js"></script>
<script src="../../dist/ml5.js"></script>
</head>
<body>
<script src="sketch.js"></script>
</body>
</html>
48 changes: 48 additions & 0 deletions examples/BodySegmentation-maskbackground/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2020-2023 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

/* ===
ml5 Example
BodyPix
=== */

let bodypix;
let video;
let segmentation;

let options = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting we might want to include a "hello world" version of this that just uses defaults. The options can sometimes overwhelm / confuse a beginner.

outputStride: 16, //adjust the output stride and see which one works best!
multiSegmentation: false,
segmentBodyParts: true,
flipHorizontal: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this something that the BodyPix model does natively or something you all added? I wonder if we should include an option like this for other models as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Dan! If your question meant "why these four paramters", that's the latter three (multiSegmentation, segmentBodyParts, flipHorizontal) actually depends case by case. For example, if the user wants to detect multiple people and output different segmentation, then multiSegmentation needs to be turned on. As for outputStride, that's because I noticed this parameter could affect model performance, in terms of accuracy, so I thought this might be a good way to familiarize our users to machine learning.
I have set a default values for all these in index.js. We can create a default input for different cases like webcam or image for parameters like flipHorizontal and segmentBodyParts, but I think it might be a good idea to leave multiSegmentation (one segmentation for multiple people or multiple segmentation) to the user?
Lemme know if there's still something I didn't explain clearly or understand wrongly! Be happy to elaborate/correct :)

};

function setup() {
createCanvas(480, 360);
// Create the video
video = createCapture(VIDEO);
video.size(width, height);
video.hide();
bodypix = ml5.bodyPix(video, options, modelReady);
bodypix.on("bodypix", gotResults);
}

// Event for body segmentation
function gotResults(result) {
// Save the latest part mask from the model in global variable "segmentation"
segmentation = result;
//Draw the video
image(video, 0, 0, width, height);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For students to work from, in my experience it's best to have the drawing in draw() and use a if as @gohai suggests. Things go haywire quickly when students try to build on the code when there is drawing in a separate callback.

image(segmentation.backgroundMask, 0, 0, width, height);
tint(255, 128); // opacity tuning
}

// Event when model is loaded
function modelReady() {
console.log("Model ready!");
}

function draw() {
}
14 changes: 14 additions & 0 deletions examples/BodySegmentation-maskbodyparts/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.7.0/p5.js"></script>
<script src="../../dist/ml5.js"></script>
</head>
<body>
<script src="sketch.js"></script>
</body>
</html>
48 changes: 48 additions & 0 deletions examples/BodySegmentation-maskbodyparts/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2020-2023 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

/* ===
ml5 Example
BodyPix
=== */

let bodypix;
let video;
let segmentation;

let options = {
outputStride: 16, //adjust the output stride and see which one works best!
multiSegmentation: false,
segmentBodyParts: true,
flipHorizontal: true,
};

function setup() {
createCanvas(480, 360);
// Create the video
video = createCapture(VIDEO);
video.size(width, height);
video.hide();
bodypix = ml5.bodyPix(video, options, modelReady);
bodypix.on("bodypix", gotResults);
}

// Event for body segmentation
function gotResults(result) {
// Save the latest part mask from the model in global variable "segmentation"
segmentation = result;
//Draw the video
image(video, 0, 0, width, height);
image(segmentation.partMask, 0, 0, width, height);
tint(255, 128); //opacity tuning
}

// Event when model is loaded
function modelReady() {
console.log("Model ready!");
}

function draw() {
}
14 changes: 14 additions & 0 deletions examples/BodySegmentation-maskperson/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.7.0/p5.js"></script>
<script src="../../dist/ml5.js"></script>
</head>
<body>
<script src="sketch.js"></script>
</body>
</html>
48 changes: 48 additions & 0 deletions examples/BodySegmentation-maskperson/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2020-2023 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

/* ===
ml5 Example
BodyPix
=== */

let bodypix;
let video;
let segmentation;

let options = {
outputStride: 16, //adjust the output stride and see which one works best!
multiSegmentation: false,
segmentBodyParts: true,
flipHorizontal: true,
};

function setup() {
createCanvas(480, 360);
// Create the video
video = createCapture(VIDEO);
video.size(width, height);
video.hide();
bodypix = ml5.bodyPix(video, options, modelReady);
bodypix.on("bodypix", gotResults);
}

// Event for body segmentation
function gotResults(result) {
// Save the latest part mask from the model in global variable "segmentation"
segmentation = result;
//Draw the video
image(video, 0, 0, width, height);
image(segmentation.personMask, 0, 0, width, height);
tint(255, 128); //opacity tuning
}

// Event when model is loaded
function modelReady() {
console.log("Model ready!");
}

function draw() {
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"dependencies": {
"@mediapipe/hands": "^0.4.1675469240",
"@mediapipe/pose": "^0.5.1675469404",
"@mediapipe/selfie_segmentation": "~0.1.0",
"@tensorflow-models/body-segmentation": "^1.0.1",
"@tensorflow-models/hand-pose-detection": "^2.0.0",
"@tensorflow-models/pose-detection": "^2.1.0",
"@tensorflow/tfjs": "^4.2.0",
Expand Down
98 changes: 98 additions & 0 deletions src/BodySegmentation/BODYPIX_PALETTE.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
export default {
leftFace: {
id: 0,
color: [110, 64, 170],
},
rightFace: {
id: 1,
color: [143, 61, 178],
},
leftUpperArmFront: {
id: 2,
color: [178, 60, 178],
},
leftUpperArmBack: {
id: 3,
color: [210, 62, 167],
},
rightUpperArmFront: {
id: 4,
color: [238, 67, 149],
},
rightUpperArmBack: {
id: 5,
color: [255, 78, 125],
},
leftLowerArmFront: {
id: 6,
color: [255, 94, 99],
},
leftLowerArmBack: {
id: 7,
color: [255, 115, 75],
},
rightLowerArmFront: {
id: 8,
color: [255, 140, 56],
},
rightLowerArmBack: {
id: 9,
color: [239, 167, 47],
},
leftHand: {
id: 10,
color: [217, 194, 49],
},
rightHand: {
id: 11,
color: [194, 219, 64],
},
torsoFront: {
id: 12,
color: [175, 240, 91],
},
torsoBack: {
id: 13,
color: [135, 245, 87],
},
leftUpperLegFront: {
id: 14,
color: [96, 247, 96],
},
leftUpperLegBack: {
id: 15,
color: [64, 243, 115],
},
rightUpperLegFront: {
id: 16,
color: [40, 234, 141],
},
rightUpperLegBack: {
id: 17,
color: [28, 219, 169],
},
leftLowerLegFront: {
id: 18,
color: [26, 199, 194],
},
leftLowerLegBack: {
id: 19,
color: [33, 176, 213],
},
rightLowerLegFront: {
id: 20,
color: [47, 150, 224],
},
rightLowerLegBack: {
id: 21,
color: [65, 125, 224],
},
leftFoot: {
id: 22,
color: [84, 101, 214],
},
rightFoot: {
id: 23,
color: [99, 81, 195],
},
};
Loading