-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
146 lines (121 loc) · 4.86 KB
/
index.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!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>Document</title>
<style>
*{padding: 0; margin: 0;}
html,body{overflow: hidden;}
canvas{position: relative; left: 50%; transform: translateX(-50%);}
#label-container{
margin-top: 14px;
margin-bottom: 14px;
font-size: 25px;
text-align: center;
font-weight: bold;
}
button{width: 100vw;height: 100vh;background: rgba(255, 54, 54, 0.1);position: fixed; top: 1px;text-align: center;}
</style>
</head>
<body>
<div id="label-container"></div>
<div id="webcam-container" ></div>
<!-- <button class="button">.</button> -->
<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>
<script type="text/javascript">
// More API functions here:
// https://github.com/googlecreativelab/teachablemachine-community/tree/master/libraries/image
// the link to your model provided by Teachable Machine export panel
const URL = "./my_model/";
let model, webcam, labelContainer, maxPredictions;
// Load the image model and setup the webcam
async function init() {
const modelURL = URL + "model.json";
const metadataURL = URL + "metadata.json";
// load the model and metadata
// Refer to tmImage.loadFromFiles() in the API to support files from a file picker
// or files from your local hard drivez
// Note: the pose library adds "tmImage" object to your window (window.tmImage)
model = await tmImage.load(modelURL, metadataURL);
maxPredictions = model.getTotalClasses();
// Convenience function to setup a webcam
const flip = true; // whether to flip the webcam
webcam = new tmImage.Webcam(520, 520, flip); // width, height, 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++) { // and class labels
labelContainer.appendChild(document.createElement("div"));
}
}
async function loop() {
webcam.update(); // update the webcam frame
await predict();
window.requestAnimationFrame(loop);
}
function delay(n){
return new Promise(function(resolve){
setTimeout(resolve,n*1000);
});
}
let cnt = 0;
let numArray = new Array();
numArray[0] = 0;
numArray[2] = 1;
const playAudio = async (fileName) => {
if(numArray[2] == 1){
numArray[2] = 0;
let ms = 0;
let audio = new Audio(fileName + ".mp3");
if(numArray[0] == fileName){
audio.play();
await setTimeout( () => {
numArray[2] = 1;
}, 3000);
// 1000이 1초
}else{
ms = 1000;
numArray[0] = fileName
audio.play();
await setTimeout( () => {
audio.muted = true
numArray[2] = 1;
}, ms);
}
}else if(numArray[2] == 0){
return 0;
}
}
let Delay = 3;
// run the webcam image through the image model
async function predict() {
// predict can take in an image, video or canvas html element
const prediction = await model.predict(webcam.canvas);
if (prediction[0].probability.toFixed(2) >= 0.80) {
labelContainer.childNodes[0].innerHTML = "143"
// var audio = new Audio('143.mp3')
playAudio('143')
} else if (prediction[1].probability.toFixed(2) >= 0.80) {
labelContainer.childNodes[0].innerHTML = "401"
playAudio('401')
} else if (prediction[2].probability.toFixed(2) >= 0.80) {
labelContainer.childNodes[0].innerHTML = "406"
playAudio('406')
} else if (prediction[3].probability.toFixed(2) >= 0.80) {
labelContainer.childNodes[0].innerHTML = "9007"
playAudio('9007')
}
else {
labelContainer.childNodes[0].innerHTML = "알 수 없음"
}
}
window.onload = init
</script>
</body>
</html>