-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (66 loc) · 2.06 KB
/
Copy pathindex.js
File metadata and controls
78 lines (66 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { HandLandmarker, FilesetResolver } from "@mediapipe/tasks-vision";
let handLandmarker;
const video = document.createElement("video");
async function initializeHandLandmarker() {
const vision = await FilesetResolver.forVisionTasks("/path/to/wasm/files");
handLandmarker = await HandLandmarker.createFromOptions(vision, {
baseOptions: { modelAssetPath: "/path/to/hand_landmarker.task" },
runningMode: "VIDEO",
numHands: 2
});
video.srcObject = await navigator.mediaDevices.getUserMedia({ video: true });
video.play();
}
async function getFingerPosition(args) {
const hands = await handLandmarker.detectForVideo(video, Date.now());
if (hands.length > 0) {
const hand = hands[0];
const landmarks = hand.landmarks;
const fingerIndex = {
thumb: 4,
index: 8,
middle: 12,
ring: 16,
pinky: 20
}[args.FINGER];
const coord = args.COORD;
return landmarks[fingerIndex][coord];
}
return 0;
}
async function getFingerDistance(args) {
const hands = await handLandmarker.detectForVideo(video, Date.now());
if (hands.length > 0) {
const hand = hands[0];
const landmarks = hand.landmarks;
const finger1Index = {
thumb: 4,
index: 8,
middle: 12,
ring: 16,
pinky: 20
}[args.FINGER1];
const finger2Index = {
thumb: 4,
index: 8,
middle: 12,
ring: 16,
pinky: 20
}[args.FINGER2];
const point1 = landmarks[finger1Index];
const point2 = landmarks[finger2Index];
return Math.sqrt(
Math.pow(point1.x - point2.x, 2) +
Math.pow(point1.y - point2.y, 2) +
Math.pow(point1.z - point2.z, 2)
);
}
return 0;
}
export default {
init: initializeHandLandmarker,
blocks: {
getFingerPosition: { operation: getFingerPosition },
getFingerDistance: { operation: getFingerDistance }
}
};