\

Now face recognition technology has been very widely used, payment fields, authentication, beauty cameras have its applications. Those of you who have an iPhone should be familiar with the following features

Facial recognition technology is also behind a “people” feature in iPhone photos that identifies and classifies faces in photos. \

This article describes how to implement face detection in Python. Face detection is the basis of face recognition. The purpose of face detection is to identify the face in the photo and locate the facial feature points, face recognition is to tell you who the person is on the basis of face detection.

Well, that’s all for the introduction. Next, start preparing our environment.

The preparatory work

Face detection in this article is based on dlib, which relies on Boost and cmake, so you need to install these packages first, using Ubuntu as an example:

  

$ sudo apt-get install build-essential cmake

$ sudo apt-get install libgtk-3-dev

$ sudo apt-get install libboost-all-dev
Copy the code

We also use Numpy and OpencV in our program, so we need to install these libraries as well:

  

$ pip install numpy

$ pip install scipy

$ pip install opencv-python

$ pip install dlib
Copy the code

Face detection is based on pre-trained model data, which can be lowered from here to model data

  

http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
Copy the code

Download to the local path after decompression, write down the decompression file path, the program will be used.

Dlib face feature points

The model data downloaded above is used to estimate the coordinate positions of 68 feature points (x, y) on human face. The positions of these 68 coordinate points are shown in the figure below:

Our program will consist of two steps: \

The first step is to examine the area of the face in the photo

The second step is to further examine the organs (eyes, nose, mouth, chin, eyebrows) in the detected face area.

Face detection code

Let’s start by defining some utility functions:

  

def rect_to_bb(rect):

    x = rect.left()

    y = rect.top()

    w = rect.right() - x 

    h = rect.bottom() - y 

    return (x, y, w, h)
Copy the code

The rect in this function is the output of the dlib face region detection. Here the RECT is converted to a sequence containing the boundary information of the rectangular region.

  

def shape_to_np(shape, dtype="int"):

    coords = np.zeros((68, 2), dtype=dtype)

    for i in range(0, 68):

            coords[i] = (shape.part(i).x, shape.part(i).y)

    return coords
Copy the code

The shape in this function is the output of the dlib face detection. A shape contains 68 points of the face mentioned above. This function converts shape to Numpy Array for subsequent processing.

  

def resize(image, width=1200):

    r = width * 1.0 / image.shape[1]

    dim = (width, int(image.shape[0] * r)) 

    resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)

    return resized
Copy the code

The image in this function is the image we want to detect. At the end of the face detection program, we will display the detection result picture for verification. Resize is made here to avoid the picture being too large to exceed the screen scope.

Next, start our main program

  

import sys 

import numpy as np

import dlib

import cv2 

if len(sys.argv) < 2:

    print "Usage: %s <image file>" % sys.argv[0]

    sys.exit(1)

image_file = sys.argv[1]

detector = dlib.get_frontal_face_detector()

predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
Copy the code

We read the image of the face to be detected from sys.argv[1], and then initialize the Detector for face region detection and Predictor for face feature detection. The parameters in shape_Predictor are the paths to the files we extracted earlier.

  

image = cv2.imread(image_file)

image = resize(image, width=1200)

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

rects = detector(gray, 1)
Copy the code

Before detecting the feature area, we need to detect the face area first. This code calls OpencV to load the image, resize it to a proper size, turn it into a grayscale image, and finally detect the face region with detector. Because a photo can contain multiple faces, what you get here is an array rects that contains information about multiple faces.

for (i, rect) in enumerate(rects): shape = predictor(gray, rect) shape = shape_to_np(shape) (x, y, w, h) = rect_to_bb(rect) cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.putText(image, "Face #{}".format(i + 1), (x - 10, y - 10), FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) for (x, y) in shape: cv2.circle(image, (x, y), 2, (0, 0, 255), -1) cv2.imshow("Output", image) cv2.waitKey(0)Copy the code

For each face detected, we further examined facial features (nose, eyes, eyebrows, etc.). For the face area, we marked it with a green box; For facial features, we mark them with red dots.

Finally, we display the photo with the detection identifier. WaitKey (0) means that you can exit the program by pressing any key.

That’s all for our program

test

Now comes the exciting moment, the moment to test our results.

Below is the original image

Below are the results of program identification

You can see that the face area is framed by green rectangles, and the features of the face (nose, eyes, etc.) are marked by red dots.

Isn’t that easy?

Appreciate the author


* * * *

In this paper, the author

Strong elder brother

Columnist for the Python Chinese Community. He worked at Morgan Stanley and eBay. Zhihu: Python and data analysis

Join the CodingGo programming community

**** Learn programming knowledge, harvest Internet insiders.