Realtime Face Detection using Python and OpenCV: A Comprehensive Guide

Realtime Face Detection using Python and OpenCV: A Comprehensive Guide

Face detection is a common process in computer vision that involves locating human faces in digital images or videos. Face detection is a critical area of research, given its vast number of applications, including facial recognition, security systems, video analysis, and image editing.

Python is one of the most popular programming languages for Computer Vision applications thanks to its simplicity, readability, and the plethora of libraries available. In this tutorial, we will explore how to perform real-time face detection using Python and the OpenCV library.

What is OpenCV?

OpenCV stands for "Open Source Computer Vision." It is a free library of programming functions and algorithms for computer vision and machine learning. It is used for image and video analysis, pattern recognition, and face detection, to name a few. OpenCV is a powerful library that can help you create complex computer vision applications with ease.

Use of haarcascade_frontalface_default.xml in our project

The haarcascade_frontalface_default.xml file is an XML file that contains the pre-trained Haar Cascade Classifier algorithm for detecting human faces. It is a machine learning-based approach in which a cascade function is trained from positive and negative face images (i.e., samples with and without faces).

The algorithm works by detecting the edges and lines in the image that correspond to facial features such as eyes, nose, and mouth. These features can be combined to locate a face. The trained data is stored in an XML file, which can be loaded into OpenCV using the CascadeClassifier function.

The haarcascade_frontalface_default.xml file is one of the pre-trained datasets available in OpenCV for face detection. It was trained on a large set of positive samples, i.e., images of human faces, that were carefully selected to be representative of those found in real-world scenarios. The dataset contains a large number of negative samples that don't have faces, making it easier to distinguish between faces and non-faces.

Overall, the Haar Cascade Classifier algorithm is a common and powerful technique used for object detection, and the haarcascade_frontalface_default.xml file is one of the many pre-trained datasets available in OpenCV.

How to Perform Real-time Face Detection using OpenCV in Python

In this tutorial, we will use the OpenCV library in Python to perform real-time face detection. We will start by loading the pre-set data from OpenCV and setting up the webcam to capture video.

The code for real-time face detection in Python is as follows:

import cv2

#loading some pre-set data from opencv
trained_face_data = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# webcam setup for capturing video
webcam = cv2.VideoCapture(0)

# always true while loop for real-time face detection
while True:

    #reading frame
    sucessful_frame_read, frame = webcam.read()

    #converting that frame into grey-scaled image
    greyscaled_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # extracting frame's face coordinates
    face_coordinates = trained_face_data.detectMultiScale(greyscaled_img)

    # drawing a rectangle around that frame using coordinates
    for(x, y, w, h) in face_coordinates:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0,255,0),2)

    # displaying that frame 
    cv2.imshow("show face", frame)

    # wait time is 1 milli seconds, 'Q' key is assigned to terminate the app
    key = cv2.waitKey(1)
    if key == 81 or key == 113:
          break

webcam.release()

What does the code do?

Let us go through the code step-by-step:

  1. We begin by importing the OpenCV library:
import cv2

Here, we are importing the OpenCV library to use its functions and methods to perform face detection using Python.

trained_face_data = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

This line of code is loading the pre-trained data from OpenCV required in the face detection process. In this case, we are using 'haarcascade_frontalface_default.xml', which is an XML file containing the pre-set data for detecting human faces. OpenCV provides other pre-trained data for detecting faces in different positions and expressions.

webcam = cv2.VideoCapture(0)

This line of code is setting up webcam initialization so that we can capture the video frames in real-time for face detection using Python.

while True:

This line of code starts a while loop which will run until the break keyword is encountered, which is used to break out of the infinite loop that is continuously executing.

sucessful_frame_read, frame = webcam.read()

This line of code captures video frames to be processed in real-time. The read() function is used to capture a frame from the webcam.

greyscaled_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

This line of code converts the frame from the webcam to grayscale, as the detectMultiScale function of OpenCV works with grayscale images.

face_coordinates = trained_face_data.detectMultiScale(greyscaled_img)

This line of code uses the detectMultiScale() function of the OpenCV library to detect human faces from the video frames. This function applies the pre-trained Haar Cascade Classifier parameters to an image in a sliding window fashion and returns the faces' coordinates in the form of x, y, width, and height values.

for(x, y, w, h) in face_coordinates:
    cv2.rectangle(frame, (x, y), (x+w, y+h), (0,255,0),2)

This line of code is used to draw rectangles on the video frames to outline the detected face regions, allowing us to visualize the face detection process. The cv2.rectangle() function is used to frame the detected faces with a green rectangle with a thickness of 2 pixels. The x, y, w, and h parameters denote the positions of the detected face in the frame.

cv2.imshow("show face", frame)

This line of code is used to display the video frames in a window. The imshow() function takes in two arguments, the window name and the frame captured from the webcam.

key = cv2.waitKey(1)
if key == 81 or key == 113:
    break

This code snippet waits for a key to be pressed and checks if the key being pressed is 'q' or 'Q'. Once any of these keys is pressed, the loop is broken, and the program is terminated.

webcam.release()

This line of code releases the webcam, an essential step to release the occupied resources once the program has finished execution.

This is a brief but comprehensive explanation of the code that I hope clarifies each line's purpose and why we use it.

Conclusion

Real-time face detection is an exciting field in computer vision with numerous applications. This tutorial has provided an overview of how to perform real-time face detection using Python and OpenCV. By following the code and explanations in this article, you will be well on your way to detecting human faces in real-time video streams. Happy coding!

Here is a Github repository containing this code for easy download and use😊: github.com/harsh082ip/RealTime_face_detecti..