How does Face Detection work using OpenCV?

Nidhi Bansal
5 min readApr 29, 2020

--

Face Detection in an image

Face detection is a computer vision technology that helps to locate/detect faces in images. It is a specific use-case of Object Detection technology that deals with detecting various objects like faces, cars, flowers, buildings, etc. in images.

For more details on Object Detection see my article: https://medium.com/analytics-vidhya/computer-vision-techniques-implementing-mask-r-cnn-on-malaria-cells-data-e03b9fbeb6be

We will be implementing Face Detection with Python using two methods:

  1. OpenCV Library.
  2. Faster R-CNN Algorithm.

Before implementing algorithms for Face Detection, let’s first understand the images.

Understanding how we read and save images on our machines is basic. Images are made up of small square boxes. These boxes are called pixels. Each pixel contains a numeric value.

In the case of images, these pixel values are used as features for Machine Learning models.

Based on representation images are classified into 3 types:

  1. Binary Image
  2. Grayscale Image
  3. Colored/RGB Image

1. Binary Image

As the name suggests, binary images are of only black and white colors. Each pixel can have only two values 0 or 1 i.e. 1 bit per pixel. For black color, the pixel value is 0 and for white color, the pixel value is 1. Let us see below the binary image and its corresponding binary values:

Binary image and its corresponding pixel values

2. Grayscale Image

A grayscale image consists of 8 bits per pixel means each pixel can have 256 different values. Values range from 0 to 255. Here 0 means black and 255 means white. Let us see below the grayscale image and its corresponding pixel values:

Grayscale image and its corresponding pixel values

In the above image, we can see pixel value 0 for black color, and for digit ‘3’ in the image, there are various values between 1 to 255.

3. Coloured/RGB image

A colored image is typically composed of multiple colors and all colors can be created from primary colors- Red, Green, and Blue(RGB)

Hence, in the case of a colored image, there are three channels — Red, Green, and Blue. Each channel matrix has values between 0–255 representing the intensity of the color for that pixel. You can consider the colored image as a three-dimensional array. These three channels are superimposed to make a colored image.

Python code to read image:

from skimage.io import imread, imshowimage = imread('car.jfif')
print("Shape of image is: " , image.shape)
imshow(image)
Output>> Shape of image is: (177, 285, 3)
car image(RGB image)

Face Detection using OpenCV library

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library.

OpenCV-Python is OpenCV API for Python.

  1. Installation of OpenCV-Python
    Packages for standard desktop environments (Windows, macOS, almost any GNU/Linux distribution)
run pip install opencv-python

For more detailed information on installation go to the link

2. Import the package into Jupyter Notebooks or any Python IDE
Import the OpenCV package with the below command.

import cv2

3. Data source
I have taken data(images) from https://www.analyticsvidhya.com/
Link here

4. Loading image with OpenCV
Python code for loading images and showing images using OpenCV

img = cv2.imread('10065.jpg')
(h, w, d) = img.shape
print(" height={},width={}, depth={}".format( h,w, d))
plt.imshow(img)
height=409,width=612, depth=3

Output: height=409,width=612, depth=3

Output image

But, this is not an original 10065.jpeg. It is because OpenCV loads an image in BGR format, so we need to convert it into an RBG format to be able to display its true colors. So we will write a small code for that.

img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #when we display the image using matplotlib, the red and blue channel gets swapped and hence the blue tinge. Using cvtcolur solve this issueplt.imshow(img_rgb)
Output image in RGB format

5. Haar Cascade classifier for Face detection
OpenCV comes with a lot of pre-trained classifiers. There are classifiers for smile, eyes, face, full-body, etc. These come in the form of XML files and can be accessed from here. Download the XML files and place them in the data folder in the same working directory as the jupyter notebook. Here, we are using ‘haarcascade_frontalface_default.xml’. The Python code is shown below.

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

For the cascade classifier, we shall use the ‘detectMultiScale’ Model of the classifier, as we need to detect objects(faces) of different sizes in the input image.

# Detect faces
faces = face_cascade.detectMultiScale(img_rgb, scaleFactor=1.1, minNeighbors=4)

detectMultiScale’ function will return a rectangle with coordinates(x,y,w,h) around the detected face. The two important parameters of this function have to be tuned according to the data:

  • scaleFactor — Parameter specifying how much the image size is reduced at each image scale.
  • minNeighbors — Parameter specifying how many neighbors each candidate rectangle should have to retain it.

For details on this function click here.

Python code to draw bounding box/rectangle around faces detected is shown below:

# Draw bounding box/rectangle around faces detected
for (x, y, w, h) in faces:
cv2.rectangle(img_rgb, (x, y), (x+w, y+h), (255, 0, 0), 2)
plt.imshow(img_rgb)
#Count number of faces detected
print('Number of faces counted in image: ', len(faces))
plt.imshow(img_rgb)

Output: Number of faces counted in image: 2

Output image with Bounding boxes

Let’s test with another image and check its output:

Output: Number of faces counted in image: 4

Images with 4 faces detected

OpenCV with Haar Cascade Classifier works very well for images, as we can see above output images.

Conclusion

We have discussed the OpenCV library method of face detection in this article. It works pretty well. We will discuss another method i.e. Faster R-CNN Algorithm for face detection in the second part of this article.

So, try to implement code snippets which I share for face detection.

If you found my article useful, give it a 👏 and help others find it. It’s a great way to give feedback!

--

--