-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
169 additions
and
75 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
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
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
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 |
---|---|---|
@@ -1,61 +1,50 @@ | ||
console.log('ml5 version:', ml5.version); | ||
// Copyright (c) 2018-2023 ml5 | ||
// | ||
// This software is released under the MIT License. | ||
// https://opensource.org/licenses/MIT | ||
|
||
let gui; | ||
let params = { | ||
modelType: "MoveNet", | ||
skeleton: false, | ||
PointColor: [0, 255, 0, 255], | ||
}; | ||
|
||
let cam; | ||
let poseNet; | ||
let video; | ||
let bodyPose; | ||
let poses = []; | ||
|
||
function preload() { | ||
// Load the bodyPose model | ||
bodyPose = ml5.bodyPose(); | ||
} | ||
|
||
function setup() { | ||
createCanvas(640, 480); | ||
background(0); | ||
// webcam | ||
cam = createCapture(VIDEO); | ||
cam.size(640, 480); | ||
cam.hide(); | ||
// poseNet | ||
poseNet = ml5.poseNet(cam, modelReady); | ||
poseNet.on("pose", gotResults); | ||
// gui | ||
gui = new dat.GUI(); | ||
gui.add(params, "modelType"); | ||
gui.addColor(params, "PointColor"); | ||
|
||
// Create the video and hide it | ||
video = createCapture(VIDEO); | ||
video.size(width, height); | ||
video.hide(); | ||
|
||
// Start detecting poses in the webcam video | ||
bodyPose.detectStart(video, gotPoses); | ||
} | ||
|
||
function draw() { | ||
background(0); | ||
image(cam, 0, 0); | ||
// Draw the webcam video | ||
image(video, 0, 0, width, height); | ||
|
||
noStroke(); | ||
// Draw all the tracked landmark points | ||
for (let i = 0; i < poses.length; i++) { | ||
for (let k = 0; k < poses[i].pose.keypoints.length; k++) { | ||
// console.log(poses[i].pose.keypoints[k]); | ||
let point = poses[i].pose.keypoints[k]; | ||
|
||
let x = point.position.x; | ||
let y = point.position.y; | ||
let score = point.score; | ||
let partName = point.part; | ||
|
||
fill(0, 255, 0); | ||
ellipse(x, y, 5, 5); | ||
|
||
text(partName, x + 15, y + 5); | ||
text(score.toFixed(2), x + 15, y + 20); | ||
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed | ||
let pose = poses[i]; | ||
for (let j = 0; j < pose.keypoints.length; j++) { | ||
let keypoint = pose.keypoints[j]; | ||
// Only draw a circle if the keypoint's confidence is bigger than 0.1 | ||
if (keypoint.score > 0.1) { | ||
fill(0, 255, 0); | ||
noStroke(); | ||
circle(keypoint.x, keypoint.y, 10); | ||
} | ||
} | ||
} | ||
} | ||
|
||
function modelReady() { | ||
console.log("Model Loaded: PoseNet"); | ||
} | ||
|
||
function gotResults(newPoses) { | ||
poses = newPoses; | ||
// Callback function for when bodyPose outputs data | ||
function gotPoses(results) { | ||
// Save the output to the poses variable | ||
poses = results; | ||
} |