Skip to content

Commit

Permalink
Adjust to 4 spaces for tabs overall.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marina Samuel committed Apr 20, 2014
1 parent bed9a4d commit 9bd2b54
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 222 deletions.
152 changes: 76 additions & 76 deletions ocr/ocr.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,115 +6,115 @@ var trainArray = [];
var trainingRequestCount = 0;

function onLoadFunction() {
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
resetCanvas();

ctx.fillSquare = fillSquare;
canvas.onmousemove = onMouseMove;
canvas.onmousedown = onMouseDown;
canvas.onmouseup = onMouseUp;
canvas = document.getElementById('canvas');
ctx = canvas.getContext('2d');
resetCanvas();

ctx.fillSquare = fillSquare;
canvas.onmousemove = onMouseMove;
canvas.onmousedown = onMouseDown;
canvas.onmouseup = onMouseUp;
}

function resetCanvas() {
data = [];
ctx.fillStyle = '#000000';
ctx.fillRect(0, 0, 200, 200);
var matrixSize = 400;
while (matrixSize--) data.push(0);
drawGrid();
data = [];
ctx.fillStyle = '#000000';
ctx.fillRect(0, 0, 200, 200);
var matrixSize = 400;
while (matrixSize--) data.push(0);
drawGrid();
}


function drawGrid() {
for (var x = 10, y = 10; x < 200; x += 10, y += 10) {
ctx.strokeStyle = '#0000ff';
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, 200);
ctx.stroke();

ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(200, y);
ctx.stroke();
}
for (var x = 10, y = 10; x < 200; x += 10, y += 10) {
ctx.strokeStyle = '#0000ff';
ctx.beginPath();
ctx.moveTo(x, 0);
ctx.lineTo(x, 200);
ctx.stroke();

ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(200, y);
ctx.stroke();
}
}

function onMouseMove(e) {
if (!canvas.isDrawing) {
return;
}
ctx.fillSquare(e.clientX - this.offsetLeft, e.clientY - this.offsetTop);
if (!canvas.isDrawing) {
return;
}
ctx.fillSquare(e.clientX - this.offsetLeft, e.clientY - this.offsetTop);
}

function onMouseDown(e) {
canvas.isDrawing = true;
ctx.fillSquare(e.clientX - this.offsetLeft, e.clientY - this.offsetTop);
canvas.isDrawing = true;
ctx.fillSquare(e.clientX - this.offsetLeft, e.clientY - this.offsetTop);
}

function onMouseUp(e) {
canvas.isDrawing = false;
canvas.isDrawing = false;
}

function fillSquare(x, y) {
var xPixel = Math.floor(x / 10);
var yPixel = Math.floor(y / 10);
data[((xPixel - 1) * 20 + yPixel) - 1] = 1;
var xPixel = Math.floor(x / 10);
var yPixel = Math.floor(y / 10);
data[((xPixel - 1) * 20 + yPixel) - 1] = 1;

this.fillStyle = '#ffffff';
this.fillRect(xPixel * 10, yPixel * 10, 10, 10);
this.fillStyle = '#ffffff';
this.fillRect(xPixel * 10, yPixel * 10, 10, 10);
}

function train() {
var digitVal = document.getElementById("digit").value;
if (!digitVal) {
alert("Please type a digit value in order to train the network");
return;
}
trainArray.push({y0: data, label: parseInt(digitVal)});
trainingRequestCount++;
// Time to send a training batch to the server.
if (trainingRequestCount == BATCH_SIZE) {
alert("Sending training data to server...");
var json = {
trainArray: trainArray,
train: true
};

sendData(json);
trainingRequestCount = 0;
trainArray = [];
}
var digitVal = document.getElementById("digit").value;
if (!digitVal) {
alert("Please type a digit value in order to train the network");
return;
}
trainArray.push({y0: data, label: parseInt(digitVal)});
trainingRequestCount++;

// Time to send a training batch to the server.
if (trainingRequestCount == BATCH_SIZE) {
alert("Sending training data to server...");
var json = {
trainArray: trainArray,
train: true
};

sendData(json);
trainingRequestCount = 0;
trainArray = [];
}
}

function test() {
var json = {
image: data,
train: false
};
sendData(json);
var json = {
image: data,
train: false
};
sendData(json);
}

function receiveResponse() {
var responseJSON = JSON.parse(this.responseText);
if (this.responseText && responseJSON.type == "test") {
alert("The neural network predicts you wrote a \'" + responseJSON.result + '\'');
}
var responseJSON = JSON.parse(this.responseText);
if (this.responseText && responseJSON.type == "test") {
alert("The neural network predicts you wrote a \'" + responseJSON.result + '\'');
}
}

function onError(e) {
alert("Error occurred while connecting to server: " + e.target.statusText);
alert("Error occurred while connecting to server: " + e.target.statusText);
}

function sendData(json) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open('POST', "http://localhost:80", false);
xmlHttp.onload = receiveResponse;
xmlHttp.onerror = onError;
var msg = JSON.stringify(json);
xmlHttp.setRequestHeader('Content-length', msg.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(msg);
var xmlHttp = new XMLHttpRequest();
xmlHttp.open('POST', "http://localhost:80", false);
xmlHttp.onload = receiveResponse;
xmlHttp.onerror = onError;
var msg = JSON.stringify(json);
xmlHttp.setRequestHeader('Content-length', msg.length);
xmlHttp.setRequestHeader("Connection", "close");
xmlHttp.send(msg);
}
Loading

0 comments on commit 9bd2b54

Please sign in to comment.