-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
268 lines (220 loc) · 6.74 KB
/
script.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
let portSel = document.getElementById("port-sel");
let connectBtn = document.getElementById("port-btn");
let scanBtn = document.getElementById("scan-btn");
let modelInp = document.getElementById("model-inp");
let modelBtn = document.getElementById("model-btn");
let modelContainer = document.getElementById("model-container");
let portContainer = document.getElementById("port-container");
// Classifier Variable
let classifier;
// Video
let video;
let flippedVideo;
// To store the classification
let label = "";
async function getModel() {
return new Promise((resolve, reject) => {
modelBtn.addEventListener('click', async () => {
try {
// test model: "https://teachablemachine.withgoogle.com/models/jzsaD4-DF/"
// console.log(modelInp.value)
classifier = ml5.imageClassifier(modelInp.value + 'model.json');
modelContainer.style.display = "none";
portContainer.style.display = "block";
let res = await fetch(modelInp.value + "metadata.json");
let metadata = await res.json();
createConfidenceGraph(metadata.labels)
resolve(classifier)
} catch (e) {
alert(`make sure ur model link is something like: \nhttps://teachablemachine.withgoogle.com/models/abcdefg/
\nreload and try again :3
\n${e}`)
reject(e)
}
})
})
}
async function getPort(ports) {
return new Promise((resolve, reject) => {
let port;
scanBtn.addEventListener('click', async () => {
// Prompt user to select any serial port.
port = await navigator.serial.requestPort();
if (!ports.includes(port)) ports.push(port);
portSel.innerHTML = ''
ports.forEach((p, i) => {
let opt = document.createElement("option")
let txtNode = document.createTextNode(
`PID: ${p.getInfo().usbProductId} VID: ${p.getInfo().usbVendorId}`
)
opt.appendChild(txtNode);
opt.value = i;
portSel.appendChild(opt);
})
portSel.value = ports.length - 1
})
connectBtn.addEventListener('click', async () => {
// console.log(sel.value)
if (ports.length) {
port = ports[portSel.value]
} else {
try {
port = await navigator.serial.requestPort();
} catch (e) {
alert(`${e} \nmake sure ur arduino got connect and then reload the page~~`);
reject(e)
}
}
modelContainer.style.display = "none";
portContainer.style.display = "none";
resolve(port);
})
})
}
function createConfidenceGraph(labels) {
let graphContainer = document.createElement("div");
// graphContainer.style.backgroundColor = "pink"
graphContainer.style.width = "100%"
graphContainer.id = "graph-container"
document.body.appendChild(graphContainer);
labels.forEach(label => {
/*
graphContainer
graphDiv
labelTxt
graph
graphDiv
labelTxt
graph
*/
let graphDiv = document.createElement("div");
graphDiv.style.width = "100%"
graphDiv.style.height = "50px"
graphDiv.classList.add("graph-div")
graphContainer.appendChild(graphDiv);
let labelTxtDiv = document.createElement("div");
let labelTxt = document.createTextNode(label);
labelTxtDiv.classList.add("graph-label");
labelTxtDiv.appendChild(labelTxt);
graphDiv.appendChild(labelTxtDiv);
let graph = document.createElement("div");
graph.style.width = "300px";
graph.style.height = "50px";
graphDiv.appendChild(graph)
let graphBar = document.createElement("div");
graphBar.id = `graph-${label}`;
graphBar.style.width = "0%";
graphBar.classList.add("graph-bar")
graph.appendChild(graphBar)
// console.log(label)
})
}
async function setup() {
createCanvas(320, 260);
modelContainer.style.display = "block";
portContainer.style.display = "none";
// Get all serial ports the user has previously granted the website access to.
const ports = await navigator.serial.getPorts();
if (ports.length) {
ports.forEach((p, i) => {
let opt = document.createElement("option")
let txtNode = document.createTextNode(
`PID: ${p.getInfo().usbProductId} VID: ${p.getInfo().usbVendorId}`
)
opt.appendChild(txtNode);
opt.value = i;
portSel.appendChild(opt);
// console.log(p)
// console.log(Object.keys(p))
// console.log(p.getInfo())
})
} else {
// no ports found
// portSel.style.display = "none";
}
classifier = await getModel();
let port = await getPort(ports);
// console.log(port)
// Wait for the serial port to open.
await port.open({ baudRate: 9600 });
// Create the video
video = createCapture(VIDEO);
video.size(320, 240);
video.hide();
flippedVideo = ml5.flipImage(video);
const textDecoder = new TextDecoderStream();
const textEncoder = new TextEncoderStream();
const readableStreamClosed = port.readable.pipeTo(textDecoder.writable);
const writableStreamClosed = textEncoder.readable.pipeTo(port.writable);
const reader = textDecoder.readable
.pipeThrough(new TransformStream(new LineBreakTransformer()))
.getReader();
const writer = textEncoder.writable.getWriter();
// Start classifying
await classifyVideo(port, readableStreamClosed, writableStreamClosed, reader, writer);
}
function draw() {
background(0);
// Draw the video
if (flippedVideo) {
image(flippedVideo, 0, 0);
} else {
text("Loading...", width / 2, height / 2);
if (mouseIsPressed == true) {
fill(100);
} else {
fill(255);
}
ellipse(mouseX, mouseY, 25, 25)
}
// Draw the label
fill(255);
textSize(16);
textAlign(CENTER);
if (label) text(label, width / 2, height - 4);
}
// Get a prediction for the current video frame
async function classifyVideo(port, readableStreamClosed, writableStreamClosed, reader, writer) {
flippedVideo = ml5.flipImage(video)
classifier.classify(flippedVideo, async (error, results) => {
// If there is an error
if (error) {
console.error(error);
return;
}
// The results are in an array ordered by confidence.
// console.log(results[0]);
label = results[0].label;
results.forEach(result => {
document.getElementById(`graph-${result.label}`).style.width = `${result.confidence.toFixed(4) * 100}%`
document.getElementById(`graph-${result.label}`).innerHTML
= `${(result.confidence * 100).toString().slice(0, 4)}%`
// document.getElementById(`graph-${result.label}`).innerHTML = result.confidence
})
// Classifiy again!
await classifyVideo(port, readableStreamClosed, writableStreamClosed, reader, writer);
});
flippedVideo.remove();
if (label) {
// console.log(label)
try {
await writer.write(label + '\n');
// // READ DATA SENT BY ARDUINO
// while (true) {
// const { value, done } = await reader.read();
// // console.log('reading')
// if (done) {
// // Allow the serial port to be closed later.
// reader.releaseLock();
// break;
// }
// if (value) {
// console.log(value);
// }
// }
} catch (error) {
// TODO: Handle non-fatal read error.
console.error(error)
}
}
}