Face-detection program

Let's write a program to detect a human face. I have named this program FaceDetection.cpp and you can download it from the Chapter08 folder of this book's GitHub repository.

Since we will be using haarcascade_frontalface_alt2.xml to detect faces, please make sure that the FaceDetection.cpp and haarcascade_frontalface_alt2.xml files are in the same folder.

To program face detection, follow these steps:

  1. In the FaceDetection.cpp program, load the Haar's pre-trained frontal face XML using the CascadeClassifier class, as shown in the following code snippet:
CascadeClassifier faceDetector("haarcascade_frontalface_alt2.xml");
  1. Declare two matrix variables, called videofeed and grayfeed, along with a VideoCapture variable, called vid(0), to capture footage from the RPi camera:
Mat videofeed, grayfeed;
VideoCapture vid(0);
  1. Inside the for loop, read the camera feed. Then, flip the camera feed horizontally. Using the cvtColor function, we can convert our videofeed into grayscale. If your Pi camera is placed upside-down, set the third parameter inside flip function to 0. The grayscale output is stored in the grayfeed variable. The following code shows how to complete this step:
vid.read(videofeed);
flip(videofeed, videofeed, 1);
cvtColor(videofeed, grayfeed, COLOR_BGR2GRAY);
  1. Let's perform a histogram equalization to improve the brightness and contrast of videofeed. Histogram equalization is required because sometimes, in low lighting, the camera may not be able to detect the face. To perform histogram equalization, we will use the equalizeHist function:
equalizeHist(grayfeed, grayfeed);
  1. Let's detect some faces. For this, the detectMultiScale function is used, as follows: 
detectMultiScale(image, object, scalefactor, min neighbors,flags, min size, max size);

The detectMultiScale function that's shown in the preceding code snippet consists of the following seven parameters:

  1. Since the detectMultiScale function provides a vector of rectangles as its output, we have to declare a vector as the Rect type. The variable name as facescalefactor is set to 1.1, min neighbors is set to 5, and the minimum scale size is set 30 x 30 pixels. The max size is ignored here because if your face size becomes bigger than the max size, your face will not be detected. To complete this step, use the following code:
vector<Rect> face;
faceDetector.detectMultiScale(grayfeed, faces, 1.3, 5, 0 | CASCADE_SCALE_IMAGE, Size(30, 30));

After detecting faces, we will create a rectangle around the detected faces and display text on the top-left side of the rectangle that states "Face detected":

for (size_t f = 0; f < face.size(); f++) 
{
rectangle(videofeed, face[f], Scalar(255, 0, 0), 2);
putText(videofeed, "Face Detected", Point(face[f].x, face[f].y), FONT_HERSHEY_PLAIN, 1.0, Scalar(0, 255, 0), 2.0);
}

Inside the for loop, we are determining how many faces are detected using the face.size() function. If one is detected, face.size() equals 1, and the for loop will be satisfied. Inside the for loop, we have the rectangle and putText function. 

The rectangle function will create a rectangle around the detected face. It consists of four parameters:

The putText function is used to display text in an image or video feed. It consists of seven parameters:

Finally, using the imshow function, we will view the video feed, along with the rectangle and text:

imshow("Face Detection", videofeed);

After using the preceding code, and if you have compiled and built the program, you will see that a rectangle has been drawn around the detected face:

 

Next, we will detect human eyes as well as recognize a smile. Once the eyes and smile have been recognized, we will create circles around them.