-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BodySegmentation: Add example to demonstrate .data
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<!-- | ||
👋 Hello! This is an ml5.js example made and shared with ❤️. | ||
Learn more about the ml5.js project: https://ml5js.org/ | ||
ml5.js license and Code of Conduct: https://github.com/ml5js/ml5-next-gen/blob/main/LICENSE.md | ||
This example demonstrates segmenting a person by body parts with ml5.bodySegmentation. | ||
--> | ||
|
||
<!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>ml5.js bodySegmentation Parts Example</title> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.2/p5.min.js"></script> | ||
<script src="../../dist/ml5.js"></script> | ||
</head> | ||
<body> | ||
<script src="sketch.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* 👋 Hello! This is an ml5.js example made and shared with ❤️. | ||
* Learn more about the ml5.js project: https://ml5js.org/ | ||
* ml5.js license and Code of Conduct: https://github.com/ml5js/ml5-next-gen/blob/main/LICENSE.md | ||
* | ||
* This example demonstrates segmenting a person by body parts with ml5.bodySegmentation. | ||
*/ | ||
|
||
let bodySegmentation; | ||
let video; | ||
let segmentation; | ||
|
||
let options = { | ||
maskType: "parts", | ||
}; | ||
|
||
function preload() { | ||
bodySegmentation = ml5.bodySegmentation("BodyPix", options); | ||
} | ||
|
||
function setup() { | ||
createCanvas(640, 480); | ||
// Create the video | ||
video = createCapture(VIDEO); | ||
video.size(width, height); | ||
video.hide(); | ||
|
||
bodySegmentation.detectStart(video, gotResults); | ||
} | ||
|
||
function draw() { | ||
background(255); | ||
image(video, 0, 0); | ||
if (segmentation) { | ||
let gridSize = 10; | ||
for (let x=0; x < video.width; x += gridSize) { | ||
for (let y=0; y < video.height; y += gridSize) { | ||
if (segmentation.data[y * video.width + x] == bodySegmentation.TORSO_FRONT) { | ||
fill(255, 0, 0); | ||
noStroke(); | ||
circle(x, y, gridSize); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// callback function for body segmentation | ||
function gotResults(result) { | ||
segmentation = result; | ||
} |