xxxxxxxxxx
76
const modelURL = 'https://teachablemachine.withgoogle.com/models/MvQSidP2m/';
const checkpointURL = modelURL + "model.json";
const metadataURL = modelURL + "metadata.json";
let model;
let totalClasses;
let myCanvas;
let classification = "please wait..";
let probability = "?";
let poser;
let video;
// 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);
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);
fill(0)
textSize(10);
text("Result : " + classification, 220, 40);
text("Probability : " + probability, 220, 20)
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 videoReady() {
console.log("Video is...Ready !");
predict();
}
async function predict() {
// Prediction #1: run input through posenet
// predict can take in an image, video or canvas html element
const {pose,posenetOutput} = await model.estimatePose(video.elt );
// Prediction 2: run input through teachable machine assification model
const prediction = await model.predict(
posenetOutput,totalClasses);
// Sort prediction array by probability
const sortedPrediction = prediction.sort((a, b) => -a.probability + b.probability);
classification = sortedPrediction[0].className;
probability = sortedPrediction[0].probability.toFixed(2);
if (pose) poser = pose.keypoints;
predict();
}