-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
226 lines (200 loc) · 6.14 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<!doctype html>
<html lang="en">
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-H0NW5Z2MYC"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-H0NW5Z2MYC');
</script>
<title>Digit Recognition WebApp</title>
<meta name="description" content="Simple Machine Learning Model into an WebApp using TensorFlow.js">
<meta name="keywords" content="Machine Learning, TensorFlow.js">
<meta name="author" content="Carlos Aguayo">
<link href="https://fonts.googleapis.com/css?family=Bellota&display=swap" rel="stylesheet">
<style>
body {
touch-action: none;
/*https://developer.mozilla.org/en-US/docs/Web/CSS/touch-action*/
font-family: "Roboto";
background-image: linear-gradient(141deg, #9fb8ad 0%, #1fc8db 51%, #2cb5e8 75%);
}
h1 {
margin: 20px;
font-size: 70px;
text-align: center;
color: #3700B3;
font-family: 'Bellota', cursive;
}
#paint {
border: 5px solid red;
margin: auto;
}
#predicted {
font-size: 60px;
margin-top: 60px;
color: #a00037;
text-align: center;
font-family: 'Bellota', cursive;
}
#number {
border: 3px solid black;
margin: auto;
margin-top: 30px;
text-align: center;
vertical-align: middle;
}
#clear {
margin: auto;
margin-top: 70px;
padding: 30px;
text-align: center;
background-color: #00b686;
/* border: none;
text-decoration: none; */
outline-style: none;
border-radius: 25px;
}
</style>
</head>
<body>
<!--<script type="text/javascript" src="http://livejs.com/live.js"></script>-->
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js"></script>
<h1>Digit Recognition</h1>
<div id="paint">
<canvas id="myCanvas"></canvas>
<center>
<h4
style="color: rgb(245, 239, 239);font-family: 'Bellota';background-color: #0d47a1; border-radius: 15px;padding: 4px;">
-by
Ayon Roy</h4>
</center>
</div>
<div id="predicted">
Recognized digit
<div id="number"></div>
<button id="clear">Clear</button>
</div>
<script>
var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
if (isMobile) {
$('#paint').css({
'width': '60%'
});
$('#number').css({
'width': '30%',
'font-size': '240px'
});
$('#clear').css({
'font-size': '50px'
});
} else {
$('#paint').css({
'width': '300px'
});
$('#number').css({
'width': '150px',
'font-size': '120px'
});
$('#clear').css({
'font-size': '35px'
});
}
var cw = $('#paint').width();
$('#paint').css({
'height': cw + 'px'
});
cw = $('#number').width();
$('#number').css({
'height': cw + 'px'
});
// From https://www.html5canvastutorials.com/labs/html5-canvas-paint-application/
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var compuetedStyle = getComputedStyle(document.getElementById('paint'));
canvas.width = parseInt(compuetedStyle.getPropertyValue('width'));
canvas.height = parseInt(compuetedStyle.getPropertyValue('height'));
var mouse = {
x: 0,
y: 0
};
canvas.addEventListener('mousemove', function (e) {
mouse.x = e.pageX - this.offsetLeft;
mouse.y = e.pageY - this.offsetTop;
}, false);
context.lineWidth = isMobile ? 60 : 25;
context.lineJoin = 'round';
context.lineCap = 'round';
context.strokeStyle = '#0000FF';
canvas.addEventListener('mousedown', function (e) {
context.moveTo(mouse.x, mouse.y);
context.beginPath();
canvas.addEventListener('mousemove', onPaint, false);
}, false);
canvas.addEventListener('mouseup', function () {
$('#number').html('<img id="spinner" src="spinner.gif"/>');
canvas.removeEventListener('mousemove', onPaint, false);
var img = new Image();
img.onload = function () {
context.drawImage(img, 0, 0, 28, 28);
data = context.getImageData(0, 0, 28, 28).data;
var input = [];
for (var i = 0; i < data.length; i += 4) {
input.push(data[i + 2] / 255);
}
predict(input);
};
img.src = canvas.toDataURL('image/png');
}, false);
var onPaint = function () {
context.lineTo(mouse.x, mouse.y);
context.stroke();
};
tf.loadLayersModel('model/model.json').then(function (model) {
window.model = model;
});
// http://bencentra.com/code/2014/12/05/html5-canvas-touch-events.html
// Set up touch events for mobile, etc
canvas.addEventListener('touchstart', function (e) {
var touch = e.touches[0];
canvas.dispatchEvent(new MouseEvent('mousedown', {
clientX: touch.clientX,
clientY: touch.clientY
}));
}, false);
canvas.addEventListener('touchend', function (e) {
canvas.dispatchEvent(new MouseEvent('mouseup', {}));
}, false);
canvas.addEventListener('touchmove', function (e) {
var touch = e.touches[0];
canvas.dispatchEvent(new MouseEvent('mousemove', {
clientX: touch.clientX,
clientY: touch.clientY
}));
}, false);
var predict = function (input) {
if (window.model) {
window.model.predict([tf.tensor(input).reshape([1, 28, 28, 1])]).array().then(function (scores) {
scores = scores[0];
predicted = scores.indexOf(Math.max(...scores));
$('#number').html(predicted);
});
} else {
// The model takes a bit to load, if we are too fast, wait
setTimeout(function () {
predict(input)
}, 50);
}
}
$('#clear').click(function () {
context.clearRect(0, 0, canvas.width, canvas.height);
$('#number').html('');
});
</script>
</body>
</html>