For this project I created a system that follows an individual when in the camera’s field of vision, and detects when the user touches his/her nose. Video is displayed below:The project consists of a Processing application (processing.org), an Arduino application (arduino.cc), and a hardware component. The computer vision component is processed in the Processing application and the user’s physical location and state (nose touch or no touch) is passed to the Arduino application which drives the hardware. The following Processing code uses the OpenCV framwork (http://www.intel.com/technology/computing/opencv/) and the Face Detect Library for Processing by Jaegon Lee (http://tokage.cafe24.com/facedetect/)
import FaceDetect.*;
import processing.serial.*;
FaceDetect fd;
Serial port;
int viewingAngle = 75; //Camera's Viewing Angle
int MAX = 4; // Max number of faces detected at once (for game)
int sPosition = 90; //servo starting position
int[] x = new int[MAX];
int[] y = new int[MAX];
int[] r = new int[MAX];
int[][] Faces = new int[MAX][3];
void setup(){
size(640,480);
fd = new FaceDetect();
fd.start(width,height, 10);
println(fd.version());
noStroke();
println(Serial.list()); // List COM-ports
//select second com-port from the list
port = new Serial(this, Serial.list()[0], 19200);
}
void draw(){
background(0);
Faces = fd.detect();
int count = Faces.length;
if (count>0) {
for (int i = 0;i<count;i++) {
x[i] = Faces[i][0];
y[i] = Faces[i][1];
r[i] = Faces[i][2] * 2;
println(i+1 + ": " + x[i]+", "+y[i] + ", " + r[i]);
int divider = (width)/viewingAngle;
int sPosition= x[i]/divider; //Calculate servo postion from Face X Coordinate
sPosition = sPosition - (sPosition%1);
//Output the servo position ( from 0 to ViewingAngle)
port.write("s"+sPosition);
}
}
int[] img = fd.image();
loadPixels();
arraycopy(img,pixels);
updatePixels();
if (count == 0) //identifies nose touch (no faces recognized)
{port.write("w");
delay(250); }
strokeWeight(2);
stroke(255,200,0);
noFill();
for(int i =0;i<count;i++) {
ellipse(x[i],y[i],r[i],r[i]);
}
}
public void stop(){
fd.stop();
super.stop();
}
The Arduino code to communicate with the processing code and drive the servo motor and LED eyeball: (note this code requires installation of the Arduino “Servo” library)
#include
Servo servo1;
int ledPin = 13;
void setup() {
servo1.attach(14);
servo1.setMaximumPulse(2500);
Serial.begin(19200);
Serial.print("Ready");
}
void loop() {
static int v = 0;
if ( Serial.available()) {
char ch = Serial.read();
switch(ch) {
case '0'...'9':
v = v * 10 + ch - '0';
break;
case 's':
servo1.write(v);
v = 0;
break;
case 'w':
blink();
v = 0;
}
}
Servo::refresh();
}
void blink(){
digitalWrite(ledPin, HIGH); // sets the LED on
delay(100); // waits for .1 second
digitalWrite(ledPin, LOW); // sets the LED off
delay(100); // waits for .1 second
}
(the include file should be “Servo.H”, but I cant get it to show up in the sourecode editor)
Finally, the hardware component consisted of just an aluminum rod, rubber eyeball, red LED, and a servo motor. The Servo motor was connected directly to the Arduino board in the following manner: Orange: 5V; Black: Ground; Yellow: Analog Input 0;
The Led was connected to Ground and Digital Pin 13.