Learn How to Create a Simple Face Tracking System in Python

Face Detection

Learn How to Create a Simple Face Tracking System in Python

Sabina Pokhrel / January 14, 2020

Face tracking is detecting a set of faces in frame 1 of a video, establishing a correspondence between the frames, and maintaining a unique ID for each of the faces throughout the video.

It is being used in different scenarios ranging from video surveillance where a target face is tracked to observe the target person’s movement; privacy-protection where a face is replaced or blurred; and entertainment where the target face is replaced with cartoons, or blended with a super star’s face.

Face tracking on image (Original image by Brooke Cagle on Unsplash)
Face tracking for privacy protection

Imagine what you could do with face tracking …💭 create your our home surveillance system so that when an unrecognized person walks in your front door, it keeps a track of his/her movements and alerts you, count the number of people walking in and out of your building, or create an app that replaces your face with a smiley 😃 while creating a dance video to share on your YouTube channel.

Face tracking and replacement on a dance video (Video by: cottonbro from Pexels, Smiley Icon by: Roundicons)

In this post, I will teach you how to create a simple face tracking system in Python by dividing the task into two steps:

1. Face detection

2. Face tracking

By the end of this post, you will be able to detect faces in the first frame and track all the detected faces in the subsequent frames.

NOTE: For this post, I will use Raspberry Pi 3B+ and run all my code on it.

Let’s get started.

Face Detection

In order to track faces, we must first be able to detect faces in any given image.

For our face detection task, we will use Xailient Face Detector. You can refer to this post to download and install the Face Detector SDK.

Face Detection on Image

Let’s start by creating a program to detect faces from a static image. Here is the code to read an image, detect faces in the image, draw a bounding box around the detected faces and then save it back to the disk.

Face Detection on Video Stream

Now that we have a program that detects static faces, let’s program it so that it can detect faces in a video stream.

We will use the picamera package for video streaming, which provides an interface to the Raspberry Pi camera module for Python.

For each frame of the video stream, we will perform face detection, draw a bounding box around the detected faces and display the frame.

Face Tracking Made Simple

Let’s add face tracking to the above program. We will detect faces in the first frame and then track the faces detected in all the other frames.

For tracking, we will use correlation_tracker() that contains machine learning algorithms and tools that are used in a wide range of domains including robotics and embedded devices.

Dlib is a library created by Davis King that contains machine learning algorithms and tools that are used in a wide range of domains including robotics and embedded devices.

To install Dlib, use the following commands:

NOTE: If you are facing issues installing Dlib, their tutorials provide detailed instructions on installing it.

The correlation_tracker() allows you to track the position of an object as it moves from frame to frame in a video.

In the first frame, we will perform face detection. For each of the faces detected, we will create a correlation tracker object (dlib.correlation_tracker()) and start tracking the face by giving its bounding box coordinates to the tracker object. Then for each of the subsequent frames, we will use these trackers to identify the location of each face.

You now have your face tracker system ready!

You can also modify the code so that you can do face tracking from a video loaded from a disk instead of streaming it live from the camera.

Here is the modified face detection and face tracking code:

Below is a screen recording video of the running face detection and tracking on a video. The video I used for testing my code was a cropped version (I used the last 8 seconds only) of the original video by Tim Savage from Pexels. You can download the original video from Pexels.

Face Tracking using Xailient Face Detector and dlib

Looking to implement real-time face detection on a Raspberry Pi? Check out this post.

Spread the love

About the Author

    Sabina is an AI Specialist and Machine Learning Engineer. She is a Writer and a former Editorial Associate at Towards Data Science.

Trusted by

Xailient’s commercial partners

Press releases

January 18, 2024

NEWS PROVIDED BY Xailient Inc.  18 Jan, 2024, 01:13 ET SYDNEY, Jan. 18, 2024 /PRNewswire/ — Xalient customer Abode, a leading provider of DIY smart home security solutions, has been recognized for their innovative new product, the Abode Edge Camera. Xailient AI runs inside the Abode Edge Camera, watching for anomalies like package deliveries or strangers on the […]

November 1, 2023

NEWS PROVIDED BY Xailient  25 Oct, 2023, 09:05 ET Wi-Fi HaLow™ Technology Enables Long-Range, Low-Power Connectivity for Smart Cameras SYDNEY and IRVINE, Calif., Oct. 25, 2023 /PRNewswire/ — Xailient, the leader in edge artificial intelligence (AI) for computer vision, today announced a strategic partnership with Morse Micro, a fast-growing fabless semiconductor company focused on Internet of Things (IoT) connectivity. Together, they […]

OnEdge Newsletter

A weekly newsletter with the best hand-picked resources about Edge AI and Computer Vision

OnEdge is a free weekly newsletter that keeps you ahead of the curve on low-powered Edge devices and computer vision AI.


You’ll get insights and resources into:

  • Edge computing use cases.
  • Market trends in Edge computing.
  • Computer Vision AI at the Edge.
  • Machine learning at the Edge.
Cookie Policy