xxxxxxxxxx
83
// The json file defining the weights of the model
const checkpointURL = 'https://storage.googleapis.com/tm-models/fRJ7LKDj/model.json';
// The metatadata json file contains the text labels of your model
// and additional information
const metadataURL = 'https://storage.googleapis.com/tm-models/fRJ7LKDj/metadata.json';
let model, webcamEl, maxPredictions;
async function init() {
// load the model and metadata
// Refer to tmImage.loadFromFiles() in the API to support files from a file picker
// or files from your local hard drive
model = await tmImage.load(checkpointURL, metadataURL);
maxPredictions = model.getTotalClasses();
// optional function for creating a webcam
// webcam has a square ratio and is flipped by default to match training
const webcamFlipped = true;
webcamEl = await tmImage.getWebcam(200, 200, 'front', webcamFlipped);
webcamEl.play();
document.querySelector('#video').appendChild(webcamEl);
// document.body.appendChild(webcamEl);
window.requestAnimationFrame(loop); // kick of pose prediction loop
}
async function loop(timestamp) {
await predict();
window.requestAnimationFrame(loop);
}
let port;
async function predict() {
// predict can take in an image, video or canvas html element
// we set flip to true since the webcam was only flipped in CSS
const flip = true;
const prediction = await model.predict(webcamEl, flip, maxPredictions);
let view = new Uint8Array(1);
// console.log(prediction);
const class1 = prediction.filter(objs => {
if (objs.className === 'Class 1') {
return objs;
}});
const class2 = prediction.filter(objs => {
if (objs.className === 'Class 2') {
return objs;
}});
const class3 = prediction.filter(objs => {
if (objs.className === 'Class 3') {
return objs;
}});
if (class1[0].probability > 0.9) {
console.log("ceral sent");
view[0] = 1;
port.send(view);
}
else if (class2[0].probability > 0.9) {
console.log("mallow sent");
view[0] = 2;
port.send(view);
}
const class1Bar = document.getElementById('class-1');
const class2Bar = document.getElementById('class-2');
class1Bar.style.width = parseInt(prediction[0].probability * 100 ) + '%';
class2Bar.style.width = parseInt(prediction[1].probability * 100) + '%';
}
init();