xxxxxxxxxx
104
// Put just the p5.js javascript here. Strip out all the HTML tags you get in the Teachable Machine output. All html should go in the index.html file. Comments below that start with "aja" indicate my modifications of the TM example code snippet.
// set variables
let classifier;
let angry;
let angry_eyes;
let wary_eyes;
let face_mask;
let imageModelURL = "https://teachablemachine.withgoogle.com/models/wTH7ZVDZw/";
// Video
let video;
let flippedVideo;
// To store the classification
let label = "";
// aja adds these two for the confidence threshold.
let confidence;
let formatted_conf = "";
// Text size varibles
var textS = 15;
// switch between neutral faces
var n_count = 0;
// Load the model first
function preload() {
classifier = ml5.imageClassifier(imageModelURL + "model.json");
//loading images
angry = loadImage('angry.png');
angry_eyes = loadImage('angry_eyes.png');
wary_eyes = loadImage('wary_eyes.png');
face_mask = loadImage('face_mask.png');
}
function setup() {
createCanvas(320, 260);
// Create the video
video = createCapture(VIDEO);
video.size(320, 240);
video.hide();
flippedVideo = ml5.flipImage(video);
// Start classifying
classifyVideo();
}
function draw() {
background(0);
// Draw the video
image(flippedVideo, 0, 0);
}
// Get a prediction for the current video frame
function classifyVideo() {
flippedVideo = ml5.flipImage(video);
classifier.classify(flippedVideo, gotResult);
flippedVideo.remove();
}
// When we get a result
function gotResult(error, results) {
// If there is an error
if (error) {
console.error(error);
return;
}
label = results[0].label;
// tried to change text size based on result but I am not sure if I called the result properly because it did not work
if (label == "Too Close"){
image(angry, 0,0);
image(angry_eyes,0,0);
image(face_mask,0,0);
} else if (label == "Close"){
image(wary_eyes,0,0);
image(face_mask,0,0);
} else if (label == "Far"){
n_count++;
if (n_count <= 100){
image(neutral_face_1,0,0);
} else if (n_count > 100 && n_count <= 200){
image(neutral_face_2,0,0);
n_count = 0;
}
}
// Classify again!
classifyVideo();
}