TWS/WAP Cheatsheet

tensorflow.org/js/models

projects/object detection

projects/image classification

HTML

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Image classification</title> <link rel="preconnect" href="https://fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css2?family=Roboto&display=swap" rel="stylesheet"> <link href="style.css" rel="stylesheet" type="text/css"> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.1/css/bulma.min.css"> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.0.1"> </script> <!-- Load the MobileNet model. --> <script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet@1.0.0"> </script> </head> <body> <div class="wrapper"> <img id="img"> <input id="input" class="input"> <div class="has-text-centered"> <button id="button" class="button is-light is-success">Predict</button> <p id="predictions"></p> </div> </div> <script src="main.js"></script> </body> </html>

CSS

* { box-sizing: border-box; } .wrapper { max-width: 320px; width: 100%; margin: 0 auto; } #button { margin-top: 1em; margin-bottom: 1em; }

JavaScript

const img = document.getElementById('img'); const button = document.getElementById('button'); const input = document.getElementById('input'); const predictionsP = document.getElementById('predictions'); button.onclick = () => { img.src = `./img/${input.value}`; mobilenet.load().then(model => { model.classify(img).then(predictions => { console.log({predictions}); predictionsP.innerHTML = ""; predictions.forEach(element => { predictionsP.innerHTML += `

ClassName: ${element.className}, probability: ${element.probability}

`; }); }); }) }

Dostupné obrázky v projektu

test1.jpg | test2.png | test3.jpg

Page