-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
208 lines (175 loc) · 8.22 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
199
200
201
202
203
204
205
206
207
208
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/gif" sizes="32x32" href="https://informacionimagenes.net/wp-content/uploads/2019/10/abecedario-con-letras-may_1317550144_img.gif">
<link rel="icon" type="image/gif" sizes="16x16" href="https://informacionimagenes.net/wp-content/uploads/2019/10/abecedario-con-letras-may_1317550144_img.gif">
<title>Numbers</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<style>
#result {
font-weight: bold;
font-size: 6rem;
text-align: center;
}
.canvas-container {
margin: 0 auto;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<main>
<div class="px-4 py-2 my-2 text-center border-bottom">
<img class="d-block mx-auto mb-2" src="https://informacionimagenes.net/wp-content/uploads/2019/10/abecedario-con-letras-may_1317550144_img.gif" alt="" width="80" height="80">
<h1 class="display-5 fw-bold">Handwritten numbers</h1>
<div class="col-lg-6 mx-auto">
<p class="lead mb-0">Handwritten numbers predictions based on TensorFlow.js</p>
</div>
</div>
<div class="b-example-divider"></div>
<div class="container mt-5">
<div class="row">
<div class="col-12 col-md-4 offset-md-4">
<div id="canvas-container">
<div><i>Try to drawing the numbers clearly to have better predictions</i></div>
<canvas id="bigcanvas" width="200" height="200"></canvas>
<canvas id="smallcanvas" width="28" height="28" style="display: none"></canvas>
</div>
<div class="text-center mt-3">
<button class="btn btn-primary" id="clear" onclick="clear()">Clear</button>
<button class="btn btn-success" id="predict" onclick="predict()">Predict</button>
<div id="result"></div>
</div>
</div>
</div>
</div>
<div class="b-example-divider"></div>
<div class="bg-dark text-secondary mt-5 px-4 py-2 text-center">
<div class="py-5">
<div class="col-lg-6 mx-auto">
</div>
</div>
</div>
<div class="b-example-divider mb-0"></div>
</main>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js"></script>
<script src="fabric.min.js"/></script>
<script src="drawing.js"></script>
<script type="text/javascript">
var model = null;
var canvas = document.getElementById("bigcanvas");
var ctx1 = canvas.getContext("2d");
var smallcanvas = document.getElementById("smallcanvas");
var ctx2= smallcanvas.getContext("2d");
function clear() {
ctx1.clearRect(0, 0, canvas.width, canvas.height);
drawingcanvas.clear();
}
function predict() {
// converting canvas to 28x28 version
resample_single(canvas, 28, 28, smallcanvas);
var imgData = ctx2.getImageData(0,0,28,28);
var arr = []; //completely array
var arr28 = []; //when it gets to 28th place 'arr' is the new indice
for (var p=0, i=0; p < imgData.data.length; p+=4) {
var value = imgData.data[p+3]/255;
arr28.push([value]); //Adding to arr28 and normalize to 0-1.Beside this, there is an array with the indice 0 again
if (arr28.length == 28) {
arr.push(arr28);
arr28 = [];
}
}
arr = [arr]; //putting an array inside other
var tensor4 = tf.tensor4d(arr);
var results = model.predict(tensor4).dataSync();
var indiceBigger = results.indexOf(Math.max.apply(null, results));
console.log("Prediction", indiceBigger);
document.getElementById("result").innerHTML = indiceBigger;
}
function resample_single(canvas, width, height, resize_canvas) {
var width_source = canvas.width;
var height_source = canvas.height;
width = Math.round(width);
height = Math.round(height);
var ratio_w = width_source / width;
var ratio_h = height_source / height;
var ratio_w_half = Math.ceil(ratio_w / 2);
var ratio_h_half = Math.ceil(ratio_h / 2);
var ctx = canvas.getContext("2d");
var ctx2 = resize_canvas.getContext("2d");
var img = ctx.getImageData(0, 0, width_source, height_source);
var img2 = ctx2.createImageData(width, height);
var data = img.data;
var data2 = img2.data;
for (var j = 0; j < height; j++) {
for (var i = 0; i < width; i++) {
var x2 = (i + j * width) * 4;
var weight = 0;
var weights = 0;
var weights_alpha = 0;
var gx_r = 0;
var gx_g = 0;
var gx_b = 0;
var gx_a = 0;
var center_y = (j + 0.5) * ratio_h;
var yy_start = Math.floor(j * ratio_h);
var yy_stop = Math.ceil((j + 1) * ratio_h);
for (var yy = yy_start; yy < yy_stop; yy++) {
var dy = Math.abs(center_y - (yy + 0.5)) / ratio_h_half;
var center_x = (i + 0.5) * ratio_w;
var w0 = dy * dy; //pre-calc part of w
var xx_start = Math.floor(i * ratio_w);
var xx_stop = Math.ceil((i + 1) * ratio_w);
for (var xx = xx_start; xx < xx_stop; xx++) {
var dx = Math.abs(center_x - (xx + 0.5)) / ratio_w_half;
var w = Math.sqrt(w0 + dx * dx);
if (w >= 1) {
//pixel too far
continue;
}
//hermite filter
weight = 2 * w * w * w - 3 * w * w + 1;
var pos_x = 4 * (xx + yy * width_source);
//alpha
gx_a += weight * data[pos_x + 3];
weights_alpha += weight;
//colors
if (data[pos_x + 3] < 255)
weight = weight * data[pos_x + 3] / 250;
gx_r += weight * data[pos_x];
gx_g += weight * data[pos_x + 1];
gx_b += weight * data[pos_x + 2];
weights += weight;
}
}
data2[x2] = gx_r / weights;
data2[x2 + 1] = gx_g / weights;
data2[x2 + 2] = gx_b / weights;
data2[x2 + 3] = gx_a / weights_alpha;
}
}
for (var p=0; p < data2.length; p += 4) {
var grey = data2[p]; //B&W
if (grey < 100) {
grey = 0;
} else {
grey = 255;
}
data2[p] = grey;
data2[p+1] = grey;
data2[p+2] = grey;
}
ctx2.putImageData(img2, 0, 0);
}
//Loading the model
(async () => {
console.log("Loding model...");
model = await tf.loadLayersModel("model.json");
console.log("Model loaded...");
})();
</script>
</body>
</html>