xxxxxxxxxx
88
const modelURL = "https://teachablemachine.withgoogle.com/models/ASWbMukmt/";
// The video
let video;
// For displaying the label
let label = "waiting...";
// The classifier
let classifier;
let clappingImage;
let dabbingImage;
let score = 0;
let scoreThreshold = 30;
let lastImageChangeTime = 0;
let currentImage;
let imageDuration = 900; // reduce to make faster
function preload() {
classifier = ml5.imageClassifier(modelURL + 'model.json');
clappingImage = loadImage("clapping.jpg");
dabbingImage = loadImage("dabbing.jpeg");
}
function setup() {
createCanvas(500, 500);
// Create the video
video = createCapture(VIDEO);
video.hide();
classifyVideo();
// Initialize currentImage with one of the images
currentImage = random() > 0.5 ? clappingImage : dabbingImage;
}
// STEP 2 classify the video!
function classifyVideo() {
classifier.classify(video, gotResults);
}
function draw() {
// Set the background to beige
background(245, 245, 220); // Beige background color
// Draw the video in the top right corner
let videoWidth = 320; // Adjust as needed
let videoHeight = 240; // Adjust as needed
image(video, width - videoWidth, 0, videoWidth, videoHeight);
// Draw the label
textSize(32);
textAlign(CENTER, CENTER);
fill(0); // Text color (black)
text(label, width / 2, height - 16);
// Check if it's time to change the image
if (millis() - lastImageChangeTime > imageDuration) {
// Randomly choose between clapping and dabbing images
currentImage = random() > 0.5 ? clappingImage : dabbingImage;
lastImageChangeTime = millis();
}
// Display the current image in the bottom left corner
image(currentImage, 0, height - currentImage.height);
//display score
updateScore();
text(score, 20, 20);
}
function gotResults(error, results) {
// Something went wrong!
if (error) {
console.error(error);
return;
}
// Store the label and classify again!
label = results[0].label;
classifyVideo();
}
function updateScore(){
if (currentImage == clappingImage && label == "clapping"){
score += 5;
}
else if (currentImage == dabbingImage && label =="dabbing"){
score +=5;
}
}
//add "nothing", rolling arms, holding hands up, peace sign into ml model
//add game aesthetics