-
Notifications
You must be signed in to change notification settings - Fork 0
/
hand.js
255 lines (202 loc) · 7.4 KB
/
hand.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
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
function isMobile () {
const isAndroid = /Android/i.test(navigator.userAgent)
const isiOS = /iPhone|iPad|iPod/i.test(navigator.userAgent)
return isAndroid || isiOS
}
// tf.wasm.setWasmPaths({
// 'tfjs-backend-wasm.wasm': `https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-wasm@${tf.wasm.version_wasm}/dist/tfjs-backend-wasm.wasm`,
// 'tfjs-backend-wasm-simd.wasm': `https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-wasm@${tf.wasm.version_wasm}/dist/tfjs-backend-wasm-simd.wasm`,
// 'tfjs-backend-wasm-threaded-simd.wasm': `https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-wasm@${tf.wasm.version_wasm}/dist/tfjs-backend-wasm-threaded-simd.wasm`,
// })
let videoWidth, videoHeight, rafID, ctx, canvas, ANCHOR_POINTS,
scatterGLHasInitialized = false, scatterGL, fingerLookupIndices = {
thumb: [0, 1, 2, 3, 4],
indexFinger: [0, 5, 6, 7, 8],
middleFinger: [0, 9, 10, 11, 12],
ringFinger: [0, 13, 14, 15, 16],
pinky: [0, 17, 18, 19, 20]
} // for rendering each finger as a polyline
const VIDEO_WIDTH = 640
const VIDEO_HEIGHT = 500
const mobile = isMobile()
// Don't render the point cloud on mobile in order to maximize performance and
// to avoid crowding limited screen space.
const renderPointcloud = mobile === false
const state = {
backend: 'webgl'
}
const stats = new Stats()
stats.showPanel(0)
document.body.appendChild(stats.dom)
if (renderPointcloud) {
state.renderPointcloud = true
}
function setupDatGui () {
const gui = new dat.GUI()
gui.add(state, 'backend', ['webgl', 'wasm'])
.onChange(async backend => {
window.cancelAnimationFrame(rafID)
await tf.setBackend(backend)
await addFlagLabels()
landmarksRealTime(video)
})
if (renderPointcloud) {
gui.add(state, 'renderPointcloud').onChange(render => {
document.querySelector('#scatter-gl-container').style.display = render ? 'inline-block' : 'none'
})
}
}
function drawPoint (y, x, r) {
ctx.beginPath()
ctx.arc(x, y, r, 0, 2 * Math.PI)
ctx.fill()
}
function drawKeypoints (keypoints) {
const keypointsArray = keypoints
for (let i = 0; i < keypointsArray.length; i++) {
const y = keypointsArray[i][0]
const x = keypointsArray[i][1]
drawPoint(x - 2, y - 2, 3)
}
const fingers = Object.keys(fingerLookupIndices)
for (let i = 0; i < fingers.length; i++) {
const finger = fingers[i]
const points = fingerLookupIndices[finger].map(idx => keypoints[idx])
drawPath(points, false)
}
}
function drawPath (points, closePath) {
const region = new Path2D()
region.moveTo(points[0][0], points[0][1])
for (let i = 1; i < points.length; i++) {
const point = points[i]
region.lineTo(point[0], point[1])
}
if (closePath) {
region.closePath()
}
ctx.stroke(region)
}
let model
async function setupCamera () {
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
throw new Error('Browser API navigator.mediaDevices.getUserMedia not available')
}
const video = document.getElementById('video')
const stream = await navigator.mediaDevices.getUserMedia({
audio: false,
video: {
facingMode: 'user',
// Only setting the video to a specified size in order to accommodate a
// point cloud, so on mobile devices accept the default size.
width: mobile ? undefined : VIDEO_WIDTH,
height: mobile ? undefined : VIDEO_HEIGHT
},
})
video.srcObject = stream
return new Promise((resolve) => {
video.onloadedmetadata = () => {
resolve(video)
}
})
}
async function loadVideo () {
const video = await setupCamera()
video.play()
return video
}
async function addFlagLabels () {
if (!document.querySelector('#simd_supported')) {
const simdSupportLabel = document.createElement('div')
simdSupportLabel.id = 'simd_supported'
simdSupportLabel.style = 'font-weight: bold'
const simdSupported = await tf.env().getAsync('WASM_HAS_SIMD_SUPPORT')
simdSupportLabel.innerHTML = `SIMD supported: <span class=${simdSupported}>${simdSupported}<span>`
document.querySelector('#info').appendChild(simdSupportLabel)
}
if (!document.querySelector('#threads_supported')) {
const threadSupportLabel = document.createElement('div')
threadSupportLabel.id = 'threads_supported'
threadSupportLabel.style = 'font-weight: bold'
const threadsSupported = await tf.env().getAsync('WASM_HAS_MULTITHREAD_SUPPORT')
threadSupportLabel.innerHTML = `Threads supported: <span class=${threadsSupported}>${threadsSupported}</span>`
document.querySelector('#info').appendChild(threadSupportLabel)
}
}
async function main () {
await tf.setBackend(state.backend)
if (!tf.env().getAsync('WASM_HAS_SIMD_SUPPORT') && state.backend == 'wasm') {
console.warn('The backend is set to WebAssembly and SIMD support is turned off.\nThis could bottleneck your performance greatly, thus to prevent this enable SIMD Support in chrome://flags')
}
model = await handpose.load()
let video
try {
video = await loadVideo()
} catch (e) {
const info = document.getElementById('info')
info.textContent = e.message
info.style.display = 'block'
throw e
}
setupDatGui()
videoWidth = video.videoWidth
videoHeight = video.videoHeight
canvas = document.getElementById('output')
canvas.width = videoWidth
canvas.height = videoHeight
video.width = videoWidth
video.height = videoHeight
ctx = canvas.getContext('2d')
ctx.clearRect(0, 0, videoWidth, videoHeight)
ctx.strokeStyle = 'red'
ctx.fillStyle = 'red'
ctx.translate(canvas.width, 0)
ctx.scale(-1, 1)
// These anchor points allow the hand pointcloud to resize according to its
// position in the input.
ANCHOR_POINTS = [
[0, 0, 0], [0, -VIDEO_HEIGHT, 0], [-VIDEO_WIDTH, 0, 0],
[-VIDEO_WIDTH, -VIDEO_HEIGHT, 0]
]
if (renderPointcloud) {
document.querySelector('#scatter-gl-container').style = `width: ${VIDEO_WIDTH}px height: ${VIDEO_HEIGHT}px`
scatterGL = new ScatterGL(document.querySelector('#scatter-gl-container'), { 'rotateOnStart': false, 'selectEnabled': false })
}
landmarksRealTime(video)
}
const landmarksRealTime = async (video) => {
async function frameLandmarks () {
stats.begin()
ctx.drawImage(video, 0, 0, videoWidth, videoHeight, 0, 0, canvas.width, canvas.height)
const predictions = await model.estimateHands(video)
if (predictions.length > 0) {
const result = predictions[0].landmarks
drawKeypoints(result, predictions[0].annotations)
if (renderPointcloud === true && scatterGL != null) {
const pointsData = result.map(point => {
return [-point[0], -point[1], -point[2]]
})
const dataset = new ScatterGL.Dataset([...pointsData, ...ANCHOR_POINTS])
if (!scatterGLHasInitialized) {
scatterGL.render(dataset)
const fingers = Object.keys(fingerLookupIndices)
scatterGL.setSequences(fingers.map(finger => ({ indices: fingerLookupIndices[finger] })))
scatterGL.setPointColorer((index) => {
if (index < pointsData.length) {
return 'steelblue'
}
return 'white' // Hide.
})
} else {
scatterGL.updateDataset(dataset)
}
scatterGLHasInitialized = true
}
}
stats.end()
rafID = requestAnimationFrame(frameLandmarks)
}
frameLandmarks()
}
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia
main()