-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
49 lines (43 loc) · 1.06 KB
/
sketch.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
const modelURL = 'https://teachablemachine.withgoogle.com/models';
const serialPort = 'COM3';
let classifier;
let serial;
let video;
let flippedVideo;
let label;
function preload() {
classifier = ml5.imageClassifier(modelURL + 'model.json');
serial = new p5.SerialPort();
}
function setup() {
serial.open(serialPort);
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(label, width / 2, height - 4);
}
function classifyVideo() {
flippedVideo = ml5.flipImage(video)
classifier.classify(flippedVideo, gotResult);
flippedVideo.remove();
}
function gotResult(error, results) {
if (error) {
console.error(error);
return;
}
label = String(results[0].label);
console.log(label);
serial.write(label);
classifyVideo();
}