xxxxxxxxxx
74
let neuralnet;
let selectInput;
let trainBtn;
let myCanvas;
let myResults;
let options = {
inputs: ['x', 'y'],
outputs: ['label'],
task: 'classification',
debug: true
}
function setup() {
myCanvas = createCanvas(400, 400);
background(200);
selectInput = select("#selectInput");
trainBtn = select("#selectInput");
myResults = select("#myResults");
inputValue = selectInput.value();
selectInput.changed(function(){
inputValue = selectInput.value();
});
neuralnet = ml5.neuralNetwork(options);
myCanvas.mousePressed(addData);
trainBtn.mousePressed(trainModel);
}
function addData(){
const xLoc = mouseX;
const yLoc = mouseY;
const labelValue = inputValue;
neuralnet.addData({x:xLoc,y:yLoc},{label: labelValue})
ellipse(xLoc, yLoc, 40,40);
text(labelValue, xLoc, yLoc);
}
function trainModel(){
neuralnet.normalizeData();
const trainingOptions = {
epochs: 24,
batchSize: 12
}
neuralnet.train(trainingOptions, finishedTraining)
}
function finishedTraining(){
classify();
}
function classify(){
neuralnet.classify({x: mouseX, y: mouseY}, gotResults)//if it were a regression you could say prediect
}
function gotResults(err,results){
if(err) {
console.log(err);
return;
}
myResults.html("I see a..." + results[0].label);
classify();
}