xxxxxxxxxx
91
// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Code for: https://youtu.be/cXgA1d_E-jY&
// Model URL
let imageModelURL = 'https://teachablemachine.withgoogle.com/models/c84SpbQo/';
// Classifier Variable
let classifier;
// Video
let video;
// To store the classification
let label = "";
// Load the model first
function preload() {
classifier = ml5.imageClassifier(imageModelURL + 'model.json');
}
var bird;
var pipes = [];
function setup() {
createCanvas(640, 480);
bird = new Bird();
pipes.push(new Pipe());
video = createCapture(VIDEO);
video.size(320, 240);
video.hide();
// Start classifying
classifyVideo();
}
function draw() {
background(0);
for (var i = pipes.length - 1; i >= 0; i--) {
pipes[i].show();
pipes[i].update();
if (pipes[i].hits(bird)) {
console.log("HIT");
}
if (pipes[i].offscreen()) {
pipes.splice(i, 1);
}
}
bird.update();
bird.show();
if (frameCount % 75 == 0) {
pipes.push(new Pipe());
}
}
function keyPressed() {
if (key == ' ') {
bird.up();
//console.log("SPACE");
}
}
function classifyVideo() {
classifier.classify(video, gotResult);
}
// When we get a result
function gotResult(error, results) {
print('result')
// 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;
if (label == 'jump') {
bird.up();
}
// Classifiy again!
setTimeout(classifyVideo, 200)
}