May 2021
Intermediate to advanced
338 pages
7h 44m
English
The MobileNet model can detect all kinds of different trucks. You could solve this problem by going through the list of identifiable trucks, or you can simply search for the word truck in the given list of class names. For simplicity, the provided answer did the latter.
The entire solution with HTML and JavaScript is here:
<!DOCTYPE html><html><head><scriptsrc="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.7.0/dist/tf.min.js"></script><scriptsrc="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet@1.0.0"></script><script>mobilenet.load().then(model=>{constimg=document.getElementById('myImage');// Classify the imagemodel.classify(img).then(predictions=>{console.log('Predictions: ',predictions);// Was there a truck?letfoundATruckpredictions.forEach(p=>{foundATruck=foundATruck||p.className.includes("truck")})// TRUCK ALERT!if(foundATruck)alert("TRUCK DETECTED!")});});</script></head><body><h1>Is this a truck?</h1><imgid="myImage"src="truck.jpg"width="100%"></img> ...