Spaces:
Running
Running
Upload index.html
Browse files- index.html +49 -8
index.html
CHANGED
@@ -7,13 +7,54 @@
|
|
7 |
<link rel="stylesheet" href="style.css" />
|
8 |
</head>
|
9 |
<body>
|
10 |
-
<div class="card">
|
11 |
-
<h1>
|
12 |
-
<
|
13 |
-
<
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
</div>
|
18 |
</body>
|
19 |
-
</html>
|
|
|
7 |
<link rel="stylesheet" href="style.css" />
|
8 |
</head>
|
9 |
<body>
|
10 |
+
<div class="card" style="text-align: center;">
|
11 |
+
<h1>Teachable Machine Image Model</h1>
|
12 |
+
<button type="button" id="startbutton" style="padding: 10px; margin-bottom:20px;" onclick="init()">Start</button>
|
13 |
+
<div id="webcam-container"></div>
|
14 |
+
<div id="label-container"></div>
|
15 |
+
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest/dist/tf.min.js"></script>
|
16 |
+
<script src="https://cdn.jsdelivr.net/npm/@teachablemachine/image@latest/dist/teachablemachine-image.min.js"></script>
|
17 |
+
<script type="text/javascript">
|
18 |
+
// the link to your model provided by Teachable Machine export panel
|
19 |
+
const URL = "./tm-my-image-model/";
|
20 |
+
let model, webcam, labelContainer, maxPredictions;
|
21 |
+
// Load the image model and setup the webcam
|
22 |
+
async function init() {
|
23 |
+
document.getElementById("startbutton").style.visibility = "hidden";
|
24 |
+
const modelURL = URL + "model.json";
|
25 |
+
const metadataURL = URL + "metadata.json";
|
26 |
+
// load the model and metadata
|
27 |
+
model = await tmImage.load(modelURL, metadataURL);
|
28 |
+
maxPredictions = model.getTotalClasses();
|
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 |
+
// append elements to the DOM
|
36 |
+
document.getElementById("webcam-container").appendChild(webcam.canvas);
|
37 |
+
labelContainer = document.getElementById("label-container");
|
38 |
+
for (let i = 0; i < maxPredictions; i++) { // and class labels
|
39 |
+
labelContainer.appendChild(document.createElement("div"));
|
40 |
+
}
|
41 |
+
}
|
42 |
+
async function loop() {
|
43 |
+
webcam.update(); // update the webcam frame
|
44 |
+
await predict();
|
45 |
+
window.requestAnimationFrame(loop);
|
46 |
+
}
|
47 |
+
// run the webcam image through the image model
|
48 |
+
async function predict() {
|
49 |
+
// predict can take in an image, video or canvas html element
|
50 |
+
const prediction = await model.predict(webcam.canvas);
|
51 |
+
for (let i = 0; i < maxPredictions; i++) {
|
52 |
+
const classPrediction =
|
53 |
+
prediction[i].className + ": " + prediction[i].probability.toFixed(2);
|
54 |
+
labelContainer.childNodes[i].innerHTML = classPrediction;
|
55 |
+
}
|
56 |
+
}
|
57 |
+
</script>
|
58 |
</div>
|
59 |
</body>
|
60 |
+
</html>
|