-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
151 lines (112 loc) · 3.42 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
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
// --------------------------------------------------------- //
// Classifier Variable
let classifier;
// Model URL
let imageModelURL = 'https://teachablemachine.withgoogle.com/models/eF56XmyGg/';
// Video
let video;
let flippedVideo;
// To store the classification
let label = "";
let oldlabel = "";
// Load the model first
function preload() {
classifier = ml5.imageClassifier(imageModelURL + 'model.json');
}
// --------------------------------------------------------- //
// variable for p5.SerialPort object
let serial;
// variable por serialPortName
let serialPortName = 'COM4';
// variable for HTML DOM input for serial port name
let htmlInputPortName;
// variable for HTML DOM button for entering new serial port name
let htmlButtonPortName;
// this is the message that will be sent to the Arduino:
let outMessage = '0';
// --------------------------------------------------------- //
function setup() {
// small canvas
createCanvas(300, 300);
// Create the video
video = createCapture(VIDEO);
video.size(320, 240);
video.hide();
flippedVideo = ml5.flipImage(video)
// Start classifying
classifyVideo();
// --------------------------------------------------------- //
// p5.js to create HTML input and set initial value
htmlInputPortName = createInput(serialPortName);
// p5.js to create HTML button and set message
button = createButton('update port');
// p5.js to add callback function for mouse press
button.mousePressed(updatePort);
// create instance of p5.SerialPort
serial = new p5.SerialPort();
// print version of p5.serialport library
console.log('p5.serialport.js ' + serial.version);
// Get a list the ports available
// You should have a callback defined to see the results. See gotList, below:
serial.list();
// Assuming our Arduino is connected, open the connection to it
serial.openPort(serialPortName);
// When you get a list of serial ports that are available
serial.on('list', gotList);
// When you some data from the serial port
serial.on('data', gotData);
}
function draw() {
background(0);
// Draw the video
image(flippedVideo, 0, 0);
// Draw the label
fill(255);
textSize(16);
textAlign(CENTER);
text(label, width / 2, height - 4);
// place example name on the top of
}
// Get a prediction for the current video frame
function classifyVideo() {
flippedVideo = ml5.flipImage(video)
classifier.classify(flippedVideo, gotResult);
}
// When we get a result
function gotResult(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;
if(label!=oldlabel){
serial.write(label);
oldlabel = label;
}
// Classifiy again!
classifyVideo();
}
// callback function to update serial port name
function updatePort() {
// retrieve serial port name from the text area
serialPortName = htmlInputPortName.value();
// open the serial port
serial.openPort(serialPortName);
}
// Got the list of ports
function gotList(thelist) {
console.log('List of Serial Ports:');
// theList is an array of their names
for (let i = 0; i < thelist.length; i++) {
// Display in the console
console.log(i + ' ' + thelist[i]);
}
}
// Called when there is data available from the serial port
function gotData() {
let currentString = serial.readLine();
console.log(currentString);
}