xxxxxxxxxx
256
const modelURL = 'https://teachablemachine.withgoogle.com/models/pBZAUveIj/';
// the json file (model topology) has a reference to the bin file (model weights)
const checkpointURL = modelURL + "model.json";
// the metatadata json file contains the text labels of your model and additional information
const metadataURL = modelURL + "metadata.json";
const flip = true; // whether to flip the webcam
let model;
let totalClasses;
let myCanvas;
let classification = "None Yet";
let probability = "100";
let poser;
let video;
let clapimage;
let dabimage;
let lassoimage;
let handsimage;
let dancer;
let dummy;
let yay;
let aw;
let score = "0"
let good = (score + 10);
var songs = ["one, two, three"];
function preload(){
songs[0] = loadSound('American Boy Dance.mp3');
clapimage = loadImage("Clapping Icon.png");
dabimage = loadImage("Dab Icon.png");
lassoimage = loadImage("Lasso Icon.png");
handsimage = loadImage("Hands Up Icon.png");
dancer = loadImage("Nice Dancing.png");
dummy = loadImage("Practicem.png");
yay = loadImage("Great.png");
aw = loadImage("Bad.png");
}
// A function that loads the model from the checkpoint
async function load() {
model = await tmPose.load(checkpointURL, metadataURL);
totalClasses = model.getTotalClasses();
console.log("Number of classes, ", totalClasses);
}
async function setup() {
myCanvas = createCanvas(400, 400);
// Call the load function, wait until it finishes loading
videoCanvas = createCanvas(320, 240)
await load();
video = createCapture(VIDEO, videoReady);
video.size(320, 240);
video.hide();
}
function draw() {
background(255);
if(video) image(video,0,0);
//rect(0, 200, 200, 50)
textSize(16);
//text("Result:" + classification, 10, height -20);
if (songs[0].isPlaying() == false){
songs[0].play();
}
if(millis()== 10){
fill(255, 0, 0);
circle(100, 100, 100, 100);
}
text("score", 20, 20)
text(score, 70, 20)
//text("Probability:" + probability, width/2, height -20)
///ALEX insert if statement here testing classification against apppropriate part of array for this time in your video
if((millis() >= 8000)&&(millis() <= 12000)){
//rect(100, 100, 100, 50);
image(handsimage, 200, 130, 100, 100);
correctornot();
}
if((millis() >= 12000)&&(millis() <= 16000)){
//rect(100, 100, 100, 50);
image(dabimage, 200, 130, 120, 100);
correctornot2();
}
if((millis() >= 16000)&&(millis() <= 20000)){
//rect(100, 100, 100, 50);
image(lassoimage, 200, 130, 120, 100);
correctornot3();
}
if((millis() >= 20000)&&(millis() <= 24000)){
//rect(100, 100, 100, 50);
image(handsimage, 200, 130, 100, 100);
correctornot();
}
if((millis() >= 24000)&&(millis() <= 28000)){
//rect(100, 100, 100, 50);
image(clapimage, 200, 130, 100, 100);
correctornot5();
}
if((millis() >= 28000)&&(millis() <= 32000)){
//rect(100, 100, 100, 50);
image(handsimage, 200, 130, 100, 100);
correctornot();
}
if((millis() >= 32000)&&(millis() <= 36000)){
//rect(100, 100, 100, 50);
image(lassoimage, 200, 130, 120, 100);
correctornot3();
}
if((millis() >= 36000)&&(millis() <= 40000)){
//rect(100, 100, 100, 50);
image(dabimage, 200, 130, 120, 100);
correctornot2();
}
if((millis() >= 41000)&&(millis() <= 80000)){
goodorbad();
}
textSize(8);
if (poser) { //did we get a skeleton yet;
for (var i = 0; i < poser.length; i++) {
let x = poser[i].position.x;
let y = poser[i].position.y;
//ellipse(x, y, 5, 5);
//text(poser[i].part, x + 4, y);
}
}
}
function goodorbad(){
textAlign(CENTER);
textSize(20);
fill(255);
text("Your Score :" + score, width/2, 50);
//text(score, width/2, 50);
if(score >= 450){
fill(255);
stroke(255);
rect(75, 75, 200, 100);
image(dancer, 100, 90, 150, 75);
}
else{
fill(255);
stroke(255);
rect(70, 75, 200, 100);
image(dummy, 100, 90, 150, 75);
}
}
function correctornot(){
if(classification == "Handsup"){
image(yay, 210, 80, 75, 50);
print("good!");
score++
}else{
image(aw, 210, 80, 75, 50);
print("bad");
}
}
function correctornot2(){
if(classification == "Dab"){
image(yay, 210, 80, 75, 50);
print("good!");
score++
}else{
image(aw, 210, 80, 75, 50);
print("bad");
score--
}
}
function correctornot3(){
if(classification == "Cowboy"){
image(yay, 210, 80, 75, 50);
print("good!");
score++
}else{
image(aw, 210, 80, 75, 50);
print("bad");
score--
}
}
function correctornot4(){
if(classification == "Knees Touch"){
image(yay, 210, 80, 75, 50);
print("good!");
score++
}else{
image(aw, 210, 80, 75, 50);
print("bad");
score--
}
}
function correctornot5(){
if(classification == "Clapping"){
image(yay, 210, 80, 75, 50);
print("good!");
score++
}else{
image(aw, 210, 80, 75, 50);
print("bad");
score--
}
}
function videoReady() {
console.log("Video Ready");
predict();
}
async function predict() {
// Prediction #1: run input through posenet
// predict can take in an image, video or canvas html element
const flipHorizontal = false;
const {
pose,
posenetOutput
} = await model.estimatePose(
video.elt, //webcam.canvas,
flipHorizontal
);
// Prediction 2: run input through teachable machine assification model
const prediction = await model.predict(
posenetOutput,
flipHorizontal,
totalClasses
);
// console.log(prediction);
// Sort prediction array by probability
// So the first classname will have the highest probability
const sortedPrediction = prediction.sort((a, b) => -a.probability + b.probability);
//communicate these values back to draw function with global variables
classification = sortedPrediction[0].className;
probability = sortedPrediction[0].probability.toFixed(2);
if (pose) poser = pose.keypoints; // is there a skeleton
predict();
}