-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
466 lines (357 loc) · 12.6 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<!DOCTYPE html>
<!-- saved from url=(0030)https://gestos.mathigatti.com/ -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- Title -->
<title>Gestures to MIDI</title>
<meta property="og:title" content="Gestures to MIDI">
<!--Favicon-->
<link rel="icon" href="https://mathigatti.com/favicon/favicon.png">
<!--Preview Images-->
<meta property="og:image" content="https://github.com/mathigatti/gestures2anything/raw/main/imgs/website.jpg">
<link rel="image_src" href="/imgs/website.jpg">
<!--Description-->
<meta name="description" content="Use your webcam to identify gestures and control anything with your hands">
<meta property="og:description" content="Use your webcam to identify gestures and control anything with your hands">
<!--Author-->
<meta name="author" content="Mathias Gatti">
<meta name="twitter:card" content="summary_large_image">
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/handpose"></script>
<script src="fingerpose.js" type="text/javascript"></script>
<script src="https://cdn.jsdelivr.net/npm/webmidi"></script>
<script src="https://github.com/Tonejs/Tone.js/releases/download/14.7.39/Tone.js"></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js" crossorigin="anonymous"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap-theme.min.css" integrity="sha384-6pzBo3FDv/PJ8r2KRkGHifhEocL+1X2rVCTTkUfGk7/0pbek5mMa1upzvWbrUbOZ" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd" crossorigin="anonymous"></script>
<style>
* {
box-sizing: border-box;
}
html, body {
width: 100%;
height: 100%;
font-family: Arial, sans-serif;
background-color: #333333;
color: #ffffff;
}
body {
margin: 0;
padding: 0;
}
img {
border: 5px solid #000;
}
#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;
}
</style>
</head>
<body>
<div class="container">
<div class="header clearfix">
<h1>Gestures to MIDI</h1>
<p><a href="#">Getures Tracker</a> | <a href="/slider.html">Hand Tracker</a></p>
</div>
<p class="lead">Use your gestures to control anything. Try triggering some loops in the browser or connect it to a MIDI input and control <a href="https://github.com/mathigatti/gestures2anything" target="_blank">whatever you want</a>.</p>
<select class="form-control" id="outputs"><option>Choose your MIDI input</option></select>
</br>
<button class="btn btn-primary" type="submit" onclick="Tone.Transport.stop();music_on=false;">Mute browser music</button>
<div align="center">
<div id="video-container">
<video id="pose-video" class="layer" playsinline="" width="640" height="480"></video>
<canvas id="pose-canvas" class="layer" width="640" height="480"></canvas>
<div id="pose-result" class="layer"></div>
</div>
<h4>Developed by <a align="center" href="https://mathigatti.com/" target="_blank">mathigatti</a></h4>
</div>
</br>
</br>
<table class="table ">
<tbody>
<tr>
<th>Gesture</th>
<td><img src="imgs/0.png" width="100px" height="100px"></td>
<td><img src="imgs/1.png" width="100px" height="100px"></td>
<td><img src="imgs/2.png" width="100px" height="100px"></td>
<td><img src="imgs/3.png" width="100px" height="100px"></td>
<td><img src="imgs/4.png" width="100px" height="100px"></td>
<td><img src="imgs/5.png" width="100px" height="100px"></td>
<td><img src="imgs/rock&roll.png" width="100px" height="100px"></td>
</tr>
<tr>
<th>Loop</th>
<td>Resting gesture</td>
<td>Drums 1</td>
<td>Drums 2</td>
<td>Bass 1</td>
<td>Melody 1</td>
<td>Melody 2</td>
<td>Melody 3</td>
</tr>
<tr>
<th>Playing?</th>
<td><input type="checkbox" id="loop"></td>
<td><input type="checkbox" id="loop"></td>
<td><input type="checkbox" id="loop"></td>
<td><input type="checkbox" id="loop"></td>
<td><input type="checkbox" id="loop"></td>
<td><input type="checkbox" id="loop"></td>
<td><input type="checkbox" id="loop"></td>
</tr>
</tbody>
</table>
</div>
<script>
// Enable WebMidi.js
WebMidi.enable(function (err) {
if (err) {
console.log("WebMidi could not be enabled.", err);
}
WebMidi.outputs.forEach(function (output, index) {
console.log("se vienee");
console.log(output.name);
$("#outputs").append(new Option(output.name, output.name));
});
});
var music_on = true;
function send_midi(note_name) {
var chosen_midi_output = $('#outputs').find(":selected").text();
WebMidi.outputs.forEach(function (output, index) {
if (output.name == chosen_midi_output) {
//output.playNote(note_name);
output.sendControlChange(75,0);
}
});
WebMidi.enable(function (err) {
if (err) {
console.log("WebMidi could not be enabled.", err);
}
WebMidi.outputs.forEach(function (output, index) {
if (output.name == chosen_midi_output) {
//output.playNote(note_name);
output.sendControlChange('soundcontrol6',0);
}
});
});
}
const config = {
video: { width: 640, height: 480, fps: 30 }
};
const landmarkColors = {
thumb: 'red',
indexFinger: 'blue',
middleFinger: 'yellow',
ringFinger: 'green',
pinky: 'pink',
palmBase: 'white'
};
const gestureStrings = {
'0': '0',
'1': '1',
'2': '2',
'3': '3',
'4': '4',
'5': '5',
'rock': 'rock'
};
const gesture2index = {
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'rock': 6
};
var predictions_array = [];
var last_gesture = null;
function count(array, elem) {
let c = 0;
for (let i = 0; i < array.length; i++)
if (array[i] == elem) {
c++
}
return c;
}
async function main() {
const video = document.querySelector("#pose-video");
const canvas = document.querySelector("#pose-canvas");
const ctx = canvas.getContext("2d");
const resultLayer = document.querySelector("#pose-result");
// configure gesture estimator
const knownGestures = [
fp.Gestures.ZeroGesture,
fp.Gestures.OneGesture,
fp.Gestures.TwoGesture,
fp.Gestures.ThreeGesture,
fp.Gestures.FourGesture,
fp.Gestures.FiveGesture,
fp.Gestures.RockGesture,
];
const GE = new fp.GestureEstimator(knownGestures);
// load handpose model
const model = await handpose.load();
console.log("Handpose model loaded");
// main estimation loop
const estimateHands = async () => {
// clear canvas overlay
ctx.clearRect(0, 0, config.video.width, config.video.height);
resultLayer.innerText = '';
// get hand landmarks from video
// Note: Handpose currently only detects one hand at a time
// Therefore the maximum number of predictions is 1
const predictions = await model.estimateHands(video, true);
for(let i = 0; i < predictions.length; i++) {
// draw colored dots at each predicted joint position
for(let part in predictions[i].annotations) {
for(let point of predictions[i].annotations[part]) {
drawPoint(ctx, point[0], point[1], 3, landmarkColors[part]);
}
}
// now estimate gestures based on landmarks
// using a minimum confidence of 7.5 (out of 10)
const est = GE.estimate(predictions[i].landmarks, 7.5);
if(est.gestures.length > 0) {
// find gesture with highest confidence
let result = est.gestures.reduce((p, c) => {
return (p.confidence > c.confidence) ? p : c;
});
resultLayer.innerText = gestureStrings[result.name];
predictions_array.push(result.name);
predictions_array = predictions_array.slice(Math.max(predictions_array.length - 10, 0));
if ( last_gesture != result.name && count(predictions_array, result.name) > 5 ) {
last_gesture = result.name
send_midi(gesture2index[result.name]);
switch_loop(gesture2index[result.name]);
}
}
}
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();
}
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");
});
function select_loop(i) {
var tick = document.getElementsByTagName('input')[i];
if (!tick.checked){
tick.checked = true;
if (music_on){
Tone.start();
Tone.Transport.start();
}
}
}
function switch_loop(i) {
var tick = document.getElementsByTagName('input')[i];
if (!tick.checked) {
select_loop(i)
} else {
unselect_loop(i);
}
}
function unselect_loop(i) {
var tick = document.getElementsByTagName('input')[i];
tick.checked = false;
}
function unselect_all(){
get_selected_loops().forEach(unselect_loop);
}
function get_selected_loops() {
var ticks = document.getElementsByTagName('input');
var selected_ticks = [];
for (i = 0; i < ticks.length; i++) {
if (ticks[i].checked){
selected_ticks.push(i)
}
}
return selected_ticks
}
function bpm(){
return 128;
}
const loop_duration = 2;
const loops = new Tone.Players({
urls: {
0: "silence.mp3",
1: "drums1.mp3",
2: "drums2.mp3",
3: "bass1.mp3",
4: "melody1.mp3",
5: "melody2.mp3",
6: "melody3.mp3",
},
fadeOut: "64n",
baseUrl: "loops/"
}).toDestination();
const osc = new Tone.Oscillator().toDestination();
Tone.Transport.bpm.value = bpm();
Tone.Transport.scheduleRepeat(time => {
get_selected_loops().forEach(function (num) {
loops.player(num).start(time).stop(time + loop_duration);
}
)
}, "1n");
</script>
</body></html>