Mid-Senior Engineers: Reinvent your career with Robotics, earning $100k-$200k+ in the next 90 days. Learn More

Face Tracking OpenCV, Python, & Arduino

Liz Miller Learn Robotics

About the Author, Liz Miller, Founder/CEO @ Learn Robotics

Liz graduated with a degree in Robotics Engineering from Worcester Polytechnic Institute and researched drones at UPenn's GRASP Lab. Liz is a former Raytheon Engineer, where she managed major $MM automation projects worldwide. Today, she's the driving force behind Learn Robotics, offering the Robotics Career Blueprint for Engineering Professionals and beginner courses through the Online Robotics Class. Liz is a third-generation entrepreneur who is all about the application of innovation in robotics, automation, and AI. Follow Liz on LinkedIn and Facebook.

Disclosure: Some of the links below are affiliate links. This means that, at zero cost to you, Learn Robotics will earn an affiliate commission if you click through the link and finalize a purchase. Learn Robotics is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a way for websites to earn advertising revenues by advertising and linking to Amazon.com.

Join our Private Discord Server, MakeRobots! Sign Up

In a previous article, I showed you how you can establish communication between Arduino and Python.  In this tutorial, I will show you how you can use OpenCV, Python, and Arduino to detect and track faces. Face tracking can be used in a variety of robotics projects and applications.

Once you learn the basics from this face-tracking OpenCV project, you can use your imagination to put these skills to work! So without wasting any more time, let’s get right into it.

Face Tracking OpenCV Project Materials

For this project, you’ll need an Arduino Uno, servos, pan-tilt kit, breadboard kit, and webcam. (Laptop built-in camera also works.) Along with the hardware components, you will also need the following software:

Set Up the Python Environment

First, we need Python 2.7 up and running. To do this first download and install python 2.7.14.
To check if it is installed correctly Goto: Windows Search >> Type “IDLE” >> Hit Enter.
You should see a Python shell that looks something like the photo below.

python opencv

You can also use the Command Prompt and script from the terminal. In search bar type ‘CMD,’ and hit enter to open Command Prompt. In CMD type >> python and hit enter. The Python interface will look similar to the photo below.

face tracking opencv

If you see an error in CMD, do not worry. You probably need to set the environment variable. You can follow this tutorial here to set up the environment variable.

Robot servo 25kg RDS3225 Metal Gear Digital servo...
equipped with strong copper & aluminum gears,CNC aluminium middle Shell; Professional servo kit easy to design and do all kinds of robot

Install ‘pyserial’, ‘OpenCV” and “NumPy” in Python

To install these modules we will use use pip install. First, open CMD and type the following codes:

>pip install serial
>pip install opencv-python
>pip install numpy

Install each of the modules one by one. These modules will help us detect and recognize objects (face tracking in this case). And, they’ll help us connect our Arduino board to Python.

Face Tracking with Python

Before starting to write code first thing to do is make a new folder as all of the code needs to be stored at the same location. Next, create a new folder, and name it anything you want. Now download the ‘Haarcascade‘ paste it in the folder. Next, download the script given below. Save it as ‘face.py’ in the same folder as haarcascade.

Download the Starter Files for this Project

Note: After downloading the code, I recommend you read through and understand the code yourself. Pay attention to all the comments in the code. These provide necessary instructions.
Face Tracking Robot OpenCV Files
Download Now!


Arduino Face Tracking Code

After the Python script is ready, we need to create an Arduino sketch to control the servos. Refer to the code below. Copy and paste it into a new Arduino sketch.

Save the sketch as ‘servo.ino‘ in the same folder as face.py and haarcascade. Upload the code and move onto the next step to make the connections.

#include<Servo.h>

Servo servoVer; //Vertical Servo
Servo servoHor; //Horizontal Servo

int x;
int y;

int prevX;
int prevY;

void setup()
{
  Serial.begin(9600);
  servoVer.attach(5); //Attach Vertical Servo to Pin 5
  servoHor.attach(6); //Attach Horizontal Servo to Pin 6
  servoVer.write(90);
  servoHor.write(90);
}

void Pos()
{
  if(prevX != x || prevY != y)
  {
    //tune this range to generate map
    int servoX = map(x, 600, 0, 70, 179); 
    //tune this range to generate map
    int servoY = map(y, 450, 0, 179, 95); 

    servoX = min(servoX, 179);
    servoX = max(servoX, 70);
    servoY = min(servoY, 179);
    servoY = max(servoY, 95);
    
    servoHor.write(servoX);
    servoVer.write(servoY);
  }
}

void loop()
{
  if(Serial.available() > 0)
  {
    if(Serial.read() == 'X')
    {
      x = Serial.parseInt();
      if(Serial.read() == 'Y')
      {
        y = Serial.parseInt();
       Pos();
      }
    }
    while(Serial.available() > 0)
    {
      Serial.read();
    }
  }
}

Pan-Tilt Mechanism

I have used a readily available kit for the Pan-Tilt. If you want you can make one yourself using wood or plastic, or you can even 3D print one. I used a kit you can get on Amazon, Gearbest, or Banggood.

pan-tilt servos

You can use our guide to assemble the Pan-Tilt mechanism properly.  Once you finish that, it’s time to move on to the electronics.

Wiring Diagram for Face Tracking OpenCV

The circuit is pretty simple. Just attach two servos to Arduino as shown in the wiring diagram below.

wiring diagram face tracking opencv

The vertical servo signal pin goes to digital pin 5. The horizontal servo signal pin goes to digital pin 6. Power goes to +5V and Ground goes to Ground.

Note: To reduce any jitters, connect an electrolytic capacitor in parallel to the power supply.

Here’s what the final assembly looks like.

final setup face tracking arduino opencv

Test the Face Tracking OpenCV project

After everything is done, the last thing to do is to test to see if it works. First, make sure that the servos are properly connected to the Arduino and sketch has been uploaded.

After the sketch is uploaded, make sure to close the Arduino IDE so that the Serial port is free to connect to Python.

Next, open face.py with Python IDLE and press ‘F5’ to run the code. You can also navigate to the project directory in terminal and run face.py code directly from the command line.

Sale
ELEGOO UNO R3 Project Most Complete Starter Kit...
With more than 200Pcs components; With a precision potentiometer, better quality, power supply module and breadboard
$64.99 −$5.00 $59.99

It will take a few seconds to connect to Arduino. Then you should be able to see a window streaming the webcam. Now the code will detect your face, and the servos will track it. You may have to tune the pan and tilt servo range to best suit the coordinates given by Python & OpenCV. Check out the video to see how you can tune the readings for more dynamic control.

If everything is set up correctly, the servos should move as you move. As an added feature, you can mount the camera directly on the servos. Then the mechanism will mimic your movements while you’re looking directly at it.

That’s all for this tutorial. If you have any questions, be sure to find us on Facebook.

You can also check out our online Arduino Course. It’s designed to help students learn the fundamentals of Arduino to be successful with more advanced projects like this one.

Experienced Engineer (Mechanical, Electrical, Computer, Software): If I offered to help you upgrade your engineering career to robotics, earning $100k-$200k+ in the next 90 days, would you take me up on that offer? Click here for details.
Liz Miller Learn Robotics

🚀 Pre-Launch: Become a "MakeR" with MakeRobots!

Hey Reader, 👋

Liz Miller, Founder/CEO, here with some Exciting News!

Learn Robotics just acquired MakeRobots™, an Online Robotics Community, and are prepping its Official Learn Robotics Debut in Late 2023.

MakeRobots™ is your one-stop-shop for learning, gaining coding, electronics, and robotics skills, connecting, and building robots for one low monthly membership!

Join MakeRobots™ at our Special Pre-launch Rate!
🤖 Access our Private Community & Robotics Courses
💬 Network, Collaborate, Connect with Other Makers
🔓 Only $5.99/month – locked in for life
⏱️ Pre-launch deal is Limited to the first 1,000 subscribers

This is a perfect opportunity for you to get into the fastest growing robotics community on the internet, at our ground-level, pre-launch membership rates.

👇 Click the button below to Claim your Pre-launch Membership and become a MakeR in the MakeRobots Community, today!

Learn Robotics Botly Favicon

MORE LEARN ROBOTICS ARTICLES