Introduction
Detecting a face in digital image is simply a way to find pixels location containing face pattern in it. Examples of this application resides in our smart-phone camera that has an ability to detect face in captured frame, security system that many applied in CCTV (Closed Circuit Television) system, and Marketing field (but this is still rarely found). One thing needed to remember is that the term of ‘face detection’ is different from ‘face recognition’ that the latter means a system can identify ‘whose’ person of any face detected, while the former means a system just ‘point out’ a face in an image – without knowing ‘who’ is he or she’. For this post, Python programming language will be used to detect a face in an image.

Post equivalent in C++

Prerequisites
In this post, bellow prerequisites are assumed have been installed on your system
  • OpenCV 4.1.1 library or latter
  • Python3, and
  • Your favorite text editor

Codes
Before going into the code, we need to prepare these files first:
  • lena.jpg (the image that we use to test), and
  • haarcascade_frontalface_alt.xml (the trained classifier for detection ‘utility’)
The two files mention above has been there in OpenCV directory you installed, just find them on your OpenCV installation directory. In my computer the xml file is in installation/OpenCV-4.1/share/opencv4/haarcascades, and for the image you can save the lena.jpg image from this post (in figure 1).

Figure 1. Our image lena.jpg (source: OpenCV org)


Importing Modul
So the first thing to do in Python code is to ‘import’ a module needed
import cv2

that’s it! All functions we need reside in the module above.



Preparing an Image to be Detected
The first thing to do is to give our window a name and read an image (lena.jpg). These two processes are done by the first and the second line of the line codes bellow.
cv2.namedWindow('FaceDetect')
img = cv2.imread('lena.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gray)

The image that is going to be processed latter needs to be in gray scale format. This is achieved by cvtColor() function above -The first argument is for input image and the second argument is color space conversion code flag. This function then returns a gray scale image.
To be well detected, equalizeHist() function is usually used to equalize the histogram for the frame / image given, decreasing ‘dark’ pixels value to balance all other pixels, or simply to brighten a dark image.



Find Face Location
This is where a face location in an image is found. In other words the face detection process happens in these lines of code bellow:
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
faces = face_cascade.detectMultiScale(gray)

The first thing we need to do, is to make a variable from CascadeClassifier class, then continued to load our haarcascade_frontalface_alt.xml file by passing the name of the .xml file. The path of this file in this case is the same as our python code.

The next line is where face detection happens by using detectMultiscale() method of the instance. According to OpenCV reference manual, it states that the method detects object of different size in the input image and the detected object are returned as a list of rectangles.
For the first and the second argument of this methods are input and output images respectively. Note that the output is in form of a list of rectangle.



Signifying Face Detected
So far, we have detected the face object or the location of the pixel has been found. Therefore it will be meaningless if the frame which will be shown doesn’t give any information about where the pixel (or face location) is in the image. The bellow code will give a green line rectangle that surrounds our lena.jpg face image, and then showing the image with a face detected on computer screen.
for (x,y,w,h) in faces:
    img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)

cv2.imshow('FaceDetect',img)

while chr(cv2.waitKey(0)) != 'q':
   continue

the for() loop expression above just counts and processes how many face of the image detected. In addition to that the for() loop also gets the faces’ point position (x,y) and width-height (w,h) of the faces (that is in list of rectangle) to be used to draw the rectangle later in its loop body.

Within the for() loop statement, we use rectangle() function to draw a green rectangle of our face-detected image. The first argument (img) is the image source that we are going to put a green rectangle on it. The second and the third arguments are vertex of the rectangle, and the last two arguments (fourth and fifth) are for color and thickness of the rectangle line respectively. For the Scalar(0,255,0) will cause our rectangle green, because the it is in BGR format. You can try to experiment with other values with this function.

And for the last following two lines are to show the image on our computer screen and wait a ‘q’ key to be pressed (to terminate the program) respectively.

When the code is run, the output will be like on the figure 2.

Figure 2. Face detected surrounded by a red line (rectangle)


You can also find this code on my github:



Conclusion
Detecting a face in an image is relatively easy to do especially for a beginner who just start to learn. Since almost all necessary functions/methods and “stuffs” has been prepared by OpenCV library, like one we use haarcascade_frontalface_alt.xml file and some methods and classes that support it. There are also so many trained cascade file that you can find out there, like on github, such as banana, dog, cat many other else trained files detector. Just have fun with it.