Face Detection and Tracking With Arduino
and OpenCV
#include <Servo.h> //Used to control the Pan/Tilt Servos
//These are variables that hold the servo IDs.
char tiltChannel=0, panChannel=1;
//These are the objects for each servo.
Servo servoTilt, servoPan;
//This is a character that will hold data from the Serial port.
char serialChar=0;
void setup(){
servoTilt.attach(2); //The Tilt servo is attached to pin 2.
servoPan.attach(3); //The Pan servo is attached to pin 3.
servoTilt.write(90); //Initially put the servos both
servoPan.write(90); //at 90 degress.
Serial.begin(57600); //Set up a serial connection for 57600 bps.
}
void loop(){
while(Serial.available() <=0); //Wait for a character on the serial
port.
serialChar = Serial.read(); //Copy the character from the serial port
to the variable
if(serialChar == tiltChannel){ //Check to see if the character is the
servo ID for the tilt servo
while(Serial.available() <=0); //Wait for the second command byte from
the serial port.
servoTilt.write(Serial.read()); //Set the tilt servo position to the
value of the second command byte received on the serial port
}
else if(serialChar == panChannel){ //Check to see if the initial serial
character was the servo ID for the pan servo.
while(Serial.available() <= 0); //Wait for the second command byte
from the serial port.
servoPan.write(Serial.read()); //Set the pan servo position to the
value of the second command byte received from the serial port.
}
//If the character is not the pan or tilt servo ID, it is ignored.
}
}
Arduino-OpenCV-Human-Follower
1. Dependencies
2. Hardware
3. Setup and Algorithm
Dependencies
PySerial
OpenCV for Python
Hardware
Arduino Uno
Chassis
webcam
motor driver
The assembled chassis with webcam looks like this.
Setup and Algorithm
The project uses OpenCV library with python for building an algorithm to detect humans by
tracking faces. The hardware is based on Arduino microcontroller and an external webcam
connected via an intermediate personal computer where all the image processing takes place.
Both the Arduino and the webcam are mounted on a chassis.
Based on the position and size of the face detected, signals from the computer are serially
communicated to the Arduino which in turn controls the bot to move accordingly to keep
tracking the human face