xxxxxxxxxx
134
//https://www.youtube.com/watch?v=pnntrewH0xg
//playing card detection video
//https://learn.ml5js.org/docs/#/reference/object-detector
//object detector
// Video
let video;
let classifier;
let label = 'waiting';
let label2 = 'waiting';
let label3 = 'waiting';
let label4 = 'waiting';
// STEP 1: Load the model!
function preload() {
classifier = ml5.imageClassifier('https://teachablemachine.withgoogle.com/models/uxq7drCDV/model.json');
sound1 = loadSound("sound1.mp3");
sound2 = loadSound("sound2.mp3");
sound3 = loadSound("sound3.mp3");
sound4 = loadSound("sound4.mp3");
}
function setup() {
createCanvas(640, 520);
video = createCapture(VIDEO);
video.hide();
// STEP 2: Start classifying
classifyVideo();
}
// STEP 2: classify!
function classifyVideo() {
classifier.classify(video, gotResults);
}
function draw() {
background(0);
image(video, 0, 0);
// STEP 4: Draw the label
textSize(30);
textAlign(CENTER, CENTER);
fill(255);
text(label, width/2, height-16);
console.log(sound1.isPlaying(), sound2.isPlaying(), sound3.isPlaying(), sound4.isPlaying());
}
// STEP 3: Get the classification!
function gotResults(error, results) {
console.log(results[0].label);
label = results[0].label;
console.log(label, label2, label3);
label4 = label3;
label3 = label2;
label2 = label;
classifyVideo();
//then classify video again
if
(label == "3 Club" && label2 == "3 Club" && label3 == "3 Club" && label4 == "3 Club")
{play1();}
if
(label == "4 Spade" && label2 == "4 Spade" && label3 == "4 Spade" && label4 == "4 Spade")
{play2();}
if
(label == "5 Diamond" && label2 == "5 Diamond" && label3 == "5 Diamond" && label4 == "5 Diamond")
{play3();}
if
(label == "9 Heart" && label2 == "9 Heart" && label3 == "9 Heart" && label4 == "9 Heart")
{play4();}
if
(label == "No Card" && label2 == "No Card" && label3 == "No Card" && label4 == "No Card")
{stopplay();}
}
function play1() {
// for (var i = 1; i <= 4; i++) {
// if (mylist[i].isPlaying==true) {sound1.jump(mylist[i].currentTime())}}
// not sure how to phrase the else sound1.loop part in this case though
if (sound1.isPlaying() == false) {
if (sound2.isPlaying() == true) {
sound1.jump(sound2.currentTime() ) ;}
else if (sound3.isPlaying() == true) {
sound1.jump(sound3.currentTime() ) ;}
else if (sound4.isPlaying() == true) {
sound1.jump(sound4.currentTime() ) ;}
else {sound1.loop(); }
}}
function play2() {
if (sound2.isPlaying() == false) {
if (sound1.isPlaying() == true) {
sound2.jump(sound1.currentTime() ) ;}
else if (sound3.isPlaying() == true) {
sound2.jump(sound3.currentTime() ) ;}
else if (sound4.isPlaying() == true) {
sound2.jump(sound4.currentTime() ) ;}
else {sound2.loop(); }
}}
function play3() {
if (sound3.isPlaying() == false) {
if (sound2.isPlaying() == true) {
sound3.jump(sound2.currentTime() ) ;}
else if (sound1.isPlaying() == true) {
sound3.jump(sound1.currentTime() ) ;}
else if (sound4.isPlaying() == true) {
sound3.jump(sound4.currentTime() ) ;}
else {sound3.loop(); }
}}
function play4() {
if (sound4.isPlaying() == false) {
if (sound2.isPlaying() == true) {
sound4.jump(sound2.currentTime() ) ;}
else if (sound3.isPlaying() == true) {
sound4.jump(sound3.currentTime() ) ;}
else if (sound1.isPlaying() == true) {
sound4.jump(sound1.currentTime() ) ;}
else {sound4.loop(); }
}}
function stopplay() {
// if (sound1.isPlaying()) {sound1.stop()}
// if (sound2.isPlaying()) {sound2.stop()}
// if (sound3.isPlaying()) {sound3.stop()}
// if (sound4.isPlaying()) {sound4.stop()}
sound1.stop(); sound2.stop(); sound3.stop(); sound4.stop();
}
//also add a time frame or number of results or a clear function would probs be easier actually....