-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexH.js
191 lines (144 loc) · 6.24 KB
/
indexH.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Cu and Zn elements
//Teachable Machine model Url
const URL = "https://teachablemachine.withgoogle.com/models/nbiNDnta_/";
let model, webcam, labelContainer, maxPredictions;
//is started Webcam flag
let startCamFlag = true;
//is started Upload flag
let startUpFlag = true;
let camButton = document.getElementById("camButton"), upButton = document.getElementById("upButton");
function startCamHandler() {
if (startUpFlag) {
if (startCamFlag) init();
else stop();
startCamFlag = !startCamFlag;
}
}
function startUpHandler() {
if (startCamFlag) {
if (startUpFlag) openUploadImage();
else closeUploadImage();
startUpFlag = !startUpFlag;
}
}
// 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 drive
// 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(200, 200); // 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");
labelContainer.appendChild(document.createElement("div"));
//Changing button text
camButton.textContent = "Stop";
//Showing containers //6oth line
document.getElementById("webcam-container").className = "";
document.getElementById("label-container").className = "";
document.getElementById("fullAnswer").className="";
}
async function stop() {
await webcam.stop();
document.getElementById("webcam-container").removeChild(webcam.canvas);
labelContainer = document.getElementById("label-container");
console.log(labelContainer.children);
labelContainer.removeChild(labelContainer.children[0]);
camButton.textContent = "Start Webcam";
//Change Answer
document.getElementById("fullAnswer").innerHTML="";
//Hiding containers
document.getElementById("webcam-container").className = "d-none";
document.getElementById("label-container").className = "d-none";
document.getElementById("fullAnswer").className="d-none";
}
async function loop() {
webcam.update(); // update the webcam frame
await predict();
window.requestAnimationFrame(loop);
}
// run the webcam image through the image model
async function predict(imageModel = webcam.canvas) {
let highestProbability;
let lastProbability = 0;
// predict can take in an image, video or canvas html element
const prediction = await model.predict(imageModel);
console.log(prediction);
for (let i = 0; i < maxPredictions; i++) {
if (prediction[i].probability.toFixed(2) > lastProbability)
highestProbability = i;
lastProbability = prediction[i].probability.toFixed(2);
}
const className = prediction[highestProbability].className;
let classNameShow = className;
labelContainer.childNodes[0].innerHTML = className;
//Predict the answer
if(classNameShow=="Copper"){
document.getElementById("fullAnswer").innerHTML="Atomic Number: 29 <br> Atomic Mass: 63.546 amu <br> Group: 11 <br> Electronic Configuration: [Ar] 3d"+'10'.sup()+" 4s"+'1'.sup();
}
else if(classNameShow=="Zinc"){
document.getElementById("fullAnswer").innerHTML="Atomic Number: 30 <br> Atomic Mass: 65.38 amu <br> Group: 12 <br> Electronic Configuration: [Ar] 3d"+'10'.sup()+" 4s"+'2'.sup();
}
}
function openUploadImage() {
//Showing elements
document.getElementById("inp").className = "";
document.getElementById("canvas").className = "";
document.getElementById("fullAnswer").className="";
//Changing button text
upButton.textContent = "Close";
}
function closeUploadImage() {
labelContainer = document.getElementById("label-container");
let canvas = document.getElementById("canvas"),input = document.getElementById("inp");
//Hiding input
input.className = "d-none";
input.value = null;
//Removing Label
labelContainer.className = "d-none";
if (labelContainer.children.length > 0)
labelContainer.removeChild(labelContainer.children[0]);
canvas.className = "d-none";
//Removing and changing the answer
document.getElementById("fullAnswer").innerHTML="";
document.getElementById("fullAnswer").className="d-none";
//Clear canvas
const context = canvas.getContext("2d");
context.clearRect(0, 0, canvas.width, canvas.height);
upButton.textContent = "Upload Image";
}
//Uploading Image
document.getElementById("inp").onchange = function (e) {
var img = new Image();
img.onload = draw;
img.onerror = failed;
img.src = window.URL.createObjectURL(this.files[0]);
};
async function draw() {
var canvas = document.getElementById("canvas");
canvas.width = this.width;
canvas.height = this.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(this, 0, 0);
const modelURL = URL + "model.json";
const metadataURL = URL + "metadata.json";
model = await tmImage.load(modelURL, metadataURL);
maxPredictions = model.getTotalClasses();
labelContainer = document.getElementById("label-container");
labelContainer.appendChild(document.createElement("div"));
labelContainer.className = "";
await predict(canvas);
}
function failed() {
console.error("The provided file couldn't be loaded as an Image media");
}