-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslider.html
334 lines (246 loc) · 9.5 KB
/
slider.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
<!DOCTYPE html>
<!-- saved from url=(0030)https://slider.mathigatti.com/ -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- Title -->
<title>Hand to MIDI</title>
<meta property="og:title" content="Hand 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 track your hand and trigger sounds">
<meta property="og:description" content="Use your webcam to track your hand and trigger sounds">
<!--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://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" 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>
<script src="https://github.com/Tonejs/Tone.js/releases/download/14.7.39/Tone.js"></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;
}
#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>Hand to MIDI</h1>
<p><a href="/">Getures Tracker</a> | <a href="#">Hand Tracker</a></p>
</div>
<p class="lead">Move your hand it will be mapped to numbers. You can use the number to <a href="https://github.com/mathigatti/gestures2anything" target="_blank">trigger stuff</a> or just play with the browser <a href="https://en.wikipedia.org/wiki/Theremin" target="_blank">theremin</a>.</p>
<select class="form-control" id="outputs"><option>Choose your MIDI input</option></select>
</br>
Turn on theremin: <input type="checkbox" id="loop" name="loop1" checked>
<br/>
<br/>
<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>
</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) {
$("#outputs").append(new Option(output.name, output.name));
});
});
</script>
<script>
const config = {
video: { width: 640, height: 480, fps: 30 }
};
const min_cc = 0;
const max_cc = 119;
const landmarkColors = {
thumb: 'red',
indexFinger: 'blue',
middleFinger: 'yellow',
ringFinger: 'green',
pinky: 'pink',
palmBase: 'white'
};
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
// load handpose model
const model = await handpose.load();
console.log("Handpose model loaded");
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]);
}
}
var min_v = 0;
var max_v = 480;
var min_h = 0;
var max_h = 640;
var value_v = predictions[0].boundingBox.bottomRight[1];
value_v = 1 - (value_v - min_v)/(max_v-min_v);
value_v = Math.floor(value_v * max_cc);
value_v = Math.max(value_v,min_cc);
value_v = Math.min(value_v,max_cc);
var value_h = predictions[0].boundingBox.bottomRight[0];
value_h = (value_h - min_h)/(max_h-min_h);
value_h = Math.floor(value_h * max_cc);
value_h = Math.max(value_h,min_cc);
value_h = Math.min(value_h,max_cc);
resultLayer.innerText = 'h:' + value_h + ' v:' + value_v;
send_midi_cc(value_v,75);
send_midi_cc(value_h,76);
if ( $('input')[0].checked ) {
updateVolume(value_v);
updateFrequency(value_h);
} else {
updateVolume(-10000);
}
}
// ...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();
}
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");
});
const osc = new Tone.Oscillator(440, "sine").toDestination().start();
var updateFrequency = function (handXPosition) {
var minFrequency = 20,
maxFrequency = 2000;
var freq = ((handXPosition / max_cc) * (maxFrequency-minFrequency)) + minFrequency;
osc.frequency.value = freq;
Tone.start();Tone.Transport.start();
};
var updateVolume = function (handYPosition) {
var minDB = -30,
maxDB = 0;
var db = ((handYPosition / max_cc) * (maxDB - minDB)) + minDB;
osc.volume.value = db;
Tone.start();Tone.Transport.start();
};
function send_midi_cc(controller, value) {
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(controller,value);
}
});
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);
}
});
});
}
</script>
</body></html>