-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
198 lines (180 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>MNIST recognition with MATLAB</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.6.2/css/bulma.min.css">
</head>
<body>
<div class="container">
<h1 class="title">Hand-written digits recognition with WebAssembly generated from MATLAB</h1>
<h2 class="subtitle">Using MNIST network imported from ONNX</h2>
<div class="columns is-centered">
<div class="column is-narrow">
<canvas id="draw-area" width="280" height="280" style="border: 2px solid;"></canvas>
<div class="field is-grouped">
<p class="control">
<a id="predict-button" class="button is-link" onclick="prediction()">
Prediction
</a>
</p>
<p class="control">
<a class="button" onclick="reset()">
Reset
</a>
</p>
</div>
</div>
<div class="column is-3">
<table class="table">
<thead>
<tr>
<th>Number</th>
<th>Score</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td class="accuracy" data-row-index="0">-</td>
</tr>
<tr>
<th>1</th>
<td class="accuracy" data-row-index="1">-</td>
</tr>
<tr>
<th>2</th>
<td class="accuracy" data-row-index="2">-</td>
</tr>
<tr>
<th>3</th>
<td class="accuracy" data-row-index="3">-</td>
</tr>
<tr>
<th>4</th>
<td class="accuracy" data-row-index="4">-</td>
</tr>
<tr>
<th>5</th>
<td class="accuracy" data-row-index="5">-</td>
</tr>
<tr>
<th>6</th>
<td class="accuracy" data-row-index="6">-</td>
</tr>
<tr>
<th>7</th>
<td class="accuracy" data-row-index="7">-</td>
</tr>
<tr>
<th>8</th>
<td class="accuracy" data-row-index="8">-</td>
</tr>
<tr>
<th>9</th>
<td class="accuracy" data-row-index="9">-</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/signature_pad/1.5.3/signature_pad.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"></script>
<script async type="text/javascript" src="js/my_predict.js"></script>
<script>
// init SignaturePad
const drawElement = document.getElementById('draw-area');
const signaturePad = new SignaturePad(drawElement, {
minWidth: 6,
maxWidth: 6,
penColor: 'white',
backgroundColor: 'black',
});
function getImageData() {
const inputWidth = inputHeight = 28;
// resize
const tmpCanvas = document.createElement('canvas').getContext('2d');
tmpCanvas.drawImage(drawElement, 0, 0, inputWidth, inputHeight);
// convert grayscale
let imageData = tmpCanvas.getImageData(0, 0, inputWidth, inputHeight);
for (let i = 0; i < imageData.data.length; i += 4) {
const avg = (imageData.data[i] + imageData.data[i + 1] + imageData.data[i + 2]) / 3;
imageData.data[i] = imageData.data[i + 1] = imageData.data[i + 2] = avg;
}
return imageData;
}
function getAccuracyScores(imageData) {
let inputs = [];
let length = 28 * 28;
for (let i = 0; i < length * 4; i = i + 4) {
inputs.push(imageData.data[i]/255);
}
console.log(inputs); // Create Data var
var Inputs = new Float32Array(inputs);
var Outputs = new Float32Array(10);
// Move Data to Heap var
var Inputsbytes = _arrayToHeap(Inputs);
var Outputsbytes = _arrayToHeap(Outputs);
// Run Function
Module._my_predict_initialize();
Module._my_predict(Inputsbytes.byteOffset, Outputsbytes.byteOffset)
Module._my_predict_terminate();
// Copy Data from Heap
Outputs = _heapToArray(Outputsbytes, Outputs);
var outputs = Array.from(Outputs);
// Free Data from Heap
_freeArray(Inputsbytes);
_freeArray(Outputsbytes);
// Display Results
console.log(outputs);
const score = outputs;
return score;
}
function prediction() {
const imageData = getImageData();
const accuracyScores = getAccuracyScores(imageData);
const maxAccuracy = accuracyScores.indexOf(Math.max.apply(null, accuracyScores));
const elements = document.querySelectorAll(".accuracy");
elements.forEach(el => {
el.parentNode.classList.remove('is-selected');
const rowIndex = Number(el.dataset.rowIndex);
if (maxAccuracy === rowIndex) {
el.parentNode.classList.add('is-selected');
}
el.innerText = accuracyScores[rowIndex].toFixed(4);
})
}
function reset() {
signaturePad.clear();
let elements = document.querySelectorAll(".accuracy");
elements.forEach(el => {
el.parentNode.classList.remove('is-selected');
el.innerText = '-';
})
}
</script>
<script>
// JavaScript Array to Emscripten Heap
function _arrayToHeap(typedArray) {
var numBytes = typedArray.length * typedArray.BYTES_PER_ELEMENT;
var ptr = Module._malloc(numBytes);
var heapBytes = new Uint8Array(Module.HEAPU8.buffer, ptr, numBytes);
heapBytes.set(new Uint8Array(typedArray.buffer));
return heapBytes;
}
// Emscripten Heap to JavasSript Array
function _heapToArray(heapBytes, array) {
return new Float32Array(
heapBytes.buffer,
heapBytes.byteOffset,
heapBytes.length / array.BYTES_PER_ELEMENT);
}
// Free Heap
function _freeArray(heapBytes) {
Module._free(heapBytes.byteOffset);
}
</script>
</body>
</html>