xxxxxxxxxx
110
let mobilenet;
let classifier;
let video;
let label = "Train Your Even/Odd";
let buttonEven;
let buttonOdd;
let buttonCheck;
let buttonTrain;
let dealerCard;
function modelReady() {
console.log("Model is ready!!!");
}
function videoReady() {
console.log("Video is ready!!!");
}
function whileTraining(loss) {
if (loss == null) {
console.log("Training Complete");
classifier.classify(gotResults);
background(0);
label = "Guess What I have";
fill(255);
textAlign(CENTER);
textSize(50);
text(label, width / 2, height / 2);
} else {
console.log(loss);
}
}
function gotResults(error, result) {
if (error) {
console.error(error);
} else {
// updated to work with newer version of ml5
//label = result;
label = result[0].label;
classifier.classify(gotResults);
}
}
function setup() {
//createCanvas(320, 270);
createCanvas(640, 200);
video = createCapture(VIDEO);
//video.hide();
background(0);
mobilenet = ml5.featureExtractor("MobileNet", modelReady);
classifier = mobilenet.classification(video, videoReady);
buttonEven = createButton("Even");
buttonEven.size(70);
buttonEven.position(0, 700);
buttonEven.mousePressed(function () {
label = "Train your Even image";
classifier.addImage("Even");
});
buttonOdd = createButton("Odd");
buttonOdd.size(70);
buttonOdd.position(80, 700);
buttonOdd.mousePressed(function () {
label = "Train your Odd image";
classifier.addImage("Odd");
});
buttonTrain = createButton("Train");
buttonTrain.size(70);
buttonTrain.position(160, 700);
buttonTrain.mousePressed(function () {
classifier.train(whileTraining);
});
buttonCheck = createButton("Check Out");
buttonCheck.position(0, 750);
buttonCheck.size(230);
buttonCheck.mousePressed(function () {
checkOut();
});
fill(255);
textAlign(CENTER);
textSize(50);
text(label, width / 2, height / 2);
}
function checkOut() {
dealerCard = random(["Odd", "Even"]);
background(0);
fill(255);
textAlign(CENTER);
textSize(40);
let line1 = "You guessed: " + label;
let line2 = "I got: " + dealerCard;
if (label == dealerCard) {
background(0, 255, 0);
text(line2, width / 2, height / 2 + 50);
text(line1, width / 2, height / 2);
text("You Win", width / 2, height / 2 - 50);
} else {
background(255, 0, 0);
text(line2, width / 2, height / 2 + 50);
text(line1, width / 2, height / 2);
text("You Lose", width / 2, height / 2 - 50);
}
}