Skip to content

Instantly share code, notes, and snippets.

@Masafuro
Created July 23, 2017 12:42
It need "FaceChaser for Arduino". It need libraries openCV, video, serial, for processing too.
import gab.opencv.*;
import processing.video.*;
import java.awt.*;
Capture video;
OpenCV opencv;
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
void setup() {
size(960, 720);
println(Capture.list());
video = new Capture(this,width/3,height/3,Capture.list()[20]);
opencv = new OpenCV(this, width/3, height/3);
opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
video.start();
String portName = Serial.list()[0];
println(portName);
myPort = new Serial(this, portName, 9600);
}
void draw() {
scale(3);
opencv.loadImage(video);
image(video, 0, 0 );
noFill();
stroke(0, 255, 0);
strokeWeight(1);
Rectangle[] faces = opencv.detect();
//println(faces.length);
int[] x = new int[20];
int[] y = new int[20];
boolean exist = false;
for (int i = 0; i < faces.length; i++) {
x[i] = faces[i].x + faces[i].width/2;
y[i] = faces[i].y + faces[i].height/2;
println(i,x[i] , y[i]);
exist = true;
rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
}
if(exist == true){
if( x[0] > 170){
myPort.write('H');
println('H');
}else if(x[0] < 130){
myPort.write('L');
println('L');
} else{
myPort.write('C');
println('C');
}
} else {
myPort.write('N');
println('N');
}
}
void captureEvent(Capture c) {
c.read();
}
void sendCommand(){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment