-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediaPipe.js
51 lines (45 loc) · 1.54 KB
/
mediaPipe.js
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
import {
HandLandmarker,
FilesetResolver,
} from "https://cdn.skypack.dev/@mediapipe/[email protected]";
let handLandmarker;
let runningMode = "VIDEO";
// let video = null;
let lastVideoTime = -1;
let captureEvent;
let loadedCamera;
window.handedness = []
window.landmarks = []
window.worldLandmarks = []
// Before we can use PoseLandmarker class we must wait for it to finish
// loading. Machine Learning models can be large and take a moment to
// get everything needed to run.
const createPoseLandmarker = async () => {
const vision = await FilesetResolver.forVisionTasks(
"https://cdn.jsdelivr.net/npm/@mediapipe/[email protected]/wasm"
);
handLandmarker = await HandLandmarker.createFromOptions(vision, {
baseOptions: {
modelAssetPath: `https://storage.googleapis.com/mediapipe-models/hand_landmarker/hand_landmarker/float16/1/hand_landmarker.task`,
delegate: "GPU"
},
runningMode: runningMode,
numHands: 2
});
};
createPoseLandmarker();
window.predictWebcam = async (video) => {
// Now let's start detecting the stream.
let startTimeMs = performance.now();
if (lastVideoTime !== video.elt.currentTime && handLandmarker) {
lastVideoTime = video.elt.currentTime;
let results = handLandmarker.detectForVideo(video.elt, startTimeMs);
handedness = results.handedness
landmarks = results.landmarks
worldLandmarks = results.worldLandmarks
}
// Call this function again to keep predicting when the browser is ready.
window.requestAnimationFrame(() => {
predictWebcam(video)
});
};