xxxxxxxxxx
178
// Classifier Variable
let classifier;
// Model URL
let imageModelURL = 'https://teachablemachine.withgoogle.com/models/OtzSr9_AN/';
// Video
let video;
let flippedVideo;
// To store the classification
let label = "";
var serial; // variable to hold an instance of the serialport library
var portName = '/dev/tty.usbmodem101'; // fill in your serial port name here
// var portName = '/dev/tty.HC-05-DevB';
var inData; // variable to hold the input data from Arduino
var outByte = 0;
// Load the model first
function preload() {
classifier = ml5.imageClassifier(imageModelURL + 'model.json');
}
function setup() {
createCanvas(420, 460);
// Create the video
video = createCapture(VIDEO);
video.size(320, 240);
video.hide();
flippedVideo = ml5.flipImage(video);
// Start classifying
classifyVideo();
//set up communication port
serial = new p5.SerialPort(); // make a new instance of the serialport library
serial.on('list', printList); // set a callback function for the serialport list event
serial.on('connected', serverConnected); // callback for connecting to the server
serial.on('open', portOpen); // callback for the port opening
serial.on('data', serialEvent); // callback for when new data arrives
serial.on('error', serialError); // callback for errors
serial.on('close', portClose); // callback for the port closing
serial.list(); // list the serial ports
serial.open(portName); // open a serial port
// numberResult(label);
}
function draw() {
background(255);
fill(0);
text("incoming value: " + inData, 130, 360);
// Draw the video
image(flippedVideo, 0, 0);
// Draw the label
textSize(16);
textAlign(CENTER);
text(label, width / 3, height - 200);
}
// Get a prediction for the current video frame
function classifyVideo() {
flippedVideo = ml5.flipImage(video)
classifier.classify(flippedVideo, gotResult);
flippedVideo.remove();
}
// 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;
numberResult(label);
// Classifiy again!
classifyVideo();
}
function numberResult(label) {
switch (label) {
case "Waving":
outByte = 1;
serial.write(outByte);
//console.log(outByte);
break;
case "1 Finger":
outByte = 2;
serial.write(outByte);
//console.log(outByte);
break;
case "2 Fingers":
outByte = 3;
serial.write(outByte);
//console.log(outByte);
break;
case "3 Fingers":
outByte = 4;
serial.write(outByte);
//console.log(outByte);
break;
case "4 Fingers":
outByte = 5;
serial.write(outByte);
//console.log(outByte);
break;
case "Fist/No Fingers":
outByte = 6;
serial.write(outByte);
//console.log(outByte);
break;
case "No Hand Detected":
outByte = 7;
serial.write(outByte);
//console.log(outByte);
break;
}
}
// Following functions print the serial communication status to the console for debugging purposes
function printList(portList) {
// portList is an array of serial port names
for (var i = 0; i < portList.length; i++) {
// Display the list the console:
print(i + " " + portList[i]);
}
}
function serverConnected() {
print('connected to server.');
}
function portOpen() {
print('the serial port opened.')
}
function serialEvent() {
inData = Number(serial.read());
}
function serialError(err) {
print('Something went wrong with the serial port. ' + err);
}
function portClose() {
print('The serial port closed.');
}
// function mouseDragged() {
// // map the mouseY to a range from 0 to 255:
// outByte = int(map(mouseY, 0, height, 0, 255));
// // send it out the serial port:
// serial.write(outByte);
//}
function keyTyped() {
if (key == 'a') {
outByte = 0;
serial.write(outByte);
console.log(outByte);
} else {
outByte = 1;
serial.write(outByte);
console.log(outByte);
}
}