<script>
//Initialize canvas
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
let isDrawing = false;
let lastX = 0;
let lastY = 0;
context.lineJoin = 'round';
context.lineCap = 'round';
context.lineWidth = 20;
//Training Data
let trainingData = [];
//Listening to mouse events
canvas.addEventListener('mousedown', (e) => {
isDrawing = true;
[lastX, lastY] = [e.offsetX, e.offsetY];
});
canvas.addEventListener('mousemove', (e) => {
if (!isDrawing) return;
context.beginPath();
context.moveTo(lastX, lastY);
context.lineTo(e.offsetX, e.offsetY);
context.stroke();
[lastX, lastY] = [e.offsetX, e.offsetY];
});
canvas.addEventListener('mouseup', () => {
isDrawing = false;
//convert canvas drawing to binary array for neural network
let imageData = context.getImageData(0, 0, canvas.width, canvas.height);
let data = imageData.data;
let pixels = [];
for (let i = 0; i < data.length; i += 4) {
let brightness = data[i] / 255;
pixels.push(brightness);
}
//add input data and output label (digit drawn) to training data array
let digit = prompt("What digit did you draw?");
if (digit >= 0 && digit <= 9) {
let output = new Array(10).fill(0);
output[digit] = 1;
trainingData.push({input: pixels, output: output});
} else {
alert("Please enter a digit between 0 and 9.");
}
});
canvas.addEventListener('mouseout', () => {
isDrawing = false;
});
//Neural Network Functions
const net = new brain.NeuralNetwork({ hiddenLayers: [784, 256], activation: 'sigmoid' });
function train() {
//code to train the neural network based on canvas input
net.train(trainingData, {
iterations: 2000,
log: true,
errorThresh: 0.005
});
}
function test() {
//code to test the neural network based on canvas input
let imageData = context.getImageData(0, 0, canvas.width, canvas.height);
let data = imageData.data;
let pixels = [];
for (let i = 0; i < data.length; i += 4) {
let brightness = data[i] / 255;
pixels.push(brightness);
}
let result = net.run(pixels);
let max = 0;
let digit = -1;
for (let i = 0; i < result.length; i++) {
if (result[i] > max) {
max = result[i];
digit = i;
}
}
alert("The neural network predicts the digit as: " + digit);
}
function save() {
//code to save the training data as a file
let dataToSave = JSON.stringify(trainingData);
let filename = "trainingData.json";
let file = new Blob([dataToSave], {type: 'text/plain'});
let a = document.createElement("a");
a.href = URL.createObjectURL(file);
a.download = filename;
document.body.appendChild(a);
a.click();
}
function importTrainingData() {
//code to import training data from a file on the computer
let input = document.createElement('input');
input.type = 'file';
input.onchange = (event) => {
const file = event.target.files[0];
if(!file || file.type !== 'application/json') {
alert("Please upload a JSON file.");
return;
}
const reader = new FileReader();
reader.onload = (event) => {
const contents = event.target.result;
trainingData = JSON.parse(contents);
}
reader.readAsText(file);
}
input.click();
}
</script>