xxxxxxxxxx
83
// Teachable Machine and ml5.js
// Intro to ML for the Arts, IMA Fall 2021
// https://github.com/ml5js/Intro-ML-Arts-IMA-F21
// Classifier Variable
let classifier;
// Model URL
const imageModelURL = 'https://teachablemachine.withgoogle.com/models/w_i1E4QDp/';
// Video
let video;
let flippedVideo;
// To store the classification
let label = "";
// Data from the file
let data = [];
let images = {};
let texts = {};
// Load the model and data first
function preload() {
classifier = ml5.imageClassifier(imageModelURL + 'model.json');
data = loadStrings('data.txt', parseData);
}
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);
// Draw the label
fill(255);
textSize(16);
textAlign(CENTER);
text(label, width / 2, height - 4);
// Display corresponding image and text
if (images[label] && texts[label]) {
let img = loadImage(images[label], img => {
image(img, 0, 0, 320, 240);
});
text(texts[label], width / 2, height - 20);
}
}
// Get a prediction for the current video frame
function classifyVideo() {
flippedVideo = ml5.flipImage(video);
classifier.classify(flippedVideo, gotResult);
}
// When we get a result
function gotResult(error, results) {
if (error) {
console.error(error);
return;
}
label = results[0].label;
classifyVideo();
}
// Parse data from the file
function parseData(data) {
data.forEach(line => {
let [label, imageUrl, text] = line.split(',');
images[label] = imageUrl;
texts[label] = text;
});
}