-
Notifications
You must be signed in to change notification settings - Fork 2
/
tensor.html
176 lines (154 loc) · 4.83 KB
/
tensor.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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>手写字体识别</title>
<style type="text/css">
*{ margin:0; padding:0;}
body{ background: #fff; padding: 20px; }
h1 {
text-align: center;
}
#canvas {
margin: 0 auto;
display: block;
position: absolute;
left: 400px;
top: 200px;
background-color: #fff;
border: 1px solid;
}
#canvas::before {
content: '手写区:';
display: block;
}
#canvasSmall {
background-color: #fff;
}
#canvasImg {
position: absolute;
top: 220px;
left: 800px;
display: block;
border: 1px solid #ccc;
-webkit-transform: scale(2.4);
}
.text1 {
position: absolute;
left: 400px;
top: 123px;
}
.text2 {
position: absolute;
left: 800px;
top: 123px;
}
.text3 {
position: absolute;
left: 1100px;
top: 123px;
}
.opera {
position: absolute;
top: 576px;
left: 456px;
}
.opera button {
width: 100px;
height: 50px;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"> </script>
</head>
<body>
<h1>调度字体</h1>
<p class="text1">手写区:</p>
<canvas id="canvas" width="300" height="300"></canvas>
<p class="text2">预览区:</p>
<img id="canvasImg" style="width:28px;height:28px;" src="data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg=="/>
<div class="opera">
<button id="translate">预测</button>
<button id="clear">清除</button>
</div>
<p class="text3">结果区:</p>
<div style="color: red;
position: absolute;
left: 1200px;
top: 200px;
font-size: 21px;" id="myw"></div>
<script>
async function loadModel(){
window.pmodel = await tf.loadModel('./model/model.json');
}
loadModel();
</script>
<script>
var ctx;
var x;
var y;
var a;
var dragFlag = false;
function circle(x,y) {
ctx.beginPath();
ctx.arc(x, y, 9, 0, Math.PI*2, true);
ctx.closePath();
ctx.fillStyle = "#000000";
ctx.fill();
}
function init() {
var canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
document.getElementById("translate").addEventListener("click",translate);
document.getElementById("clear").addEventListener("click",clear);
canvas.addEventListener("mousemove",mouseMove);
canvas.addEventListener("mousedown",mousedown);
canvas.addEventListener("mouseup",mouseup);
ctx.fillStyle = '#ffffff';
ctx.fillRect(0,0,300,300);
}
function clear(){
ctx.fillStyle = '#ffffff';
document.getElementById("myw").innerHTML = '';
ctx.fillRect(0,0,300,300);
document.getElementById("canvasImg").src = 'data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==';
}
function translate(e){
// var imageData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
// let row = 0;
// let column = 0;
// let pixels = [[]];
// const data = imageData.data;
// var a2 = document.getElementById("a2").getContext('2d');
// a2.drawImage(ctx.canvas, 0, 0, 28, 28);
// var f = a2.getImageData(0, 0, 28, 28);
let img = tf.fromPixels(document.getElementById("canvasImg"), 1);
img = img.reshape([1,28, 28, 1]);
img = tf.cast(img, 'float32');
let g = window.pmodel.predict(img);
var b = g.argMax(1);
console.log(g.dataSync());
document.getElementById('myw').innerText = b.dataSync()[0];
}
function mousedown(){
dragFlag = true;
}
function mouseup(){
dragFlag = false;
document.getElementById("canvasImg").src = ctx.canvas.toDataURL();
}
function mouseMove (e){
var rect = e.currentTarget.getBoundingClientRect();
var gravityPoint = {
x: e.clientX - rect.left,
y: e.clientY - rect.top
};
x = gravityPoint.x;
y = gravityPoint.y;
if (dragFlag) {
circle(x,y);
}
}
init();
</script>
</body>
</html>