Project tutorial
Face Tracking Camera

Face Tracking Camera © GPL3+

When it sees you, it won't stop following!

  • 6,475 views
  • 14 comments
  • 48 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
×1
webcam
×1
rubber band
×1
Adafruit industries ada63 image 75px
9V 1A Switching Wall Power Supply
×1
MG-90s servos
×2
2.1*5.5mm jack plug
×1
Stmicroelectronics l7805cv image
Linear Regulator (7805)
×1
M2*10mm self tapping screws
×15
paperclip
×1
31ac4603 40
Hook Up Wire Kit, 22 AWG
×1
pin header
×1
Kingbright wp7113srd d image
5 mm LED: Red
×1
Kingbright wp7113sgd
5 mm LED: Green
×1
Led technology l03r5000q1 image
5 mm LED: Yellow
×1
4415447 jg5sklyzsj
Resistor 220 ohm
×3
heat-shrink tubing
×1

Necessary tools and machines

4158215
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
4550736
Plier, Long Nose
09507 01
Soldering iron (generic)
multimeter
screwdriver
3drag
3D Printer (generic)

Apps and online services

About this project

A few months ago, I modelled and printed one of my colleague's head for fun. I thought It would be funny to push the joke and build a mechanism for this head which can detect people and keep staring at them as they move around the room.

I struggled to make the mechanism small enough to fit my 3D print so instead of locking the project in a cupboard forever and forget about it, I decided to make an easy to assemble model with a easy to use software for everyone to enjoy and build upon.

How does it work?

The camera moves using two servos driven by an Arduino Uno. The camera is plugged to a computer where a software tries to find faces in the images received from the camera.

If a face is found, the software will send a message to the Arduino to make the camera move in order to get the detected face in the centre of the image.

If no face is found, the software will send a message to the Arduino to make it move to a random position.

I tried to make the software quite flexible with option such as:

  • modifiable servo ranges
  • possibility to invert the servos rotation
  • manual control

Hopefully this will make it easy to reuse it for other purposes.

I also added 3 LEDs that show the state of the detection, red for no detection, yellow when a face is detected but not in the center of the image, and green when a face is detected and in the center of the image.

The LEDs are not very interesting on their own but it should be easy to modify the device to perform a useful action instead of just turning ON and OFF a few lights.

The communication between the Arduino and the software is done through serial communication (via USB).

The face detection software was written in Python. I left all the Python files on GitHub in case you want to have a look at the code (warning : it's probably not great, I am learning on my own) or reuse them for your own project.

If Python isn't your thing you can also download the executable version on my website.

If you want to build your own, check the following videos. I tried to make them as easy as possible to follow, I hope I did a good job at that.

Everything you need

Assembly

Wiring

Software and setup

Code

Arduino codeC/C++
This is the code that will be uploaded to the arduino
//Sketch based on work done by Robin2 on the arduino forum
//more info here
//https://forum.arduino.cc/index.php?topic=225329.msg1810764#msg1810764


#include <Servo.h>

Servo panServo;
Servo tiltServo; 

byte redledPin = 2;
byte yellowledPin = 3;
byte greenledPin = 4;

const byte buffSize = 40;
char inputBuffer[buffSize];
const char startMarker = '<';
const char endMarker = '>';
byte bytesRecvd = 0;
boolean readInProgress = false;
boolean newDataFromPC = false;

float panServoAngle = 90.0;
float tiltServoAngle = 90.0;
int LED_state = 0;

//8=============D

void setup() {
  Serial.begin(115200);
  
  panServo.attach(8);
  tiltServo.attach(9);

  pinMode(redledPin, OUTPUT);
  pinMode(yellowledPin, OUTPUT);
  pinMode(greenledPin, OUTPUT);
  
  //moveServo();
  start_sequence();

  delay(200);
  
  Serial.println("<Hasta la vista baby>"); // send message to computer
}

//8=============D

void loop() {
  getDataFromPC();
  replyToPC();
  moveServo();
  setLED();
}

//8=============D

void getDataFromPC() {

    // receive data from PC and save it into inputBuffer
    
  if(Serial.available() > 0) {

    char x = Serial.read();              //read char from serial
      
    if (x == endMarker) {                //look for end marker
      readInProgress = false;            //if found, set read in progress true (will stop adding new byte to buffer)
      newDataFromPC = true;              //let arduino know that new data is available
      inputBuffer[bytesRecvd] = 0;       //clear input buffer
      processData();                      // process data in buffer
    }
    
    if(readInProgress) {
      inputBuffer[bytesRecvd] = x;      //populate input buffer with bytes
      bytesRecvd ++;                    //increment index
      if (bytesRecvd == buffSize) {     //when buffer is full
        bytesRecvd = buffSize - 1;      //keep space for end marker
      }
    }

    if (x == startMarker) {              // look for start maker
      bytesRecvd = 0;                    // if found, set byte received to 0
      readInProgress = true;             // set read in progress true
    }
  }
}

//8=============D

void processData() // for data type "<float, float, int>" 
{
  char * strtokIndx; // this is used by strtok() as an index

   strtokIndx = strtok(inputBuffer,",");      // get the first part
   panServoAngle = atof(strtokIndx);         // convert this part to a float

   strtokIndx = strtok(NULL,",");          // get the second part(this continues where the previous call left off)
   tiltServoAngle = atof(strtokIndx);     // convert this part to a float

   strtokIndx = strtok(NULL, ",");      // get the last part
   LED_state = atoi(strtokIndx);          // convert this part to an integer (string to int)
}

//8=============D

void replyToPC() {

  if (newDataFromPC) {
    newDataFromPC = false;
    Serial.print("<");
    Serial.print(panServo.read());
    Serial.print(",");
    Serial.print(tiltServo.read());
    Serial.println(">");
  }
}

//8=============D

void moveServo() 
{
  panServo.write(panServoAngle);
  tiltServo.write(tiltServoAngle);
}

void setLED()
{
  if(LED_state == 2){
    digitalWrite(redledPin, LOW);
    digitalWrite(yellowledPin, HIGH);
    digitalWrite(greenledPin, LOW);
    }
  else if(LED_state == 1){
    digitalWrite(redledPin, LOW);
    digitalWrite(yellowledPin, LOW);
    digitalWrite(greenledPin, HIGH);    
    }
  else if(LED_state == 0){
    digitalWrite(redledPin, HIGH);
    digitalWrite(yellowledPin, LOW);
    digitalWrite(greenledPin, LOW);  
    }  
  else if(LED_state == 3){
    digitalWrite(redledPin, HIGH);
    digitalWrite(yellowledPin, HIGH);
    digitalWrite(greenledPin, HIGH);  
    }  
  else{
    digitalWrite(redledPin, LOW);
    digitalWrite(yellowledPin, LOW);
    digitalWrite(greenledPin, LOW);    
    }
  
  }

//8=============D

  void start_sequence()
  {
    panServo.write(90);
    tiltServo.write(90);
    delay(300);

  
    digitalWrite(redledPin, HIGH);
    delay(100);
    digitalWrite(redledPin, LOW);
    digitalWrite(yellowledPin, HIGH);
    delay(100);
    digitalWrite(yellowledPin, LOW);
    digitalWrite(greenledPin, HIGH);
    delay(100);
  
    digitalWrite(redledPin, LOW);
    digitalWrite(yellowledPin, LOW);
    digitalWrite(greenledPin, LOW);
    delay(100);
    digitalWrite(redledPin, HIGH);
    digitalWrite(yellowledPin, HIGH);
    digitalWrite(greenledPin, HIGH);
    delay(100);
    digitalWrite(redledPin, LOW);
    digitalWrite(yellowledPin, LOW);
    digitalWrite(greenledPin, LOW);
    }
Face detection software
Here is the repository where you can find the files for the face detection software . You can also find a executable version on my website .

Custom parts and enclosures

face tracking camera parts
all the parts needed to built the face tracking camera mini . You can also find them here : https://www.littlefrenchkev.com/download

Schematics

Wiring diagram
Complete rcyazi7xpi

Comments

Similar projects you might like

IoT Face Tracking and Recognition

by vicente zavala

  • 9,867 views
  • 1 comment
  • 24 respects

Face Tracking Using Arduino

Project tutorial by WolfxPac

  • 5,640 views
  • 3 comments
  • 20 respects

IoT Face Tracking and Recognition

Project tutorial by vicente zavala

  • 3,834 views
  • 1 comment
  • 18 respects

Smart Face Tracking Robot Car

Project tutorial by Sebastian Hernandez

  • 8,466 views
  • 7 comments
  • 47 respects

Email 3G/GPRS Camera

Project showcase by ASimon

  • 3,776 views
  • 1 comment
  • 9 respects

RGB Camera Backlight Control With Android Mobile

Project tutorial by DIY Partners

  • 1,945 views
  • 1 comment
  • 8 respects
Add projectSign up / Login