xxxxxxxxxx
57
const myModelURL = 'https://teachablemachine.withgoogle.com/models/T49gvVlqV/';
let myModel;
let flippedVideo;
let myVideo;
let myResults;
function preload() {
myModel = ml5.imageClassifier(myModelURL + 'model.json', modelLoaded);
}
function modelLoaded() {
console.log('myModel', myModel);
}
function setup() {
createCanvas(800, 600);
myVideo = createCapture(VIDEO);
myVideo.size(320, 240);
myVideo.hide();
flippedVideo = ml5.flipImage(myVideo);
classifyMyVideo();
textAlign(CENTER);
textSize(64);
fill(255);
}
function classifyMyVideo() {
flippedVideo = ml5.flipImage(myVideo);
myModel.classify(flippedVideo, gotResults);
}
function gotResults(error, results) {
if (error) {
console.error(error);
}
if (results) {
console.log(results);
myResults = results;
classifyMyVideo();
}
}
function draw() {
background(220);
image(flippedVideo, 0, 0, width, height);
if (myResults) {
const label = myResults[0].label;
text(label, width / 2, height / 2);
if (label === 'Happy') {
text('😁', width / 2, height / 2 + 100);
} else if (label === 'Angry') {
text('😡', width / 2, height / 2 + 100);
} else if (label === 'Sad') {
text('😢', width / 2, height / 2 + 100);
}
}
}