-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
86 lines (69 loc) · 2.46 KB
/
main.js
File metadata and controls
86 lines (69 loc) · 2.46 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
79
80
81
82
83
84
85
import {
HandLandmarker,
FilesetResolver
} from "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0";
import Stats from 'https://cdnjs.cloudflare.com/ajax/libs/stats.js/r17/Stats.min.js';
const init = async () =>{
const stats = new Stats();
document.body.appendChild(stats.dom);
const video = document.getElementById("input_video");
const canvasElement = document.getElementById("output_canvas");
const canvasCtx = canvasElement.getContext("2d");
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices
.getUserMedia({ video: true })
.then(function (stream) {
video.srcObject = stream;
video.play();
})
.catch(function (error) {
console.error("Error accessing the camera: ", error);
});
} else {
alert("Sorry, your browser does not support the camera API.");
}
const vision = await FilesetResolver.forVisionTasks(
// path/to/wasm/root
"https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@latest/wasm"
);
const handLandmarker = await HandLandmarker.createFromOptions(
vision,
{
baseOptions: {
modelAssetPath: "./hand_landmarker.task", //.taskファイルを指定する
delegate: "GPU" //CPU or GPUで処理するかを指定する
},
numHands: 2 //認識できる手の数
});
await handLandmarker.setOptions({ runningMode: "video" });
let lastVideoTime = -1;
const renderLoop = () => {
canvasElement.width = video.videoWidth;
canvasElement.height = video.videoHeight;
let startTimeMs = performance.now();
if (video.currentTime > 0 && video.currentTime !== lastVideoTime) {
const results = handLandmarker.detectForVideo(video,startTimeMs);
lastVideoTime = video.currentTime;
canvasCtx.save();
canvasCtx.clearRect(0, 0, canvasElement.width, canvasElement.height);
canvasCtx.drawImage(video, 0, 0, canvasElement.width, canvasElement.height);
if (results.landmarks) {
for (const landmarks of results.landmarks) {
drawConnectors(canvasCtx, landmarks, HAND_CONNECTIONS, {
color: "#00FF00",
lineWidth: 5
});
drawLandmarks(canvasCtx, landmarks, { color: "#FF0000", lineWidth: 2 });
}
}
canvasCtx.restore();
}
requestAnimationFrame(() => {
stats.begin();
renderLoop();
stats.end();
});
}
renderLoop();
}
init();