Skip to content

Commit 308b786

Browse files
authored
HandPose connections (#226)
* implement getConnections * add example * rename example
1 parent 9a4dd7e commit 308b786

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
<!DOCTYPE html>
10+
<html lang="en">
11+
<head>
12+
<meta charset="UTF-8" />
13+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
14+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
15+
<title>ml5.js handPose Skeletal Connection Example</title>
16+
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.9.4/p5.min.js"></script>
17+
<script src="../../dist/ml5.js"></script>
18+
</head>
19+
<body>
20+
<script src="sketch.js"></script>
21+
</body>
22+
</html>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
}

src/HandPose/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import * as tf from "@tensorflow/tfjs";
2222
import * as handPoseDetection from "@tensorflow-models/hand-pose-detection";
23+
import { MEDIAPIPE_CONNECTED_KEYPOINTS_PAIRS } from "@tensorflow-models/hand-pose-detection/dist/constants";
2324
import callCallback from "../utils/callcallback";
2425
import handleArguments from "../utils/handleArguments";
2526
import handleOptions from "../utils/handleOptions";
@@ -292,6 +293,15 @@ class HandPose {
292293
return hand;
293294
});
294295
}
296+
297+
/**
298+
* Returns the pairs of keypoint indices that are connected by a skeletal representation.
299+
* @returns {number[][]} The connected keypoint pairs.
300+
* @public
301+
*/
302+
getConnections() {
303+
return MEDIAPIPE_CONNECTED_KEYPOINTS_PAIRS;
304+
}
295305
}
296306

297307
/**

0 commit comments

Comments
 (0)