xxxxxxxxxx
212
/*
Inspired by "Trainable Camera", Created by Andreas Refsgaard - https://editor.p5js.org/AndreasRef/sketches/BynhuHsqX
*/
let video;
let myResults
let resultLabel
let myColor;
let detector
let detections=[]
let featureExtractor;
let classifier;
let loss;
let imagesOfA = 0;
let imagesOfB = 0;
let classificationResult;
let confidence = 0;
let pg;
let lastSnapShot;
let timer = 0;
let showLatestPhoto = false;
let nCurrentClass = 0;
let shutter;
function setup() {
createCanvas(640, 480);
pixelDensity(1);
video = createCapture(VIDEO);
video.size(640, 480);
video.hide();
speech = new p5.Speech(voiceReady); //callback, speech synthesis object
// speech.onLOad = voiceReady;
speech.started(startSpeaking);
//speech.ended(endSpeaking);
function startSpeaking() {
background(0, 255, 0);
}
function voiceReady() {
console.log(speech.voices);
}
// input name field
// myButton = createButton("Create Label");
// myButton.position(512, 120);
// // myButton.mousePressed(getText);
// myButton.elt.style.fontSize = "20px";
// myInput = createInput("Relplace this text with your name?");
// myInput.position(0, 300);
// myInput.size(500);
// myInput.elt.style.fontSize = "20px";
// Extract the already learned features from MobileNet
featureExtractor = ml5.featureExtractor('MobileNet', modelReady);
//Specify the number of classes/labels
const options = { numLabels: 2 };
classifier = featureExtractor.classification(video, options);
shutter = loadSound('shutter.wav');
pg = createGraphics(width, height);
setupButtons();
}
function gotDetections(error, results) {
if (error) {
console.error(error);
}
detections = results;
detector.detect(video, gotDetections);
}
// function getText() {
// const inputValue = myInput.value();
// console.log("myinput", inputValue);
// if (!inputValue || inputValue.length <= 0) {
// return;
// }
// // set label value to inputted text
// }
function draw() {
background(122);
image(video, 0, 0);
if (classificationResult == 'A') {
nCurrentClass = 0;
} else if (classificationResult == 'B') {
nCurrentClass++;
}
//Show the last image taken for a short period
if (showLatestPhoto) {
image(pg, 0, 0);
}
//Flash effect
if (timer < 5) {
background(timer * 25 + 130);
} else if (timer < 8) {
background(255);
}
if (timer > 100) {
showLatestPhoto = false;
}
timer++;
if (classificationResult == 'B' && nCurrentClass > 50) {
takePicture();
speech.speak('CHEATING!! Alert CHEATING Alert')
speech.speak('Student in seat 3B is engaging in foulplay. Warning! Remove from exam room. Warning!')
}
}
function takePicture() {
save('myCanvas.jpg'); //Does not work in iFrames, but should work if you run the code locally outside of the p5js web editor
shutter.play();
pg.image(video, 0, 0);
timer = 0;
showLatestPhoto = true;
nCurrentClass = 0;
}
// A function to be called when the model has been loaded
function modelReady() {
select('#modelStatus').html('Base Model (MobileNet) loaded!');
}
// A function to be called when the video has loaded
function videoReady() {
select('#videoStatus').html('Video ready!');
}
// Classify the current frame.
function classify() {
classifier.classify(gotResults);
}
// A util function to create UI buttons
function setupButtons() {
// When the A button is pressed, add the current frame
// from the video with a label of "A" to the classifier
buttonA = select('#ButtonA');
buttonA.mousePressed(function() {
classifier.addImage('A');
select('#amountOfAImages').html(imagesOfA++);
});
// When the B button is pressed, add the current frame
// from the video with a label of "B" to the classifier
buttonB = select('#ButtonB');
buttonB.mousePressed(function() {
classifier.addImage('B');
select('#amountOfBImages').html(imagesOfB++);
});
// Train Button
train = select('#train');
train.mousePressed(function() {
classifier.train(function(lossValue) {
if (lossValue) {
loss = lossValue;
select('#loss').html('Loss: ' + loss);
} else {
select('#loss').html('Done Training! Final Loss: ' + loss);
}
});
});
// Predict Button
buttonPredict = select('#buttonPredict');
buttonPredict.mousePressed(classify);
// Save model
saveBtn = select('#save');
saveBtn.mousePressed(function() {
classifier.save();
});
// Load model
loadBtn = select('#load');
loadBtn.changed(function() {
classifier.load(loadBtn.elt.files, function(){
select('#modelStatus').html('Custom Model Loaded!');
});
});
}
// Show the results
function gotResults(err, result) {
// Display any error
if (err) {
console.error(err);
}
select('#result').html(result[0].label);
select('#confidence').html(nf(result[0].confidence,0,2));
classificationResult = result[0].label;
confidence = result[0].confidence;
classify();
}