bosayama commited on
Commit
dd76c38
·
verified ·
1 Parent(s): c7238cf
Files changed (1) hide show
  1. gr +56 -0
gr ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Teachable Machine Image Model</title>
7
+ </head>
8
+ <body>
9
+ <div>Teachable Machine Image Model</div>
10
+ <div id="webcam-container"></div>
11
+ <div id="label-container"></div>
12
+ <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest/dist/tf.min.js"></script>
13
+ <script src="https://cdn.jsdelivr.net/npm/@teachablemachine/image@latest/dist/teachablemachine-image.min.js"></script>
14
+ <script type="text/javascript">
15
+ const URL = "https://teachablemachine.withgoogle.com/models/ZPfAhDYCh/";
16
+
17
+ let model, webcam, labelContainer, maxPredictions;
18
+
19
+ async function init() {
20
+ const modelURL = URL + "model.json";
21
+ const metadataURL = URL + "metadata.json";
22
+
23
+ model = await tmImage.load(modelURL, metadataURL);
24
+ maxPredictions = model.getTotalClasses();
25
+
26
+ const flip = true;
27
+ webcam = new tmImage.Webcam(200, 200, flip);
28
+ await webcam.setup();
29
+ await webcam.play();
30
+ window.requestAnimationFrame(loop);
31
+
32
+ document.getElementById("webcam-container").appendChild(webcam.canvas);
33
+ labelContainer = document.getElementById("label-container");
34
+ for (let i = 0; i < maxPredictions; i++) {
35
+ labelContainer.appendChild(document.createElement("div"));
36
+ }
37
+ }
38
+
39
+ async function loop() {
40
+ webcam.update();
41
+ await predict();
42
+ window.requestAnimationFrame(loop);
43
+ }
44
+
45
+ async function predict() {
46
+ const prediction = await model.predict(webcam.canvas);
47
+ for (let i = 0; i < maxPredictions; i++) {
48
+ const classPrediction = prediction[i].className + ": " + prediction[i].probability.toFixed(2);
49
+ labelContainer.childNodes[i].innerHTML = classPrediction;
50
+ }
51
+ }
52
+
53
+ init(); // Automatically start the model when the page loads
54
+ </script>
55
+ </body>
56
+ </html>