|
| 1 | +<!-- https://teachablemachine.withgoogle.com/models/8h2tNT1YS/ --> |
| 2 | +<div>Teachable Machine Image Model</div> |
| 3 | +<button type="button" onclick="init()">Start</button> |
| 4 | +<div id="webcam-container"></div> |
| 5 | +<div id="label-container"></div> |
| 6 | +<script src=" https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js" ></script> |
| 7 | +<script src=" https://cdn.jsdelivr.net/npm/@teachablemachine/[email protected]/dist/teachablemachine-image.min.js" ></script> |
| 8 | +<script type="text/javascript"> |
| 9 | + // More API functions here: |
| 10 | + // https://github.com/googlecreativelab/teachablemachine-community/tree/master/libraries/image |
| 11 | + |
| 12 | + // the link to your model provided by Teachable Machine export panel |
| 13 | + const URL = "https://teachablemachine.withgoogle.com/models/8h2tNT1YS/"; |
| 14 | + |
| 15 | + let model, webcam, labelContainer, maxPredictions; |
| 16 | + |
| 17 | + // Load the image model and setup the webcam |
| 18 | + async function init() { |
| 19 | + const modelURL = URL + "model.json"; |
| 20 | + const metadataURL = URL + "metadata.json"; |
| 21 | + |
| 22 | + // load the model and metadata |
| 23 | + // Refer to tmImage.loadFromFiles() in the API to support files from a file picker |
| 24 | + // or files from your local hard drive |
| 25 | + // Note: the pose library adds "tmImage" object to your window (window.tmImage) |
| 26 | + model = await tmImage.load(modelURL, metadataURL); |
| 27 | + maxPredictions = model.getTotalClasses(); |
| 28 | + |
| 29 | + // Convenience function to setup a webcam |
| 30 | + const flip = true; // whether to flip the webcam |
| 31 | + webcam = new tmImage.Webcam(200, 200, flip); // width, height, flip |
| 32 | + await webcam.setup(); // request access to the webcam |
| 33 | + await webcam.play(); |
| 34 | + window.requestAnimationFrame(loop); |
| 35 | + |
| 36 | + // append elements to the DOM |
| 37 | + document.getElementById("webcam-container").appendChild(webcam.canvas); |
| 38 | + labelContainer = document.getElementById("label-container"); |
| 39 | + for (let i = 0; i < maxPredictions; i++) { // and class labels |
| 40 | + labelContainer.appendChild(document.createElement("div")); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + async function loop() { |
| 45 | + webcam.update(); // update the webcam frame |
| 46 | + await predict(); |
| 47 | + window.requestAnimationFrame(loop); |
| 48 | + } |
| 49 | + |
| 50 | + // run the webcam image through the image model |
| 51 | + async function predict() { |
| 52 | + // predict can take in an image, video or canvas html element |
| 53 | + const prediction = await model.predict(webcam.canvas); |
| 54 | + for (let i = 0; i < maxPredictions; i++) { |
| 55 | + const classPrediction = |
| 56 | + prediction[i].className + ": " + prediction[i].probability.toFixed(2); |
| 57 | + labelContainer.childNodes[i].innerHTML = classPrediction; |
| 58 | + } |
| 59 | + } |
| 60 | +</script> |
0 commit comments