xxxxxxxxxx
96
// Copyright (c) 2019 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
/* ===
ml5 Example
Webcam Image Classification using a pre-trained customized model and p5.js
This example uses p5 preload function to create the classifier
=== */
let classifier;
let imageModelURL = 'https://teachablemachine.withgoogle.com/models/OnMttmbI4/' ;
let camera;
let count = 0;
// Video
let video;
let flippedVideo;
// To store the classification
let label = "";
function preload() {
camera = loadImage("camera.png");
classifier = ml5.imageClassifier(imageModelURL + 'model.json');
}
function setup() {
createCanvas(608, 342);
// Create the video
video = createCapture(VIDEO);
video.size(288, 224);
flippedVideo = ml5.flipImage(video)
video.hide();
// Start classifying
classifyVideo();
}
function draw() {
background(0);
image(flippedVideo, 68, 85);
image(camera, 0, 0, 608, 342);
fill(255);
textSize(14);
textAlign(CENTER);
if(label == "Penny"){
text('lil penny mamita', 210, 114);
} else if(label == "Penny & Boom"){
text('penny & boom', 216, 114);
} else {
text('penny & koob', 216, 114);}
}
// Get a prediction for the current video frame
function classifyVideo() {
flippedVideo = ml5.flipImage(video)
classifier.classify(flippedVideo, gotResult);
}
// function classifyVideo() {
// flippedVideo = ml5.flipImage(video)
// for (let i = 0; i <= 100; i++) {
// counter = i;
// console.log(`Counter: ${counter}`);
// if (counter === 100) {
// classifier.classify(flippedVideo, gotResult);
// }
// }
// }
// When we get a result
function gotResult(error, results) {
// If there is an error
if (error) {
console.error(error);
return;
}
// The results are in an array ordered by confidence.
// console.log(results[0]);
label = results[0].label;
// Classifiy again!
classifyVideo();
}