I have a Tensorflow.js model making predictions and I want to send a certain websocket message each time the predicted label changes.
I have tried to store the latest label in a variable and compare that to the newest one. However, my "cat" websocket message is still activated each second even if the predicted label stays the same. Problem is probably some newbie mistake regarding the use of async function or storing the values - I would really appreciate any directions! Thank you so much.
Code (cleaned to include only relevant bits):
let newLabel;
let oldLabel;
async function app() {
const model = await tf.loadGraphModel(MODEL_URL);
console.log('Successfully loaded model');
const labels = ["cat", "dog", "human"];
console.log(labels);
const webcam = await tf.data.webcam(video);
var myPredict;
async function predictFunction () {
myPredict = setInterval (predictLoop, 1000);
}
async function predictLoop () {
...
const result = await model.predict(t4d);
result.print();
result.as1D().argMax().print();
const labelIndex = result.as1D().argMax();
const predictionLabel = result.as1D().argMax().dataSync()[0];
newLabel = labels[predictionLabel];
const predict = await result.data();
const value = predict[predictionLabel];
document.getElementById("demo").innerHTML = `
I can see a: ${newLabel}\n
Confidence is: ${value}\n
`;
if (newLabel== "cat" && newLabel !==oldLabel){
var data = {
speed: 100
}
socket.emit("cat", data);
sendParam ("/sound", 1);
}
else {
socket.emit("other");
sendParam ("/sound", 2);
}
img.dispose();
result.dispose();
smalImg.dispose();
resized.dispose();
t4d.dispose();
tf.engine().endScope();
await tf.nextFrame();
}
oldLabel = newLabel;
predictFunction();
}
app();