Time to make NeuralNetworkClassifier watcher classify fruits.
Let's create an initialization function for the NNC watcher.
constinitializeNNC= () => {};
Retrieve the elements we'll be working with, along with the bounding rect of the element defining the watcher's contour.
constinitializeNNC= () => {// Get the elements we'll be working with...constnncElement=document.getElementsByClassName('nnc')[0];constnncRecognizedClassElement=document.getElementsByClassName('nnc-recognized-class')[0];// ...along with the bounding rect that defines the watcher sizeconstnncBounds=nncElement.getBoundingClientRect();};
The watcher data structure above is almost complete, but it's missing one key component: what to actually do when classification is triggered.
// ...// All Lampix classifiers return a list of recognized objects// NNClassifier only recognizes one at a time, hence expecting// an array with one element and destructuring itconstnncCallback= ([recognizedObject]) => {nncRecognizedClassElement.textContent =`Recognized: ${recognizedObject.classTag}`;if (Number(recognizedObject.classTag) ===1) {// Change border color on each new detectionnncElement.style.borderColor =randomColor(); } else {// Go back to white if object no longer therenncElement.style.borderColor ='#FFFFFF'; } };constnncFruitsWatcher= { name:'NeuralNetworkClassifier', shape:lampix.helpers.rectangle(nncBounds.left,nncBounds.top,nncBounds.width,nncBounds.height ), params: { neural_network_name:'fruits' }, onClassification: nncCallback };
All that's left now is to inform Lampix of its existence, by adding the following to the end of the initializeNNC function.
// ...lampix.watchers.add(nncFruitsWatcher);
Now, initializeNNC should look like this:
constinitializeNNC= () => {// Get the elements we'll be working with...constnncElement=document.getElementsByClassName('nnc')[0];constnncRecognizedClassElement=document.getElementsByClassName('nnc-recognized-class')[0];// ...along with the bounding rect that defines the watcher sizeconstnncBounds=nncElement.getBoundingClientRect();// All Lampix classifiers return a list of recognized objects// NNClassifier only recognizes one at a time, hence expecting// an array with one element and destructuring itconstnncCallback= ([recognizedObject]) => {nncRecognizedClassElement.textContent =`Recognized: ${recognizedObject.classTag}`;if (Number(recognizedObject.classTag) ===1) {nncElement.style.borderColor ='#FF0000'; } else {// Go back to white if object no longer therenncElement.style.borderColor ='#FFFFFF'; } };constnncFruitsWatcher= { name:'NeuralNetworkClassifier', shape:lampix.helpers.rectangle(nncBounds.left,nncBounds.top,nncBounds.width,nncBounds.height ), params: { neural_network_name:'fruits' }, onClassification: nncCallback };lampix.watchers.add(nncFruitsWatcher);};