-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
163 lines (140 loc) · 4.52 KB
/
script.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
let write = document.getElementById("write");
let predict = document.getElementById("predict");
let erase = document.getElementById("erase");
let canvas = document.getElementById("can");
let ctx = canvas.getContext("2d");
let w = canvas.width;
let h = canvas.height;
const canvasLineJoin = "round";
const canvasStrokeStyle = "white";
const canvasLineWidth = 12;
ctx.strokeStyle = canvasStrokeStyle;
ctx.lineJoin = canvasLineJoin;
ctx.lineWidth = canvasLineWidth;
ctx.fillStyle = "white";
// ctx.lineJoin = canvasLineJoin;
let body = document.getElementsByTagName("body")[0];
ctx.filter = 'blur(1px)';
var clickX = new Array();
var clickY = new Array();
var clickD = new Array();
let flag = false,
prevX = 0,
currX = 0,
prevY = 0,
currY = 0,
dot_flag = false;
let x = "black",
y = 20;
body.onload = function() {
ctx.fillStyle = "black";
ctx.fillRect(0, 0, w, h);
canvas.addEventListener("mousemove", function (e) {
findxy('move', e)
}, false);
canvas.addEventListener("mousedown", function (e) {
findxy('down', e)
}, false);
canvas.addEventListener("mouseup", function (e) {
findxy('up', e)
}, false);
canvas.addEventListener("mouseout", function (e) {
findxy('out', e)
}, false);
}
function findxy(res, e) {
// console.log("in");
// debugger
if (res == 'down') {
prevX = currX;
prevY = currY;
currX = e.clientX - canvas.offsetLeft;
currY = e.clientY - canvas.offsetTop;
flag = true;
dot_flag = true;
if (dot_flag) {
// ctx.beginPath();
clickX.push(currX);
clickY.push(currY);
clickD.push(true);
// ctx.moveTo(prevX, prevY);
// ctx.lineTo(currX, currY);
// ctx.stroke();
dot_flag = false;
}
}
if (res == 'up' || res == "out") {
flag = false;
}
if (res == 'move') {
if (flag) {
prevX = currX;
prevY = currY;
currX = e.clientX - canvas.offsetLeft;
currY = e.clientY - canvas.offsetTop;
ctx.beginPath();
clickX.push(currX);
clickY.push(currY);
clickD.push(true);
ctx.moveTo(prevX, prevY);
ctx.lineTo(currX, currY);
ctx.stroke();
dot_flag = false;
}
}
}
function deleted() {
var m = 1;
if (m) {
ctx.clearRect(0, 0, w, h);
ctx.fillStyle = "black";
ctx.fillRect(0, 0, 200, 200);
var clickX = new Array();
var clickY = new Array();
var clickD = new Array();
write.innerHTML="";
// document.getElementById("can.style.display = "none";
}
}
function boundingBox() {
var minX = Math.min.apply(Math, clickX) - 20;
var maxX = Math.max.apply(Math, clickX) + 20;
var minY = Math.min.apply(Math, clickY) - 20;
var maxY = Math.max.apply(Math, clickY) + 20;
var tempCanvas = document.createElement("canvas"),
tCtx = tempCanvas.getContext("2d");
tempCanvas.width = maxX - minX;
tempCanvas.height = maxY - minY;
tCtx.drawImage(canvas, minX, minY, maxX - minX, maxY - minY, 0, 0, maxX - minX, maxY - minY);
// var imgBox = document.getElementById("canvas_image");
// imgBox.src = tempCanvas.toDataURL();
return tempCanvas;
}
function preprocessCanvas(image) {
// if model is not available, send the tensor with expanded dimensions
// resize the input image to digitrecognizermlp's target size of (1, 28, 28, 1)
let tensor = tf.browser.fromPixels(image)
.resizeNearestNeighbor([28, 28])
.mean(2)
.expandDims(2)
.expandDims()
.toFloat();
console.log(tensor.shape);
return tensor.div(255.0);
// else throw an error
}
async function prediction(){
const model = await tf.loadLayersModel('localstorage://my-model');
// canvas.toDataURL('image/png');
let croppedCanvas = boundingBox();
let tensor = preprocessCanvas(croppedCanvas);
let preds = model.predict(tensor);
preds.argMax(1).print();
let tensorData = preds.argMax(1).dataSync();
let result = tensorData[0];
write.innerHTML=result;
// console.log(result);
// textxs.dispose();
}
erase.addEventListener("click", deleted);
predict.addEventListener("click", prediction);