-
Notifications
You must be signed in to change notification settings - Fork 112
/
index.html
353 lines (304 loc) · 9.03 KB
/
index.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8" />
<title>FingerPose Example</title>
<!-- Require the peer dependencies of handpose. -->
<!-- You must explicitly require a TF.js backend if you're not using the tfs union bundle. -->
<!-- The main handpose library -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/[email protected]/dist/hand-pose-detection.js"></script>
<!-- The fingerpose library -->
<script src="fingerpose.js" type="text/javascript"></script>
<style>
* {
box-sizing: border-box;
user-select: none;
}
html,
body {
width: 100%;
height: 100%;
overflow: hidden;
font-family: Arial, sans-serif;
background-color: #ffffff;
color: #333333;
}
body {
margin: 0;
padding: 0;
}
.container {
margin: 20px auto;
display: flex;
}
.video,
.debug {
padding: 0 20px;
}
table.summary {
border: 1px solid #333;
border-collapse: collapse;
}
table.summary td,
table.summary th {
border: 1px solid #333;
padding: 5px 8px;
}
#video-container {
width: 640px;
height: 480px;
position: relative;
}
.layer {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
#pose-video {
transform: scaleX(-1);
}
.pose-result {
font-size: 100px;
text-align: right;
padding: 20px 30px 0 0;
}
#pose-result-left {
text-align: left;
}
</style>
</head>
<body>
<div class="container">
<div class="video">
<div id="video-container">
<video id="pose-video" class="layer" playsinline></video>
<canvas id="pose-canvas" class="layer"></canvas>
<div id="pose-result-left" class="layer pose-result"></div>
<br>
<div id="pose-result-right" class="layer pose-result"></div>
</div>
</div>
<div class="debug">
<h2>Left Hand</h2>
<table id="summary-left" class="summary">
<thead>
<tr>
<th>Idx</th>
<th>Finger</th>
<th style="width: 110px">Curl</th>
<th style="width: 170px">Direction</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>Thumb</td>
<td><span id="curl-0">-</span></td>
<td><span id="dir-0">-</span></td>
</tr>
<tr>
<td>1</td>
<td>Index</td>
<td><span id="curl-1">-</span></td>
<td><span id="dir-1">-</span></td>
</tr>
<tr>
<td>2</td>
<td>Middle</td>
<td><span id="curl-2">-</span></td>
<td><span id="dir-2">-</span></td>
</tr>
<tr>
<td>3</td>
<td>Ring</td>
<td><span id="curl-3">-</span></td>
<td><span id="dir-3">-</span></td>
</tr>
<tr>
<td>4</td>
<td>Pinky</td>
<td><span id="curl-4">-</span></td>
<td><span id="dir-4">-</span></td>
</tr>
</tbody>
</table>
<br>
<h2>Right Hand</h2>
<table id="summary-right" class="summary">
<thead>
<tr>
<th>Idx</th>
<th>Finger</th>
<th style="width: 110px">Curl</th>
<th style="width: 170px">Direction</th>
</tr>
</thead>
<tbody>
<tr>
<td>0</td>
<td>Thumb</td>
<td><span id="curl-0">-</span></td>
<td><span id="dir-0">-</span></td>
</tr>
<tr>
<td>1</td>
<td>Index</td>
<td><span id="curl-1">-</span></td>
<td><span id="dir-1">-</span></td>
</tr>
<tr>
<td>2</td>
<td>Middle</td>
<td><span id="curl-2">-</span></td>
<td><span id="dir-2">-</span></td>
</tr>
<tr>
<td>3</td>
<td>Ring</td>
<td><span id="curl-3">-</span></td>
<td><span id="dir-3">-</span></td>
</tr>
<tr>
<td>4</td>
<td>Pinky</td>
<td><span id="curl-4">-</span></td>
<td><span id="dir-4">-</span></td>
</tr>
</tbody>
</table>
</div>
</div>
<script>
const config = {
video: { width: 640, height: 480, fps: 30 }
}
const landmarkColors = {
thumb: 'red',
index: 'blue',
middle: 'yellow',
ring: 'green',
pinky: 'pink',
wrist: 'white'
}
const gestureStrings = {
'thumbs_up': '👍',
'victory': '✌🏻'
}
async function createDetector() {
return window.handPoseDetection.createDetector(
window.handPoseDetection.SupportedModels.MediaPipeHands,
{
runtime: "mediapipe",
modelType: "full",
maxHands: 2,
}
)
}
async function main() {
const video = document.querySelector("#pose-video")
const canvas = document.querySelector("#pose-canvas")
const ctx = canvas.getContext("2d")
const resultLayer = {
right: document.querySelector("#pose-result-right"),
left: document.querySelector("#pose-result-left")
}
// configure gesture estimator
// add "✌🏻" and "👍" as sample gestures
const knownGestures = [
fp.Gestures.VictoryGesture,
fp.Gestures.ThumbsUpGesture
]
const GE = new fp.GestureEstimator(knownGestures)
// load handpose model
const detector = await createDetector()
console.log("mediaPose model loaded")
// main estimation loop
const estimateHands = async () => {
// clear canvas overlay
ctx.clearRect(0, 0, config.video.width, config.video.height)
resultLayer.right.innerText = ''
resultLayer.left.innerText = ''
// get hand landmarks from video
const hands = await detector.estimateHands(video, {
flipHorizontal: true
})
for (const hand of hands) {
for (const keypoint of hand.keypoints) {
const name = keypoint.name.split('_')[0].toString().toLowerCase()
const color = landmarkColors[name]
drawPoint(ctx, keypoint.x, keypoint.y, 3, color)
}
const est = GE.estimate(hand.keypoints3D, 9)
if (est.gestures.length > 0) {
// find gesture with highest match score
let result = est.gestures.reduce((p, c) => {
return (p.score > c.score) ? p : c
})
const chosenHand = hand.handedness.toLowerCase()
resultLayer[chosenHand].innerText = gestureStrings[result.name]
updateDebugInfo(est.poseData, chosenHand)
}
}
// ...and so on
setTimeout(() => { estimateHands() }, 1000 / config.video.fps)
}
estimateHands()
console.log("Starting predictions")
}
async function initCamera(width, height, fps) {
const constraints = {
audio: false,
video: {
facingMode: "user",
width: width,
height: height,
frameRate: { max: fps }
}
}
const video = document.querySelector("#pose-video")
video.width = width
video.height = height
// get video stream
const stream = await navigator.mediaDevices.getUserMedia(constraints)
video.srcObject = stream
return new Promise(resolve => {
video.onloadedmetadata = () => { resolve(video) }
})
}
function drawPoint(ctx, x, y, r, color) {
ctx.beginPath()
ctx.arc(x, y, r, 0, 2 * Math.PI)
ctx.fillStyle = color
ctx.fill()
}
function updateDebugInfo(data, hand) {
const summaryTable = `#summary-${hand}`
for (let fingerIdx in data) {
document.querySelector(`${summaryTable} span#curl-${fingerIdx}`).innerHTML = data[fingerIdx][1]
document.querySelector(`${summaryTable} span#dir-${fingerIdx}`).innerHTML = data[fingerIdx][2]
}
}
window.addEventListener("DOMContentLoaded", () => {
initCamera(
config.video.width, config.video.height, config.video.fps
).then(video => {
video.play()
video.addEventListener("loadeddata", event => {
console.log("Camera is ready")
main()
})
})
const canvas = document.querySelector("#pose-canvas")
canvas.width = config.video.width
canvas.height = config.video.height
console.log("Canvas initialized")
});
</script>
</body>
</html>