-
Notifications
You must be signed in to change notification settings - Fork 0
/
hands.html
82 lines (73 loc) · 2.56 KB
/
hands.html
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
<!DOCTYPE html><html lang="ja"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width">
<title>Mediapipe Hands test</title>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/control_utils/control_utils.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/drawing_utils/drawing_utils.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@mediapipe/hands/hands.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Mediapipe Hands test</h1>
<div class="container">
<video id="videoElement" playsinline></video>
<canvas id="canvasElement"></canvas>
</div>
<label><input type="checkbox" id="showimg">show original image</label>
<label><input type="checkbox" id="mirrormode" checked>mirror mode</label>
<label><input type="checkbox" id="backcameramode">backcamera mode</label>
<hr>
<footer>
<a href=./>demo index</a><br>
lib: <a href="https://chuoling.github.io/mediapipe/solutions/hands.html">Hands - mediapipe</a><br>
src: <a href="https://github.com/code4fukui/mediapipe-test/">mediapipe-test</a><br>
</footer>
<script type="module">
import { Camera } from "https://code4fukui.github.io/Camera/Camera.js";
const g = canvasElement.getContext("2d");
const hands = new Hands({ locateFile: (file) => `https://cdn.jsdelivr.net/npm/@mediapipe/hands/${file}` });
hands.setOptions({
maxNumHands: 10,
modelComplexity: 1,
minDetectionConfidence: 0.5,
minTrackingConfidence: 0.5,
});
hands.onResults((res) => {
const w = canvasElement.width;
const h = canvasElement.height;
g.save();
if (mirrormode.checked) {
g.scale(-1, 1);
g.translate(-w, 0);
/* // 180度回転
g.translate(w / 2, h / 2);
g.rotate(Math.PI);
g.translate(-w / 2, -h / 2);
*/
}
g.clearRect(0, 0, w, h);
if (showimg.checked) {
g.drawImage(res.image, 0, 0, w, h);
}
if (res.multiHandLandmarks) {
for (const landmarks of res.multiHandLandmarks) {
drawConnectors(g, landmarks, HAND_CONNECTIONS, { color: "#222", lineWidth: 5 });
drawLandmarks(g, landmarks, { color: "#222", lineWidth: 2 });
}
}
g.restore();
});
const camera = new Camera(videoElement, {
onFrame: async () => {
const dpi = devicePixelRatio;
canvasElement.width = videoElement.videoWidth * dpi;
canvasElement.height = videoElement.videoHeight * dpi;
await hands.send({ image: videoElement });
},
width: 1280,
height: 720,
backcamera: backcameramode.checked,
});
camera.start();
backcameramode.onchange = () => camera.flip();
</script>
</body>
</html>