May 5, 2021

Face Detection Using OpenCV and Python 2021

Topic – Face Detection Using OpenCV and Python 2021

What is a Facial Recognition System?

A facial recognition system is a technology capable of matching a human face from a digital image or a video frame against a database of faces, typically employed to authenticate users through ID verification services, works by pinpointing and measuring facial features from a given image.

Wikipedia

Face Detection Using OpenCV and Python 2021


What is OpenCV?

OpenCV or Open Computer Vision is a huge high functionality library that provides us with functions to perform Computer Vision tasks like object detection, face recognition systems.

Released in June 2000, the library is cross-platform and free for use under the open-source Apache 2 License.

In Face Detection Using OpenCV and Python, we will cover –

  1. Install OpenCV
  2. Face Detection using Haar-cascades- overview
  3. Detect faces in images
  4. Detect faces in videos

For this tutorial, we assume that the readers have prior knowledge of the OpenCV library and Python.

So if you are not familiar with these two make sure to visit the OpenCV-Python tutorials page and learn it.

So, let’s begin with Face Detection Using OpenCV and Python

Step 1: Install OpenCV

First, make sure you have OpenCV installed. You can install it using pip in your command line prompt, or simply here.

pip install opencv-python

You are all set! In the next section we will talk about face recognition workflow!

Step 2: Face Detection using Haar-cascades

Haar-cascades in 2021
Haar-cascades

Haar is a group of digital image features used in object recognition. They got their name from Haar wavelets. Haar was used in the first real-time face detector.

Haar Cascade is a machine learning-based approach where we use a lot of positive and negative images to train the classifier.

Positive images – the images which we want our classifier to identify. (faces here)

Negative images – Images of everything else, which does not contain the object we want to detect.

Datasettrained classifier XML file (haarcascade_frontalface_default.xml), which is available in OpenCV’s GitHub.

Save it to your working directory.

Step 3: Detect faces in images

In the case of image recognition, the system makes a decision if the test face belongs to one of the people in the database.

It selects it, based on comparing the features computed in the previous step for test faces and a database of people.

The following snippet detects human face on image data!

import cv2

# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# Read the input image
img = cv2.imread('test.jpg')

# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)

# Draw rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)

# Display the output
cv2.imshow('img', img)
cv2.waitKey()

The comments above each line of code, will be helpful for you to make sense on how the code flow works in face detection.

Let’s look at a sample output!

Face Detection Using OpenCV and Python

Step 4: Detect faces in videos

Final step in Face Detection Using OpenCV and Python

Videos consist of frames. Hence, facial recognition in videos deals with capturing details of faces from frames to frames and simultaneously detecting them!

So we perform the face detection for each frame in a video.

The special concept here is to use an infinite loop to loop through each frame in the video.

The first value returned is a flag that indicates if the frame was read correctly or not. We don’t need it. The second value returned is the still frame on which we will be performing the detection.

Once a face in a particular frame is recognized with a high degree of confidence, that particular face does not need to be processed for the next frames.

Only track the face and keep the association between the recognized person and the tracked face.

Look at the code-

import cv2

# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# To capture video from webcam. 
cap = cv2.VideoCapture(0)

# To use a video file as input 
# cap = cv2.VideoCapture('filename.mp4')

while True:
    # Read the frame
    _, img = cap.read()

    # Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Detect the faces
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)

    # Draw the rectangle around each face
    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)

    # Display
    cv2.imshow('img', img)

    # Stop if escape key is pressed
    k = cv2.waitKey(30) & 0xff
    if k==27:
        break

# Release the VideoCapture object
cap.release()

WOW!

This was as simple as you just saw!

Concluding Face Detection Using OpenCV and Python

Computer Vision is just not about face detection, and face detection is just not about what you just saw.

To work on computer vision, object detection, and image processing systems, you need to have your basics cleared! Only then you can create such quick implementations of a huge system.

To know more about OpenCV, read the documentation, tutorials provided on its official page and start your journey today!

Hope you had fun with this article!

Happy Machine Learning!

You can check out our other projects with source code below

  1. Fake News Classifier with NLP 2021
  2. Spam Email Detection using Machine Learning Projects for Beginners in Python (2021)
  3. Hands-on Exploratory data analysis python Code & Steps -2021
  4. Interesting python project (2021) Mouse control with hand gestures.
  5. Best (2021) Python Project with Source Code
  6. Live Color Detector (#1 ) in Python 2021

Feel free to contact us!

Mini Projects