-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.html
96 lines (79 loc) · 3.34 KB
/
camera.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>🚀 Reboot Rebels</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="camera.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="header">
<img src="imgs/logo.svg" alt="logo">
<ul>
<li><a href="/index.html">Home</a></li>
<li><a href="/ar.html">AR</a></li>
<li><a href="#">Product</a></li>
</ul>
</div>
<div>Click start and wait woo</div>
<button type="button" onclick="init()">Start</button>
<div id="webcam-container"></div>
<div id="output-container"></div>
<div id="label-container"></div>
<!-- Start javascript yey -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@teachablemachine/[email protected]/dist/teachablemachine-image.min.js"></script>
<!-- start the important javascript -->
<script type="text/javascript">
const URL = "https://teachablemachine.withgoogle.com/models/cKCDnB9SN/";
let model, webcam, labelContainer, maxPredictions;
// turn on the cammm
async function init() {
const modelURL = URL + "model.json";
const metadataURL = URL + "metadata.json";
// access the model
model = await tmImage.load(modelURL, metadataURL);
maxPredictions = model.getTotalClasses();
//webcam settings
const flip = true; // whether to flip the webcam
webcam = new tmImage.Webcam(200, 200, flip);
await webcam.setup(); // request access to the webcam
await webcam.play();
window.requestAnimationFrame(loop);
// append elements to the DOM
document.getElementById("webcam-container").appendChild(webcam.canvas);
labelContainer = document.getElementById("label-container");
for (let i = 0; i < maxPredictions; i++) {
labelContainer.appendChild(document.createElement("div"));
}
}
async function loop() {
webcam.update();
await predict();
window.requestAnimationFrame(loop);
}
// run the webcam image through the image model
async function predict() {
const prediction = await model.predict(webcam.canvas);
for (let i = 0; i < maxPredictions; i++) {
const classPrediction =
prediction[i].className + ": " + prediction[i].probability.toFixed(2);
labelContainer.childNodes[i].innerHTML = classPrediction;
if (prediction[0].probability.toFixed(2) > 0.98) {
document.getElementById("output-container").innerHTML =
"No items recognized";
}
if (prediction[1].probability.toFixed(2) > 0.98) {
window.open("https://www.ecowatch.com/eco-friendly-water-bottles-2649345802.html");
}
if (prediction[2].probability.toFixed(2) > 0.98) {setTimeout(function(){ window.open("https://www.tryingmebag.com/Non-woven-bag?gclid=Cj0KCQjw_8mHBhClARIsABfFgpjZhuMgkdSjHCR0vANDhW8Z5HXFNWn3S0nBnGgd2OYjffjpMDNzV7UaAuWCEALw_wcB"); }, 5000);
}
if (prediction[3].probability.toFixed(2) > 0.98) {
document.getElementById("output-container").innerHTML = "You already used an environmental friendly item, great job!!";
}
}
}
</script>
</body>
</html>