-
Notifications
You must be signed in to change notification settings - Fork 1
/
drawScript.js
129 lines (110 loc) · 3.35 KB
/
drawScript.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
var img_array = [];
var MODEL_PATH = "https://raw.githubusercontent.com/piyush-kgp/Digit-Recognition-with-TFJS/master/tfjs_target_dir/model.json";
var canvas, ctx, flag = false,
prevX = 0,
currX = 0,
prevY = 0,
currY = 0,
dot_flag = false;
var x = "black",
// y = 1;
y = 10;
function init() {
canvas = document.getElementById('can');
ctx = canvas.getContext("2d");
w = canvas.width;
h = canvas.height;
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 draw() {
ctx.beginPath();
ctx.moveTo(prevX, prevY);
ctx.lineTo(currX, currY);
ctx.strokeStyle = x;
ctx.lineWidth = y;
ctx.stroke();
ctx.closePath();
console.log(currX, currY);
img_array.push([currX, currY]);
}
function erase() {
ctx.clearRect(0, 0, w, h);
var img_array = [];
}
async function save() {
// var dataURL = canvas.toDataURL();
// // console.log('base64', dataURL);
// var link = document.createElement('a');
// link.innerHTML = 'download image';
// link.addEventListener('click', function(ev) {
// link.href = dataURL;
// link.download = "mypainting.png";
// }, false);
// document.body.appendChild(link);
// console.log(img_array);
// let src = cv.imread("can");
// console.log(src);
// console.log('image width: ' + src.cols + '\n' +
// 'image height: ' + src.rows + '\n' +
// 'image size: ' + src.size().width + '*' + src.size().height + '\n' +
// 'image depth: ' + src.depth() + '\n' +
// 'image channels ' + src.channels() + '\n' +
// 'image type: ' + src.type() + '\n');
var fin_array = new Float32Array(784);
for (var i = 0; i < img_array.length; i++) {
var point = img_array[i];
var x = point[0];
var y = point[1];
var pos = 28*Math.floor(y/10) + Math.floor(x/10)
fin_array[pos]=1;
}
console.log(fin_array);
// document.getElementById('out').innerHTML = fin_array;
const model = await tf.loadModel(MODEL_PATH);
const X = tf.tensor2d(fin_array, [1, 784]);
y = model.predict(X);
pred = y.argMax(1);
pred = Array.from(pred.dataSync());
console.log('Predicted=', pred[0]);
document.getElementById('result').innerHTML = 'Predicted Digit = '+pred;
}
function findxy(res, e) {
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();
ctx.fillStyle = x;
ctx.fillRect(currX, currY, 2, 2);
ctx.closePath();
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;
draw();
}
}
}