Skip to content

Commit 6d2ce6e

Browse files
authored
Refactor HandPose examples (#38)
* refactor handpose examples * simplify parts example
1 parent 00558fe commit 6d2ce6e

File tree

9 files changed

+71
-0
lines changed

9 files changed

+71
-0
lines changed
File renamed without changes.
File renamed without changes.

examples/Handpose-parts/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>ml5.js Handpose p5 Webcam Example</title>
8+
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.js"></script>
9+
<script src="../../dist/ml5.js"></script>
10+
</head>
11+
<body>
12+
<script src="sketch.js"></script>
13+
</body>
14+
</html>

examples/Handpose-parts/sketch.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2023 ml5
2+
//
3+
// This software is released under the MIT License.
4+
// https://opensource.org/licenses/MIT
5+
6+
let handpose;
7+
let video;
8+
let hands = [];
9+
10+
// A variable to track a pinch between thumb and index
11+
let pinch = 0;
12+
13+
function preload() {
14+
// Load the handpose model.
15+
handpose = ml5.handpose();
16+
}
17+
18+
function setup() {
19+
createCanvas(640, 480);
20+
// Create the webcam video and hide it
21+
video = createCapture(VIDEO);
22+
video.size(width, height);
23+
video.hide();
24+
// start detecting hands from the webcam video
25+
handpose.detectStart(video, gotHands);
26+
}
27+
28+
function draw() {
29+
// Draw the webcam video
30+
image(video, 0, 0, width, height);
31+
32+
// If there is at least one hand
33+
if (hands.length > 0) {
34+
// Find the index finger tip and thumb tip
35+
let finger = hands[0].index_finger_tip;
36+
let thumb = hands[0].thumb_tip;
37+
38+
// Draw circles at finger positions
39+
let centerX = (finger.x + thumb.x) / 2;
40+
let centerY = (finger.y + thumb.y) / 2;
41+
// Calculate the pinch "distance" between finger and thumb
42+
let pinch = dist(finger.x, finger.y, thumb.x, thumb.y);
43+
44+
// This circle's size is controlled by a "pinch" gesture
45+
fill(0, 255, 0, 200);
46+
stroke(0);
47+
strokeWeight(2);
48+
circle(centerX, centerY, pinch);
49+
}
50+
}
51+
52+
// Callback function for when handpose outputs data
53+
function gotHands(results) {
54+
// save the output to the hands variable
55+
hands = results;
56+
console.log(hands);
57+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)