Skip to content

Commit cb4118f

Browse files
authored
Added bodySegmentation.getPartsId() (#219)
* Added `getPartsId()` & modified example * Fixed styles & removed selfie segmentation constants
1 parent 9e2f47c commit cb4118f

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

examples/bodySegmentation-select-body-parts/sketch.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ function draw() {
3232
background(255);
3333
image(video, 0, 0);
3434
if (segmentation) {
35+
let parts = bodySegmentation.getPartsId();
3536
let gridSize = 10;
3637
for (let x = 0; x < video.width; x += gridSize) {
3738
for (let y = 0; y < video.height; y += gridSize) {
38-
if (
39-
segmentation.data[y * video.width + x] == bodySegmentation.TORSO_FRONT
40-
) {
39+
if (segmentation.data[y * video.width + x] == parts.TORSO_FRONT) {
4140
fill(255, 0, 0);
4241
noStroke();
4342
circle(x, y, gridSize);

src/BodySegmentation/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,14 @@ class BodySegmentation {
131131
"bodySegmentation"
132132
);
133133

134-
// add body part constants to the instance variable
135-
for (let key in BODYPIX_PALETTE) {
136-
this[key] = BODYPIX_PALETTE[key].id;
137-
}
134+
// Add function to return parts' key and id
135+
this.getPartsId = () => {
136+
let parts = {};
137+
for (let key in BODYPIX_PALETTE) {
138+
parts[key] = BODYPIX_PALETTE[key].id;
139+
}
140+
return parts;
141+
};
138142
} else {
139143
pipeline = tfBodySegmentation.SupportedModels.MediaPipeSelfieSegmentation;
140144
modelConfig = handleOptions(
@@ -180,10 +184,6 @@ class BodySegmentation {
180184
},
181185
"bodySegmentation"
182186
);
183-
184-
// add constants to the instance variable
185-
this.BACKGROUND = 0;
186-
this.PERSON = 255;
187187
}
188188

189189
await tf.ready();

0 commit comments

Comments
 (0)