|
| 1 | +/* |
| 2 | + * 👋 Hello! This is an ml5.js example made and shared with ❤️. |
| 3 | + * Learn more about the ml5.js project: https://ml5js.org/ |
| 4 | + * ml5.js license and Code of Conduct: https://github.com/ml5js/ml5-next-gen/blob/main/LICENSE.md |
| 5 | + * |
| 6 | + * This example demonstrates drawing skeletal connections through ml5.handPose. |
| 7 | + */ |
| 8 | + |
| 9 | +let handPose; |
| 10 | +let video; |
| 11 | +let hands = []; |
| 12 | +let connections; |
| 13 | + |
| 14 | +function preload() { |
| 15 | + // Load the handPose model |
| 16 | + handPose = ml5.handPose(); |
| 17 | +} |
| 18 | + |
| 19 | +function setup() { |
| 20 | + createCanvas(640, 480); |
| 21 | + // Create the webcam video and hide it |
| 22 | + video = createCapture(VIDEO); |
| 23 | + video.size(640, 480); |
| 24 | + video.hide(); |
| 25 | + // start detecting hands from the webcam video |
| 26 | + handPose.detectStart(video, gotHands); |
| 27 | + // Get the skeletal connection information |
| 28 | + connections = handPose.getConnections(); |
| 29 | +} |
| 30 | + |
| 31 | +function draw() { |
| 32 | + // Draw the webcam video |
| 33 | + image(video, 0, 0, width, height); |
| 34 | + |
| 35 | + // Draw the skeletal connections |
| 36 | + for (let i = 0; i < hands.length; i++) { |
| 37 | + let hand = hands[i]; |
| 38 | + for (let j = 0; j < connections.length; j++) { |
| 39 | + let pointAIndex = connections[j][0]; |
| 40 | + let pointBIndex = connections[j][1]; |
| 41 | + let pointA = hand.keypoints[pointAIndex]; |
| 42 | + let pointB = hand.keypoints[pointBIndex]; |
| 43 | + stroke(255, 0, 0); |
| 44 | + strokeWeight(2); |
| 45 | + line(pointA.x, pointA.y, pointB.x, pointB.y); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Draw all the tracked hand points |
| 50 | + for (let i = 0; i < hands.length; i++) { |
| 51 | + let hand = hands[i]; |
| 52 | + for (let j = 0; j < hand.keypoints.length; j++) { |
| 53 | + let keypoint = hand.keypoints[j]; |
| 54 | + fill(0, 255, 0); |
| 55 | + noStroke(); |
| 56 | + circle(keypoint.x, keypoint.y, 10); |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// Callback function for when handPose outputs data |
| 62 | +function gotHands(results) { |
| 63 | + // save the output to the hands variable |
| 64 | + hands = results; |
| 65 | +} |
0 commit comments