-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhwr_nn.js
91 lines (79 loc) · 3.04 KB
/
hwr_nn.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
function formatGrayscaleData(data, width) {
let result = '';
for (let i = 0; i < data.length; i++) {
result += data[i].toString().padStart(3, ' ') + ' ';
if ((i + 1) % width === 0) {
result += '\n';
}
}
return result;
}
document.addEventListener('DOMContentLoaded',()=>{
var o=false;
var largeCanvas=document.getElementById("Lcanva");
var smallCanvas=document.getElementById("Scanva");
var smallCtx=smallCanvas.getContext('2d');
var progress=document.getElementsByClassName('prediction_rate')[0];
smallCtx.fillStyle = "black";
smallCtx.fillRect(0, 0, largeCanvas.width, largeCanvas.height);
var largeCtx=largeCanvas.getContext('2d');
let isDrawing = false;
largeCanvas.addEventListener('mousedown', () => {
isDrawing = true;
});
largeCanvas.addEventListener('mouseup', () => {
isDrawing = false;
});
largeCanvas.addEventListener('mousemove', (e) => {
if (isDrawing) {
const rect = largeCanvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
largeCtx.fillStyle = "white";
largeCtx.beginPath();
largeCtx.arc(x, y, 10, 0, Math.PI * 2); // Increased brush size
largeCtx.fill();
}
});
var btn=document.getElementsByClassName("predictBtn")[0];
var output = document.getElementsByClassName('output');
var ptxt=document.getElementsByClassName('ptxt')[0];
var span=document.getElementsByClassName('pspan')[0];
var num=0;
var pred=[];
console.log(ptxt);
btn.addEventListener('click',()=>{
if(!o)
{
smallCtx.drawImage(largeCanvas, 0, 0, 28, 28);
const imageData = smallCtx.getImageData(0, 0, 28, 28);
const data = imageData.data;
const grayscaleData = [];
for (let i = 0; i < data.length; i += 4) {
const r = data[i];
const g = data[i + 1];
const b = data[i + 2];
const grayscale = Math.round(0.299 * r + 0.587 * g + 0.114 * b);
grayscaleData.push(grayscale/255.0);
}
pred=feedforward(grayscaleData);
num= (Math.max(...pred));
progress.style.width=((num).toPrecision(4)*100).toString()+"%";
ptxt.innerHTML=(num.toPrecision(2)*100)+"%";
output[pred.indexOf(num)].style.backgroundColor="#3a5a40";
btn.className="closeBtn";
span.innerHTML="clear";
o=true;
}
else{
largeCtx.clearRect(0, 0, largeCanvas.width, largeCanvas.height);
smallCtx.clearRect(0, 0, largeCanvas.width, largeCanvas.height);
smallCtx.fillStyle = "black";
smallCtx.fillRect(0, 0, largeCanvas.width, largeCanvas.height);
btn.className="predictBtn";
span.innerHTML="predict";
output[pred.indexOf(num)].style.backgroundColor="#fafafa";
o=false;
}
})
})