-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
208 lines (172 loc) · 4.47 KB
/
index.js
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
let canvas = document.querySelector('canvas');
let ctx_ = document.getElementById('chart').getContext('2d');
let ctx = canvas.getContext('2d');
let isWriting = false;
let updateCanvas = () => {
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
}
let reset = () => {
updateCanvas();
pixels = [];
createGrid();
}
// Window Handlers.
window.onload = updateCanvas();
window.addEventListener('resize', e => reset());
window.addEventListener('keyup', e => {
if(e.keyCode == 67) {
reset();
}
});
const gridHeight = 28;
const gridWidth = 28;
let mouseX = 0;
let mouseY = 0;
let pixels = [];
class Pixel {
constructor(x, y, dim) {
this.x = x;
this.y = y;
this.isOn = false;
this.dim = dim;
this.bounding = [x, x + dim, y, y + dim];
}
draw() {
if (!this.isOn) {
ctx.fillStyle = '#0d0d0d';
ctx.strokeStyle = '#0d0d0d';
} else {
ctx.fillStyle = '#d3d3d3';
ctx.strokeStyle = '#d3d3d3'
}
ctx.beginPath();
ctx.fillRect(this.x, this.y, this.dim, this.dim);
ctx.lineWidth = 1;
ctx.strokeRect(this.x, this.y, this.dim, this.dim);
}
}
let pixelDim = null;
let anchorX = null;
let anchorY = null;
let gridBounding = null;
let createGrid = () => {
pixelDim = 20;
anchorX = (canvas.width / 2) - (pixelDim * (gridWidth/2));
anchorY = canvas.height / 2 - (pixelDim * (gridHeight/5));
gridBounding = [anchorX, anchorX + (pixelDim * gridWidth), anchorY, anchorY + (pixelDim * gridHeight)];
for (let i = 0; i < gridHeight; i++) {
let x = anchorX + (pixelDim * i);
for (let j = 0; j < gridWidth; j++) {
let y = anchorY + (pixelDim * j);
let p = new Pixel(x, y, pixelDim);
p.draw();
pixels.push(p);
}
}
}
let inBounds = (x, y, target) => {
return (x > target[0] && x < target[1]) &&
(y > target[2] && y < target[3]);
}
window.addEventListener('mousedown', e => {
mouseX = e.clientX;
mouseY = e.clientY;
if (inBounds(mouseX, mouseY, gridBounding)) {
isWriting = true;
}
});
// GRID EVENT HANDLERS //
window.addEventListener('mousemove', e => {
mouseX = e.clientX;
mouseY = e.clientY;
if (!inBounds(mouseX, mouseY, gridBounding)) {
isWriting = false;
}
else if (isWriting) {
pixels.forEach(p => {
if (!p.isOn) {
if (inBounds(mouseX, mouseY, p.bounding)) {
p.isOn = true;
p.draw();
}
}
});
}
});
window.addEventListener('mouseup', e => {
mouseX = e.clientX;
mouseY = e.clientY;
if (isWriting) {
raw_matrix = parseGrid();
// Predict with CNN.
let softmax = predict(raw_matrix).dataSync();
let preds = Array.from(softmax).map(n => parseFloat(n.toPrecision(4)));
console.log(preds);
let bar = Chart.Bar(ctx_, {
// The data for our dataset
data: {
labels: ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
datasets: [{
backgroundColor: 'rgba(247, 127, 155, 0.5)',
borderColor: 'black',
data: preds,
borderWidth: 2
}]
},
// Configuration options go here
options: {
legend: {
display: false,
},
responsive: false,
scales: {
xAxes: [{
ticks: {
fontSize: 30
}
}],
yAxes: [{
ticks: {
fontSize: 25
}
}]
}
}
});
//bar.resize(200, 100);
}
isWriting = false;
});
// TensorFlow.js stuff. //
// Load MNIST_Model...
let model = null;
async function loadNeuralNet() {
// link para seu arquivo model.json
model = await tf.loadLayersModel('https://qodatecnologia.github.io/tfjs/json/model.json');
}
// Parses our grid into a matrix so we can then convert to a tensor.
let parseGrid = () => {
matrix = [];
for (let i = 0; i < gridHeight; i++) {
matrix_col = [];
for (let j = 0; j < gridWidth; j++) {
if (pixels[j + (gridHeight * i)].isOn) {
matrix_col.push(1.);
} else {
matrix_col.push(0);
}
}
matrix.push(matrix_col);
}
// Transpose the matrix...
return matrix[0].map((col, i) => matrix.map(row => row[i]));
}
let predict = (r) => {
tensor = tf.tensor(r, [28, 28, 1], 'float32');
tensor = tf.expandDims(tensor, 0);
return model.predict(tensor);
};
// Start...
createGrid();
loadNeuralNet();