Building a Face Recognition System with Python and the OpenCV Library

Facial recognition is a popular technology used in security systems, mobile devices, and social media applications. It involves identifying and verifying a person's identity by analyzing their facial features. Python is a versatile programming language, and the OpenCV library provides a wide range of image and video processing capabilities, including facial recognition.

In this tutorial, we will explore how to build a facial recognition system with Python and the OpenCV library. We will start with the installation of the OpenCV library and necessary dependencies. Then we will dive into the main content, which includes facial detection, facial recognition, and tracking. We will also discuss how to train our own facial recognition model using OpenCV.

By the end of this tutorial, you will have a solid understanding of how to build a facial recognition system with Python and OpenCV and will be able to apply this knowledge to a variety of real-world scenarios.

Getting Started

Before we dive into using the openCV library, we first need to install the library using pip. However, since it does not come built-in, we must first install the openCV library. This can be done using the pip package manager.

To install the openCV library, open your terminal and type the following command −

pip install opencv-python-headless opencv-contrib-python-headless numpy

This will download and install the openCV library and its dependencies. Once installed, we can start working with openCV and leverage its modules!

Building a Face Recognition System With the OpenCV Library

To train our own facial recognition model, we need a dataset of images of the people we want to recognize. We can use the OpenCV createLBPHFaceRecognizer() function to create a model that can recognize faces based on their LBPH features. Here's how −

# Load the dataset
dataset_path = "path/to/dataset"
dataset = load_dataset(dataset_path)

# Extract features and labels from the dataset
features, labels = extract_features_labels(dataset)

# Create LBPH face recognizer and train the model
recognizer = cv2.face.createLBPHFaceRecognizer()
recognizer.train(features, labels)

# Save the trained model
model_path = "path/to/model"

In the code above, we first load the dataset of images of the people we want to recognize. Then we use the extract_features_labels() function to extract the LBPH features and labels from the dataset. Finally, we create an instance of the LBPH face recognizer using the cv2.face.createLBPHFaceRecognizer() function and train the model using the extracted features and labels.

Now that we have the trained data ready, let us jump straight into the code and try to code and explain it rather than break it down into components. This will make it easier for you to understand and not get too confused along the way!

import cv2
import os

# Get the training images from a directory
def get_training_images(directory):
   faces = []
   labels = []
   label = 0

   for subdir in os.listdir(directory):
      subdir_path = os.path.join(directory, subdir)
      if not os.path.isdir(subdir_path):

      for filename in os.listdir(subdir_path):
         img_path = os.path.join(subdir_path, filename)
         img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
         if img is None:


      label += 1

   return faces, labels

# Train the facial recognition model
def train_model(faces, labels):
   recognizer = cv2.face.createLBPHFaceRecognizer()
   recognizer.train(faces, labels)
   return recognizer

# Recognize a face in an image
def recognize_face(img, recognizer, face_cascade):
   gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
   faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

   for (x, y, w, h) in faces:
      roi_gray = gray[y:y + h, x:x + w]
      id, confidence = recognizer.predict(roi_gray)

      if confidence < 100:
         label = "Person {}".format(id)
         label = "Unknown"

      cv2.putText(img, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 255, 0), 2)
      cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

   return img

# Main function
def main():
   # Load the face cascade classifier
   face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

   # Load the training images
   faces, labels = get_training_images("training_images")

   # Train the facial recognition model
   recognizer = train_model(faces, labels)

   # Initialize the camera
   camera = cv2.VideoCapture(0)

   while True:
      # Capture a frame from the camera
      ret, img =

      # Recognize faces in the image
      img = recognize_face(img, recognizer, face_cascade)

      # Display the image
      cv2.imshow("Face Recognition", img)

      # Wait for a key press
      if cv2.waitKey(1) & 0xFF == ord("q"):

   # Release the camera and close the window

if __name__ == "__main__":

This code includes functions to load and train a facial recognition model using OpenCV's createLBPHFaceRecognizer() function, as well as a function to recognize faces in an image using a trained model and OpenCV's detectMultiScale() function to detect faces in an image.

The main function uses the computer's camera to capture frames and then recognizes faces in those frames using the recognize_face() function. The recognized faces are labeled with the person's name or "Unknown" if the face is not recognized, and then displayed in a window using OpenCV's imshow() function.


In conclusion, the use of facial recognition technology is increasing rapidly in various fields, from security to entertainment. The OpenCV library offers an efficient and effective way to develop facial recognition systems using Python. In this tutorial, we have covered the basic steps of building a facial recognition system with OpenCV, from detecting faces in images and videos to training our own facial recognition model. We have also discussed some of the key considerations when implementing facial recognition systems, such as the ethical and legal implications of the technology.

While facial recognition technology has the potential to revolutionize many industries, it is important to ensure that it is used ethically and responsibly. The implementation of proper privacy and security measures is essential to prevent misuse of this technology. Additionally, it is important to be aware of the potential biases and limitations of facial recognition systems and to continually improve the accuracy and fairness of the technology.

Overall, the OpenCV library provides a powerful tool for building facial recognition systems with Python. By following the steps outlined in this tutorial and keeping in mind the ethical considerations of the technology, developers can create robust and effective facial recognition systems for a variety of applications.

Updated on: 31-Aug-2023


Kickstart Your Career

Get certified by completing the course

Get Started