Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Brain</title>
- <script src="brain.js-master/browser.min.js"></script>
- <style>
- body {
- background-color: #fff;
- font-family: Trebuchet MS, sans-serif;
- }
- body *{
- user-select: none;
- }
- @keyframes click{
- 0%{ background: #f0f0f0;}
- 100%{ background: #d0d0d0;}
- }
- #canv {
- position: absolute;
- top: 0;right: 0;bottom: 0;left: 0;
- margin: auto;
- background-color: white;
- box-shadow: 0 2.5px 10px #ddd;
- border-radius: 5px;
- }
- #buttons{
- display: block;
- position: fixed;
- width: max-content;
- height: max-content;
- padding: 15px;
- margin: 5px;
- background: #fff;
- border-radius: 10px;
- box-shadow: 0 2.5px 7.5px #ddd;
- top: 50px;
- right: 50px;
- text-align: center;
- }
- .button{
- display: block;
- position: relative;
- width: 150px;
- height: 20px;
- line-height: 20px;
- vertical-align: middle;
- text-align: center;
- margin: 5px auto;
- border-radius: 7.5px;
- box-shadow: 0 2.5px 5px #f0f0f0;
- padding: 5px 15px;
- transition: 0.25s ease;
- font-size: 18px;
- cursor: pointer;
- }
- .button:hover{
- background: #f0f0f0;
- box-shadow: 0 2.5px 12.5px #ddd;
- }
- .hr{
- display: block;
- position: relative;
- width: 90%;
- padding: 1.5px;
- margin: 10px auto;
- background: #ddd;
- }
- </style>
- </head>
- <body>
- <canvas id="canv" style="display: block;">Ваш браузер устарел, обновитесь.</canvas>
- <script>
- function DCanvas(el)
- {
- const ctx = el.getContext('2d');
- const pixel = 20;
- let is_mouse_down = false;
- canv.width = 750;
- canv.height = 750;
- 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.drawImage = function(image){
- const w = canv.width;
- const h = canv.height;
- ctx.drawImage(image, 0, 0, w, h);
- }
- 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( 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 last_object = localStorage.lo;
- // var file = new FileReader("txt/json_td.txt");
- // file.readAsText(file);
- // var json_td = file.readln();
- // let train_data = json_td ? JSON.parse(json_td) : [];
- let train_data = [];
- const d = new DCanvas(document.getElementById('canv'));
- var buttons = document.getElementsByClassName('button');
- function action(e){
- if( e == 'clear' )
- {
- d.clear();
- }
- if( e == 'add' )
- {
- vector = d.calculate(true);
- let new_object = prompt('Add', last_object).toLowerCase();
- //train
- last_object = new_object;
- if(new_object)
- train_data.push({
- input: vector,
- output: {[new_object]: 1}
- });
- }
- if( e == 'rec' )
- {
- net = new brain.NeuralNetwork();
- net.train(train_data, {log: true});
- vector = d.calculate()
- const result = brain.likely(vector, net);
- if(!confirm(result)){
- let true_object = prompt('What was it?').toLowerCase();
- if(true_object)
- train_data.push({
- input: vector,
- output: {[true_object]: 1}
- });
- last_object = true_object;
- }
- }
- }
- window.onbeforeunload = function(event){
- // var file = new File("txt/json_td.txt");
- // var td = JSON.stringify(train_data);
- // file.open("w");
- // file.write(td);
- // file.close();
- localStorage.lo = last_object;
- };
- window.onload = function(){
- // console.log(JSON.stringify(localStorage.td));
- };
- </script>
- <div id="buttons">
- <div class="button" onclick="action('clear');">Clear</div>
- <div class="button" onclick="action('add')">Add</div>
- <div class="hr"></div>
- <div class="button" onclick="document.getElementById('img_file').click()">Add image</div>
- <input type="file" style="display: none;" id="img_file" accept="image/png, image/jpg, image/gif" onchange="var img = new Image();
- img.onload = function(){ d.drawImage(img);}
- img.src = window.URL.createObjectURL(this.files[0]);
- ">
- <div class="hr"></div>
- <div class="button" onclick="action('rec')">Recognize</div>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement