-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
166 lines (147 loc) · 6.05 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="description" content="Detector de moedas de 1 real e 50 centavos">
<meta name="keywords" content="IA, HTML, JavaScript, CSS, Edge Impulse, WASM">
<meta name="author" content="Alexsander Gutierrez Gonçalves">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="theme-color" content="#000000">
<title>Detector de moedas de 1 real e 50 centavos</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
background-color: #000;
}
h1,
input,
label,
output {
text-align: center;
color: #fff;
font-family: monospace;
display: block;
margin: auto;
}
#canvas {
inset: 0;
margin: auto;
}
</style>
</head>
<body>
<h1></h1>
<canvas id="canvas"></canvas>
<label for="accuary-input">
Precisão:
<input id="accuary-input" type="range" value="0" min="0" max="1" step="0.01" />
<output id="accuary-output">0</output>
</label>
<canvas id="offcanvas" hidden width="48" height="48"></canvas>
<video id="video" hidden controls autoplay></video>
<script src="edge-impulse-standalone.js"></script>
<script src="run-impulse.js"></script>
<script>
(async () => {
var classifier = new EdgeImpulseClassifier();
await classifier.init();
let project = classifier.getProjectInfo();
document.querySelector('h1').textContent = project.owner + ' / ' + project.name + ' (version ' + project.deploy_version + ')';
let results = [];
function detect() {
try {
let imageData = offctx.getImageData(0, 0, 48, 48);
let values = [];
for (let ix = 0; ix < imageData.width * imageData.height; ix++) {
values.push(Number((imageData.data[ix * 4] << 16)
| (imageData.data[ix * 4 + 1] << 8)
| (imageData.data[ix * 4 + 2])));
}
let res = classifier.classify(values);
results = res.results;
}
catch (ex) {
alert('Failed to classify: ' + (ex.message || ex.toString()));
}
}
let accuary = 0;
/** @type {HTMLCanvasElement} */
const canvas = document.getElementById('canvas');
/** @type {HTMLCanvasElement} */
const offcanvas = document.getElementById('offcanvas');
/** @type {HTMLInputElement} */
const accuaryInput = document.getElementById('accuary-input');
/** @type {HTMLOutputElement} */
const accuaryOutput = document.getElementById('accuary-output');
accuaryInput.addEventListener('input', () => {
accuary = accuaryInput.value;
accuaryOutput.value = accuary;
})
const ctx = canvas.getContext('2d');
const offctx = offcanvas.getContext('2d');
function drawResults(results) {
for (let i = 0; i < results.length; i++) {
const { x, y, width, height, label, value } = results[i];
if (value <= accuary) {
continue;
}
const _x = x / 48 * canvas.width;
const _y = y / 48 * canvas.height;
const _w = width / 48 * canvas.width;
const _h = height / 48 * canvas.height;
const color = label == "1 real" ? "green" : "red";
ctx.beginPath();
ctx.strokeStyle = color;
ctx.lineWidth = 2;
ctx.arc(_x + _w / 2, _y + _h / 2, Math.min(width, height), 0, 2 * Math.PI);
ctx.stroke();
ctx.closePath();
const fontSize = 16;
ctx.font = `${fontSize}px monospace`;
const text = `${label} ${value}`;
const txt = ctx.measureText(text);
ctx.fillStyle = color;
const textWidth = Math.abs(txt.actualBoundingBoxLeft) + Math.abs(txt.actualBoundingBoxRight);
const textHeight = Math.abs(txt.actualBoundingBoxAscent) + Math.abs(txt.actualBoundingBoxDescent);
const rectWidth = textWidth + 16;
const rectHeight = textHeight + 8;
ctx.fillRect(_x + _w / 2 - rectWidth / 2, _y, rectWidth, rectHeight);
ctx.fillStyle = "#fff";
ctx.textRendering = "optimizeLegibility";
ctx.fillText(text, _x + _w / 2 - textWidth / 2, _y + textHeight / 2 + rectHeight / 2);
}
}
function loop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
offctx.drawImage(video, 0, 0, 48, 48);
detect();
drawResults(results);
window.requestAnimationFrame(loop);
}
/** @type {HTMLVideoElement} */
const video = document.getElementById('video');
try {
navigator.mediaDevices.getUserMedia({
audio: false,
video: {
aspectRatio: 1,
facingMode: 'environment'
}
}).then(mediaStream => {
video.srcObject = mediaStream;
const size = Math.min(window.innerWidth, window.innerHeight);
canvas.width = size;
canvas.height = size;
loop();
})
} catch (e) {
alert(e)
}
})();
</script>
</body>
</html>