-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
139 lines (122 loc) · 3.82 KB
/
index.html
File metadata and controls
139 lines (122 loc) · 3.82 KB
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
<!DOCTYPE html>
<head>
<body style="background-color: rgb(35, 68, 100)">
<title>Digit Recognition</title>
<style>
body {
touch-action: none;
font-family: "Times New Roman";
}
h1 {
margin: 25px;
font-size: 55px;
text-align: center;
}
#paint {
border:3px solid #ADEFD1FF;
margin: auto;
}
#predicted {
font-size: 30px;
margin-top: 10px;
text-align: center;
}
#number {
border: 3px solid #ADEFD1FF;
margin: auto;
margin-top: 10px;
text-align: center;
vertical-align: middle;
}
#clear {
margin-top: 10px;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.5.2/dist/tf.min.js"></script>
<h1 style="color:rgb(252, 252, 252);">Digit Recognition</h1>
<div id="paint">
<canvas id="myCanvas"></canvas>
</div>
<div id="predicted">
<p style="color:rgb(255, 255, 255)";>
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) {
} else {
$('#paint').css({'width': '250px'});
$('#number').css({'width': '100px', 'font-size': '90px'});
$('#clear').css({'font-size': '20px'});
}
var cw = $('#paint').width();
$('#paint').css({'height': cw + 'px'});
cw = $('#number').width();
$('#number').css({'height': cw + 'px'});
// drawing
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);
var onPaint = function() {
context.lineTo(mouse.x, mouse.y);
context.stroke();
};
context.lineWidth = '13';
context.lineJoin = 'round';
context.lineCap = 'round';
context.strokeStyle = '#FFFFFF';
canvas.addEventListener('mousedown', function(e) {
context.moveTo(mouse.x, mouse.y);
context.beginPath();
canvas.addEventListener('mousemove', onPaint, false);
}, false);
canvas.addEventListener('mouseup', function() {
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);
tf.loadLayersModel('model/model.json').then(function(model) {
window.model = model;
});
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, wait
setTimeout(function(){predict(input)}, 50);
}
}
$('#clear').click(function(){
context.clearRect(0, 0, canvas.width, canvas.height);
$('#number').html('');
});
</script>
</body>
</html>