xxxxxxxxxx
99
const modelURL = 'https://teachablemachine.withgoogle.com/models/I4gPei7un/';
const checkpointURL = modelURL + "model.json";
const metadataURL = modelURL + "metadata.json";
const flip = true;
let webcam;
let model;
let totalClasses;
let myCanvas;
let ctx;
let name;
let confidentscore;
async function load() {
model = await tmPose.load(checkpointURL, metadataURL);
totalClasses = model.getTotalClasses();
}
async function loadWebcam() {
webcam = new tmPose.Webcam(600, 500, flip); // can change width and height
await webcam.setup(); // request access to the webcam
await webcam.play();
window.requestAnimationFrame(loopWebcam);
}
async function setup() {
myCanvas = createCanvas(600, 500);
ctx = myCanvas.elt.getContext("2d");
await load();
await loadWebcam();
}
async function loopWebcam(timestamp) {
// console.log(timestamp);
webcam.update(); // update the webcam frame
await predict();
window.requestAnimationFrame(loopWebcam);
snapshot(timestamp);
}
let previous = 0;
let password = "";
function snapshot(current) {
if ((password.length < 4)&& (current-previous > 4000)){ //GET INPUT ONLY WHEN PASSWORD LENGTH IS LESS THAN 4
// console.log(pause);
if (confidentscore > 0.9){
if (name != 'stand still'){
password += name;
console.log(password);
previous = current;
}
}
}
}
async function predict() {
const flipHorizontal = false;
const { pose, posenetOutput } = await model.estimatePose(
webcam.canvas,
flipHorizontal
);
const prediction = await model.predict(
posenetOutput,
flipHorizontal,
totalClasses
);
const sortedPrediction = prediction.sort((a, b) => - a.probability + b.probability);
const res = select('#res'); // select <span id="res">
res.html(sortedPrediction[0].className);
name = sortedPrediction[0].className;
const prob = select('#prob'); // select <span id="prob">
prob.html(sortedPrediction[0].probability.toFixed(2));
confidentscore = sortedPrediction[0].probability.toFixed(2);
if (pose) {
drawPose(pose);
}
}
function drawPose(pose) {
if (webcam.canvas) {
ctx.drawImage(webcam.canvas, 0, 0);
// draw the keypoints and skeleton
if (pose) {
const minPartConfidence = 0.5;
tmPose.drawKeypoints(pose.keypoints, minPartConfidence, ctx);
tmPose.drawSkeleton(pose.keypoints, minPartConfidence, ctx);
}
}
}