-
Notifications
You must be signed in to change notification settings - Fork 0
/
Neuro.html
176 lines (149 loc) · 3.71 KB
/
Neuro.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>Simple NeuralNetwork by JS</title>
<script src="brain.js/browser.min.js"></script>
<style>
body {
background-color: #333;
}
#canv {
position: absolute;
top: 0; right: 0; left: 0; bottom: 0;
margin: auto;
background-color: white;
}
</style>
</head>
<body>
<canvas id="canv" style="display:block;">The version of your browser was released long ago. Please, upload the up-to-date one!</canvas>
<script>
function DCanvas(el) {
const ctx = el.getContext("2d");
const pixel = 20;
let is_mouse_down = false;
canv.width = 500;
canv.height = 500;
this.drawLine = function(x1, y1, x2, y2, color = "gray") {
ctx.beginPath();
ctx.strokeStyle = color;
ctx.lineJoin = "miter";
ctx.lineWidth = 1;
ctx.moveTo(x1, y1);
ctx.lineTo(x2, y2);
ctx.stroke();
}
this.drawCell = function(x, y, w, h) {
ctx.fillStyle = "blue";
ctx.strokeStyle = "blue";
ctx.lineJoin = "miter";
ctx.lineWidth = 1;
ctx.rect(x, y, w, h);
ctx.fill();
}
this.clear = function() {
ctx.clearRect(0, 0, canv.width, canv.height);
}
this.drawGrid = function() {
const w = canv.width;
const h = canv.height;
const p = w / pixel;
const xStep = w / p;
const yStep = h / p;
for(let x = 0; x < w; x += xStep) {
this.drawLine(x, 0, x, h);
}
for(let y = 0; y < h; y += yStep) {
this.drawLine(0, y, w, y);
}
}
this.calculate = function(draw = false) {
const w = canv.width;
const h = canv.height;
const p = w / pixel;
const xStep = w / p;
const yStep = h / p;
const vector = [];
let __draw = [];
for (let x = 0; x < w; x += xStep) {
for (let y = 0; y < h; y += yStep) {
const data = ctx.getImageData(x, y, xStep, yStep);
let nonEmptyPixelsCount = 0;
for (let i = 0; i < data.data.length; i += 10) {
const isEmpty = data.data[i] === 0;
if (!isEmpty) {
nonEmptyPixelsCount += 1;
}
}
if(nonEmptyPixelsCount > 1 && draw) {
__draw.push([x, y, xStep, yStep]);
}
vector.push(nonEmptyPixelsCount > 1 ? 1 : 0);
}
}
if (draw) {
this.clear();
this.drawGrid();
for(_d in __draw) {
this.drawCell(__draw[_d][0], __draw[_d][1], __draw[_d][2], __draw[_d][3]);
}
}
return vector;
}
el.addEventListener("mousedown", function(e) {
is_mouse_down = true;
ctx.beginPath();
});
el.addEventListener("mouseup", function(e) {
is_mouse_down = false;
});
el.addEventListener("mousemove", function(e) {
if (is_mouse_down) {
ctx.fillStyle = "red";
ctx.strokeStyle = "red";
ctx.lineWidth = pixel;
ctx.lineTo(e.offsetX, e.offsetY);
ctx.stroke();
ctx.beginPath();
ctx.arc(e.offsetX, e.offsetY, pixel / 2, 0, Math.PI * 2);
ctx.fill();
ctx.beginPath();
ctx.moveTo(e.offsetX, e.offsetY);
}
});
}
let vector = [];
let net = null;
let train_data = [];
const d = new DCanvas(document.querySelector("#canv"));
//d.drawGrid();
document.addEventListener("keypress", function(e) {
if (e.key.toLowerCase() == "c") {
d.clear();
}
if (e.key.toLowerCase() == "v") {
vector = d.calculate(true);
//train
if (confirm("Positive?")) {
train_data.push({
input: vector,
output: {positive: 1}
});
} else {
train_data.push({
input: vector,
output: {negative: 1}
});
}
}
if (e.key.toLowerCase() == "b") {
net = new brain.NeuralNetwork();
net.train(train_data, {log: true});
const result = brain.likely(d.calculate(), net);
alert(result);
}
});
</script>
</body>
</html>