Skip to content

Commit dc2fa18

Browse files
committed
simplify handpose example
1 parent f875305 commit dc2fa18

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

examples/Handpose/script.js

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,40 @@ let hands = [];
44

55
function setup() {
66
createCanvas(640, 480);
7+
8+
// Create the video and hide it
79
video = createCapture(VIDEO);
810
video.size(width, height);
11+
video.hide();
912

10-
const options = {};
11-
handpose = ml5.handpose(video, options, modelReady);
12-
13-
// This sets up an event that fills the global variable "predictions"
14-
// with an array every time new hand poses are detected
15-
handpose.on("hand", (results) => {
16-
hands = results;
17-
});
13+
// Load the model and attach an event
14+
handpose = ml5.handpose(video, modelReady);
15+
handpose.on("hand", gotHands);
16+
}
1817

19-
// Hide the video element, and just show the canvas
20-
video.hide();
18+
// Event for hand detection
19+
function gotHands(results) {
20+
// Always save the latest output from the model in global variable "hands"
21+
hands = results;
2122
}
2223

24+
// Event for when model loaded
2325
function modelReady() {
2426
console.log("Model ready!");
2527
}
2628

2729
function draw() {
30+
// Draw the video
2831
image(video, 0, 0, width, height);
2932

30-
// We can call the drawKeypoints function to draw all keypoints
31-
32-
drawKeypoints();
33-
}
34-
35-
// A function to draw ellipses over the detected keypoints
36-
function drawKeypoints() {
37-
for (let i = 0; i < hands.length; i += 1) {
38-
const hand = hands[i];
39-
for (let j = 0; j < hand.keypoints.length; j += 1) {
40-
const keypoint = hand.keypoints[j];
33+
// Draw all the tracked hand points
34+
for (let i = 0; i < hands.length; i++) {
35+
let hand = hands[i];
36+
for (let j = 0; j < hand.keypoints.length; j++) {
37+
let keypoint = hand.keypoints[j];
4138
fill(0, 255, 0);
4239
noStroke();
43-
ellipse(keypoint.x, keypoint.y, 10, 10);
40+
circle(keypoint.x, keypoint.y, 10);
4441
}
4542
}
4643
}

0 commit comments

Comments
 (0)