-
Notifications
You must be signed in to change notification settings - Fork 0
/
identify.js
67 lines (59 loc) · 1.82 KB
/
identify.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
// **********
// open editor.p5js.org and paste this code
// update com port number and training model url to continue
// Download and unzip p5.js serial control program from
// - https://github.com/p5-serial/p5.serialcontrol/releases/download/0.1.2/p5.serialcontrol-win32-x64.zip.
// Connect your device to USB and run the p5.serialcontrol.exe
// select the right serial port
// Press the run button on the editor.p5js.org window
let portName = 'COM..'; // serial port
let imageModelURL = 'https://teachablemachine.withgoogle.com/models/...'; // model URL
// **********
let serial;
let classifier;
let video;
let flippedVideo;
let label = "";
function preload() {
classifier = ml5.imageClassifier(imageModelURL + 'model.json');
}
function setup() {
serial = new p5.SerialPort();
serial.list();
serial.open(portName);
serial.on('list', gotList);
createCanvas(320, 260);
video = createCapture(VIDEO);
video.size(320, 240);
video.hide();
flippedVideo = ml5.flipImage(video)
classifyVideo();
}
function draw() {
background(0);
image(flippedVideo, 0, 0);
fill(255);
textSize(16);
textAlign(CENTER);
text("Result: " + label, width / 2, height - 4);
}
function classifyVideo() {
flippedVideo = ml5.flipImage(video)
classifier.classify(flippedVideo, gotResult);
}
function gotResult(error, results) {
if (error) {
console.error(error);
return;
}
label = results[0].label;
print("Result:", String(results[0].label))
serial.write(String(results[0].label));
classifyVideo();
}
function gotList(thelist) {
console.log("List of Serial Ports:");
for (let i = 0; i < thelist.length; i++) {
console.log(i + " " + thelist[i]);
}
}