Face detection with OpenCV and Haar Cascades from scratch

Face detection with OpenCV and Haar Cascades using the webcam

What is face detection?

Face detection using a computer is one of the applications of Computer Vision.

It consists of locating faces in images or videos, captured by a camera or saved on the hard drive, through the use of machine learning algorithms.

Face detection with Python, OpenCV and Haar Cascades

OpenCV is an open source computer vision and machine learning library with support for Python, C++, and Java.

It offers more than two thousand algorithms designed to perform various tasks, including face detection and recognition.

Its pretrained models, based on the Haar Cascades technique, allow the detection of faces and eyes with acceptable speed.

Next, we show you the installation of the software and libraries necessary for the implementation of a face detection system using the PC’s webcam with OpenCV and Haar Cascades.

We will then provide you with the code as well as a brief explanation of the commands used.

Install Python, NumPy and OpenCV

Python installation

The first thing you have to do is install Python. To do this, you can download its installation file from the following link:

Once the installation is complete, open the Command Prompt. To do this, press the Windows + R keys and click OK.

Type the following command and press Enter:

python --version

If you see the version of Python, it means that the installation was successful.

Also, make sure the pip package manager has been installed correctly. To do this, run the command:

pip -v

NumPy installation

Once Python is installed, it will be necessary to install the NumPy library, since OpenCV uses its array-type data structure.

For NumPy installation, you can use the pip package manager. To do this, open Command Prompt and run the following command:

pip install numpy

OpenCV installation

To install OpenCV, open Windows Command Prompt and run the following command:

pip install opencv-contrib-python

Download pre-trained face detection models

The last thing to do is to download from GitHub the pre-trained models that OpenCV will use for face and eye detection. Click on this link.

Locate and click the haarcascade_frontalface_default.xml file. Next, right-click on the Raw button and select the Save Link As option. The file will be downloaded to your hard drive.

Download the haarcascade_eye.xml file in the same way.

To finish copy both files with XML extension inside the Python installation folder. If you don’t know where it is, open Command Prompt and run the Python where command.

Code to detect faces and eyes using the PC’s webcam

import numpy as np
import cv2

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

webcam = cv2.VideoCapture(0)

while True:
    ret, frame = webcam.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:
        cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = frame[y:y+h, x:x+w]

        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

    cv2.imshow('Face Detection',frame)
    p = cv2.waitKey(28)
    if p == 27:
        break

webcam.release()
cv2.destroyAllWindows()

To run the program:

Type Python in the Windows search bar and click IDLE (Python xxx) in the list of matches.

In Python IDLE, click File > New File. In the window that opens, click File > Save As. Type a name and save the file with a .py extension to your hard drive.

Copy and paste the code, click File > Save. Then click Run > Run Module. Your computer’s camera will activate and a box will appear around your face and eyes.

Leave a Comment

Your email address will not be published.

Scroll to Top