Что нам нужно, чтобы увидеть прогноз, это всего 10 строк, как показано ниже. В этом случае я использовал npm вместо тега script. (используя вебпак)
import * as cocoSsd from ‘@tensorflow-models/coco-ssd’; const img = new Image(); img.src = ‘./images/dog.jpg’; const prediction = async() => { const model = await cocoSsd.load(); console.log(“loaded…”); const predictions = await model.detect(img); document.getElementById(“prediction”).innerHTML=`class:${predictions[0].class} score:${predictions[0].score}`; } prediction();
Следующий HTML, index.html, был сгенерирован кодом Visual Studio (только что добавлен тег скрипта, тег изображения и тег div)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script src="bundle.js"></script> <title>Document</title> </head> <body> <img src="images/dog.jpg" id="dog" alt=""> <div id="prediction"></div> </body> </html>
Результат
Если вы используете тег script, вам нужно будет написать около 11 строк в index.html.
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script> <script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd"></script> <script> const img = new Image(); img.src = './cat.jpg'; cocoSsd.load().then(model => { model.detect(img).then(predictions => { console.log('Predictions: ', predictions); }); }); </script>
Если вы добавите функцию загрузки изображения/веб-камеру, вы можете легко создать приложение ml.
что-то вроде этого https://sleepy-maker.github.io/tensorflowjs-and-keras/image_classification/webcam/app/public /