-
Notifications
You must be signed in to change notification settings - Fork 38
/
indexEI.html
493 lines (315 loc) · 17.8 KB
/
indexEI.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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
<body>
<div id="main">
<canvas id="myGrowCanvas" style="position:absolute; left:0px; top:0px; width:1200px; height:600px; " ></canvas> <br>
<canvas id="myOverlap2" style="position:absolute; left:0px; top:0px; width:120px; height:600px; background:transparent; " ></canvas>
<table border=1 style="position:absolute; left:800px; top:0px; ">
<tr><td>
Version 0.16.6.92 <br>
<div id="description">
<div id="description-title" style="font-size:30px">Demo Edge Impulse WASM</div>
Frequency ms <input type=text id="myText01" value=130 onChange="{
myFrequencyMs = this.value
clearInterval(myTimer01)
myTimer01 = setInterval(myPictureNow, myFrequencyMs);
}"><br>
(square) width/height: <input type=text value="96" size=5 id="myWidthHeight" READONLY> <br>
Stop Data Collection: <input type=checkbox id="myCheckbox01"><br>
Stop All Sounds <input type=checkbox id="myCheckbox02sounds" ><br>
Stop 0 (First) label sound: <input type=checkbox id="myCheckbox03lable0" checked=true><br>
<input type=button value="Width -" onclick="{
document.getElementById('myGrowCanvas').style.width = parseInt(document.getElementById('myGrowCanvas').style.width) - 50
}"><input type=button value="Width +" onclick="{
document.getElementById('myGrowCanvas').style.width = parseInt(document.getElementById('myGrowCanvas').style.width) + 50
}">
<input type=button value="Height -" onclick="{
document.getElementById('myGrowCanvas').style.height = parseInt(document.getElementById('myGrowCanvas').style.height) - 50
}"><input type=button value="Height +" onclick="{
document.getElementById('myGrowCanvas').style.height = parseInt(document.getElementById('myGrowCanvas').style.height) + 50
}">
<br>
Camera: <select id="mySelect" onChange="setupPage()"><option value="user">Front Camera<option selected="selected" value="environment">Rear Camera</select><br>
Default is not mirrored: see canvas: scaleX(-1); transform: scaleX(-1);<br>
Default GRAYSCALE, Check to use Color <input type=checkbox id="myCheckboxColor"><br><br>
<div id="myDiv01" style="font-size:30px">...</div>
<textarea id="myTextArea01" style="display:none; " rows=10 cols=70>...</textarea> <br> <br> <br>
<div id="mySpecialDiv">...</div>
</td></tr></table>
<!-- <video id="video" playsinline="" style="display:none; -webkit-transform: scaleX(1); transform: scaleX(1); width: auto; height: auto;"></video> -->
<video id="video" playsinline="" style="display:none; -webkit-transform: scaleX(-1); transform: scaleX(-1); width: auto; height: auto;"></video>
<canvas id="myOnTopOutput" style="display:none; background-color:white; border:black 3px solid;" ></canvas> <!-- position:absolute; top:0; left:0; -->
<canvas id="output" style="display:none; " ></canvas>
<canvas id="myColorCanvas" style="display:none; " ></canvas> <br>
</div>
</body>
<script src="edge-impulse-standalone.js"></script>
<!-- Link to short internet sounds, load your own sound if uploaded to the same folder with the below code. .wav, .a4u, mp3 sound files should work -->
<!--
<audio preload="auto" id="mySound8" src="mySound01.mp3"></audio>
-->
<audio preload="auto" id="mySound0" src="https://raw.githubusercontent.com/wesbos/JavaScript30/master/01%20-%20JavaScript%20Drum%20Kit/sounds/clap.wav"></audio>
<audio preload="auto" id="mySound1" src="https://raw.githubusercontent.com/wesbos/JavaScript30/master/01%20-%20JavaScript%20Drum%20Kit/sounds/boom.wav"></audio>
<audio preload="auto" id="mySound2" src="https://raw.githubusercontent.com/wesbos/JavaScript30/master/01%20-%20JavaScript%20Drum%20Kit/sounds/kick.wav"></audio>
<audio preload="auto" id="mySound3" src="https://raw.githubusercontent.com/wesbos/JavaScript30/master/01%20-%20JavaScript%20Drum%20Kit/sounds/openhat.wav"></audio>
<audio preload="auto" id="mySound4" src="https://raw.githubusercontent.com/wesbos/JavaScript30/master/01%20-%20JavaScript%20Drum%20Kit/sounds/ride.wav"></audio>
<audio preload="auto" id="mySound5" src="https://raw.githubusercontent.com/wesbos/JavaScript30/master/01%20-%20JavaScript%20Drum%20Kit/sounds/snare.wav"></audio>
<audio preload="auto" id="mySound6" src="https://raw.githubusercontent.com/wesbos/JavaScript30/master/01%20-%20JavaScript%20Drum%20Kit/sounds/tom.wav"></audio>
<audio preload="auto" id="mySound7" src="https://raw.githubusercontent.com/wesbos/JavaScript30/master/01%20-%20JavaScript%20Drum%20Kit/sounds/tink.wav"></audio>
<audio preload="auto" id="mySound8" src="https://raw.githubusercontent.com/wesbos/JavaScript30/master/01%20-%20JavaScript%20Drum%20Kit/sounds/hihat.wav"></audio>
<script>
let myTimer01 = 0;
let myOutputString = ''
let myFrequencyMs = document.getElementById('myText01').value
let myWidthHeightSquare = document.getElementById('myWidthHeight').value
function RGBAToHex(r,g,b,a) {
r = r.toString(16);
g = g.toString(16);
b = b.toString(16);
if (r.length == 1)
r = "0" + r;
if (g.length == 1)
g = "0" + g;
if (b.length == 1)
b = "0" + b;
// return "#" + r + g + b + ", " ;
return "0x" + r + g + b + ", " ;
}
const setupCamera = async function(){
video = document.getElementById('video');
// stop it if running
video.pause()
video.srcObject = null
const stream = await navigator.mediaDevices.getUserMedia({
'audio': false,
'video': { facingMode: document.getElementById('mySelect').value },
});
video.srcObject = stream;
return new Promise((resolve) => {
video.onloadedmetadata = () => {
resolve(video);
};
});
}
const renderPrediction = async function(){
document.ctx.clearRect(0, 0, document.ctx.canvas.width, document.ctx.canvas.width);
document.ctx.drawImage(video, 0, 0, videoWidth, videoHeight);
ctxColor.drawImage(video, 0, 0, videoWidth, videoHeight);
let imgData = document.ctx.getImageData(0, 0, document.ctx.canvas.width, document.ctx.canvas.height);
let pixels = imgData.data;
for (var i = 0; i < pixels.length; i += 4) {
let lightness = parseInt((pixels[i] + pixels[i + 1] + pixels[i + 2]) / 3);
pixels[i] = lightness;
pixels[i + 1] = lightness;
pixels[i + 2] = lightness;
}
document.ctx.scale(-1, 1); // mirror image
document.ctx.putImageData(imgData, 0, 0);
requestAnimationFrame(renderPrediction);
};
const myPictureNow = async function(){
var classifier = new EdgeImpulseClassifier();
await classifier.init();
let props = classifier.getProperties();
let project = classifier.getProjectInfo();
myProjectName = project.name
myWidthHeightSquare = props.input_width
document.getElementById('myWidthHeight').value = props.input_width
// console.log(props.input_width) // props.sensor props.frequency props.frame_sample_count props.input_width props.input_height
ctxOnTop.clearRect(0, 0, canvasOnTop.width, canvasOnTop.height);
let myX = (640-myWidthHeightSquare)/2
let myY = (480-myWidthHeightSquare)/2
let ImageData2;
if (document.getElementById('myCheckboxColor').checked ) {
ImageData2 = ctxColor.getImageData(myX, myY, myWidthHeightSquare, myWidthHeightSquare); // grab COLOR image x,y,width,height
} else {
ImageData2 = document.ctx.getImageData(myX, myY, myWidthHeightSquare, myWidthHeightSquare); // grab GRAYSCALE image x,y,width,height
}
ctxOnTop.putImageData(ImageData2, myX, myY ); //put image near center
ctxGrow.putImageData(ImageData2, 0, 0); // try to grow the image
// alert(RGBAToHex(ImageData2.data[0], ImageData2.data[1], ImageData2.data[2], ImageData2.data[3]) )
let pixels2 = ImageData2.data
myOutputString = ''
if (document.getElementById('myCheckbox01').checked ) {
//console.log('checked')
//console.log(document.getElementById('myCheckbox01').checked)
myOutputString = document.getElementById('myTextArea01').value
document.getElementById('myTextArea01').style.display = 'inline'
} else {
document.getElementById('myTextArea01').style.display = 'none'
for (var i = 0; i < pixels2.length; i += 4) {
myOutputString += RGBAToHex(ImageData2.data[i], ImageData2.data[i+1], ImageData2.data[i+2], ImageData2.data[i+3])
}
// console.log('myOutputString')
// console.log(myOutputString)
// document.getElementById('myTextArea01').value= JSON.stringify(myOutputString, 2, '\n')
// myOutputString = myOutputString.substring(1); // remove first "
myOutputString = myOutputString.substring(0, myOutputString.length - 2); // remove last ,"
document.getElementById('myTextArea01').value = myOutputString
}
results = await classifier.classify( myOutputString.split(', ') ); //, true can set debug true auto shows in console
let myTemp = ''
myTemp = '<table border=1 style="font-size:30px">'
let myBestClassificationNumber = -1
let myBestClassificationValue = 0.25 // lowest best allowable value
for (let j = 0; j < results.results.length; j++){
if (results.results[j].value > myBestClassificationValue ){
myBestClassificationNumber = j; // find the biggest array value
myBestClassificationValue = results.results[j].value
}
}
//////////////////////////////////////////// Where the magic happens //////////////////////////////////////////////////////////////////////////////
ctxOverlap2.beginPath();
ctxOverlap2.clearRect(0, 0, ctxOverlap2.canvas.width, ctxOverlap2.canvas.height)
for (let i = 0; i < results.results.length; i++){
// console.log(results.results[i].label)
// console.log(results.results[i].value)
// console.log(results.results[i].x)
// console.log(results.results[i].y)
// console.log(results.results[i].height)
// console.log(results.results[i].width)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// working on the visual bounding boxes.
myXChange = 10 // adjust for stretch etc
myYChange = 2
ctxOverlap2.lineWidth = "1";
ctxOverlap2.strokeStyle = 'yellow';
ctxOverlap2.font = "20pt Calibri"; //bold 48px serif 30px Arial 60pt Calibri
ctxOverlap2.strokeText(results.results[i].label, results.results[i].x * myXChange, (results.results[i].y-10)*myYChange); // Text, x, y
ctxOverlap2.rect(results.results[i].x * myXChange, results.results[i].y * myYChange, (results.results[i].x + results.results[i].width)*myXChange, (results.results[i].y + results.results[i].height)*myYChange ); // rectangle x,y,width,height
ctxOverlap2.stroke();
if (myBestClassificationNumber == i){ // check if this is the best one
document.getElementById('mySpecialDiv').innerHTML = "Top Result: " //+ results.timing
document.getElementById('mySpecialDiv').innerHTML += "<br>results.results[i].label: " + results.results[i].label
document.getElementById('mySpecialDiv').innerHTML += "<br>results.results[i].value: " + results.results[i].value
document.getElementById('mySpecialDiv').innerHTML += "<br>results.results[i].x: " + results.results[i].x
document.getElementById('mySpecialDiv').innerHTML += "<br>results.results[i].y: " + results.results[i].y
document.getElementById('mySpecialDiv').innerHTML += "<br>results.results[i].width: " + results.results[i].width
document.getElementById('mySpecialDiv').innerHTML += "<br>results.results[i].height: " + results.results[i].height + "<br><br>"
document.getElementById('mySpecialDiv').innerHTML += JSON.stringify(results, null, 4); // might as well show everything
if (! document.getElementById('myCheckbox02sounds').checked){
if (! document.getElementById('myCheckbox03lable0').checked){
if (i==0) { document.getElementById('mySound0').currentTime = 0; document.getElementById('mySound0').play() }
}
if (i==1) { document.getElementById('mySound1').currentTime = 0; document.getElementById('mySound1').play() }
if (i==2) { document.getElementById('mySound2').currentTime = 0; document.getElementById('mySound2').play() }
if (i==3) { document.getElementById('mySound3').currentTime = 0; document.getElementById('mySound3').play() }
if (i==4) { document.getElementById('mySound4').currentTime = 0; document.getElementById('mySound4').play() }
if (i==5) { document.getElementById('mySound5').currentTime = 0; document.getElementById('mySound5').play() }
if (i==6) { document.getElementById('mySound6').currentTime = 0; document.getElementById('mySound6').play() }
if (i==7) { document.getElementById('mySound7').currentTime = 0; document.getElementById('mySound7').play() }
if (i==8) { document.getElementById('mySound8').currentTime = 0; document.getElementById('mySound8').play() }
}
myTemp += '<tr><td>'+ results.results[i].label + '</td><td><font style=\'color:yellow; background-color:blue;\'>' + results.results[i].value.toFixed(3) +'</font></td></tr>'
} else {
myTemp += '<tr><td>'+ results.results[i].label + '</td><td>' + results.results[i].value.toFixed(3) +'</td></tr>'
}
}
myTemp += '</table>'
document.getElementById('myDiv01').innerHTML = 'Project Name: ' + myProjectName +
', results.anomaly: '+ results.anomaly + ', results.timing: ' + results.timing + '<br>' + myTemp
}
const setupPage = async function(){
await setupCamera();
video.play();
videoWidth = video.videoWidth;
videoHeight = video.videoHeight;
console.log('videoWidth')
console.log(videoWidth)
console.log('videoHeight')
console.log(videoHeight)
video.width = videoWidth;
video.height = videoHeight;
canvasOnTop = document.getElementById('myOnTopOutput');
canvasOnTop.width = videoWidth;
canvasOnTop.height = videoHeight;
ctxOnTop = canvasOnTop.getContext('2d');
canvasColor = document.getElementById('myColorCanvas');
canvasColor.width = videoWidth;
canvasColor.height = videoHeight;
ctxColor = canvasColor.getContext('2d');
canvasGrow = document.getElementById('myGrowCanvas');
canvasGrow.width = videoWidth;
canvasGrow.height = videoHeight;
ctxGrow = canvasGrow.getContext('2d');
canvas = document.getElementById('output');
canvas.width = videoWidth;
canvas.height = videoHeight;
document.ctx = canvas.getContext('2d');
canvasOverlap2 = document.getElementById('myOverlap2');
canvasOverlap2.width = videoWidth;
canvasOverlap2.height = videoHeight;
ctxOverlap2 = canvasOverlap2.getContext('2d');
// setInterval(function(){ alert("Hello"); }, 3000);
myTimer01 = setInterval(myPictureNow, myFrequencyMs);
// myTimer01 = setInterval('myPictureNow', 3000);
renderPrediction();
//ctxOverlap2.beginPath();
//ctxOverlap2.lineWidth = "2";
//ctxOverlap2.strokeStyle = "red";
// context.strokeStyle = '#ff0000';
ctxOverlap2.rect(0,0,100,80); // rectangle x,y,width,height
ctxOverlap2.stroke();
};
// main program!
setupPage();
</script>
<script>
// From the old run_impulse.js file
// Classifier module
let classifierInitialized = false;
Module.onRuntimeInitialized = function() {
classifierInitialized = true;
};
class EdgeImpulseClassifier {
_initialized = false;
init() {
if (classifierInitialized === true) return Promise.resolve();
return new Promise((resolve) => {
Module.onRuntimeInitialized = () => {
resolve();
classifierInitialized = true;
};
});
}
getProjectInfo() {
if (!classifierInitialized) throw new Error('Module is not initialized');
return Module.get_project();
}
classify(rawData, debug = false) {
if (!classifierInitialized) throw new Error('Module is not initialized');
let props = Module.get_properties();
const obj = this._arrayToHeap(rawData);
let ret = Module.run_classifier(obj.buffer.byteOffset, rawData.length, debug);
Module._free(obj.ptr);
if (ret.result !== 0) {
throw new Error('Classification failed (err code: ' + ret.result + ')');
}
let jsResult = {
anomaly: ret.anomaly,
results: []
};
for (let cx = 0; cx < ret.size(); cx++) {
let c = ret.get(cx);
if (props.model_type === 'object_detection' || props.model_type === 'constrained_object_detection') {
jsResult.results.push({ label: c.label, value: c.value, x: c.x, y: c.y, width: c.width, height: c.height });
}
else {
jsResult.results.push({ label: c.label, value: c.value });
}
c.delete();
}
ret.delete();
return jsResult;
}
getProperties() {
return Module.get_properties();
}
_arrayToHeap(data) {
let typedArray = new Float32Array(data);
let numBytes = typedArray.length * typedArray.BYTES_PER_ELEMENT;
let ptr = Module._malloc(numBytes);
let heapBytes = new Uint8Array(Module.HEAPU8.buffer, ptr, numBytes);
heapBytes.set(new Uint8Array(typedArray.buffer));
return { ptr: ptr, buffer: heapBytes };
}
}
</script>