Realize face recognition and extract 68 feature points of face, maybe this Python library can help you!

I wrote an article on the realization of face recognition before. The technology used in it is realized by calling Baidu API. This time, we will use the dlib package to realize the functions of face area detection and feature point extraction.

dlib encapsulates many excellent machine learning algorithms, which can realize face recognition, detection, recognition, video target pursuit and other functions. It is an open source library developed by C++. It also provides a Python interface for us to call directly.

1, dilb installation

The installation method of the dlib package is also installed with pip, but it is different from other packages in that the input

pip install dlib

Before, you need to install the Cmake package. The tool is mainly to compile dlib. The installation command is similar to other packages.

pip install Cmake

2. Realize face recognition

When using dlib to realize the face recognition function, first define a detector and a picture preview window:

detector = dlib.get_frontal_face_detector()
win = win = dlib.image_window()

Then use the load_rgb_image() function to read the image:

img = dlib.load_rgb_image(f)

Next, we will implement the core function face detection. Here we need to use the detector defined above

dets,score,idx = detector.run(img,1-1)

img is the image we read. The second parameter 1 represents the upsampling multiple of the image. The larger the value, the better the final recognition result. -1 represents the adjustment of the segmentation threshold, and a negative value indicates that more will be returned. Test results

The returned dets returns a face area rectangle, which represents the left, upper, right, and lower borders. It is in the form of a tuple. If one face is detected, it is a tuple. If there are multiple faces, multiple The tuple is placed in a list; with this rectangle coordinate, you can do the following:

  • Cut the face area and extract the area;
  • Line marking of face area

Score represents the detection probability of face recognition results. The larger the result, the better the recognition result; if there are multiple faces in a picture, multiple detection probabilities are returned and stored in the form of a list; idx is used to detect a picture The index used for multiple faces can be indexed

win.clear_overlay()
win.set_image(img)
win.add_overlay(dets)
dlib.hit_enter_to_continue

Finally, we use the previously defined win window to preview the image, and the results are as follows,

Of course, you can also use OpenCV to outline the outline. The results of OpenCV outline are as follows (the reason for the color difference is that OpenCV reads the BGR channel order);

Snipaste_2020-06-01_23-46-03.png

3. dlib extracts 68 feature points of the face

OpenCV can also be used for face recognition, but the effect is not as good as dlib. In addition to the wireframe detection mentioned above, dlib can also directly extract 68 feature points on the face and return them as coordinates;

With the coordinates of these 68 feature points, it can easily help us realize face alignment, fusion and other applications. Let’s take a look at the effect outlined with dlib.

Snipaste_2020-06-02_14-48-24.png

The difference with face recognition is that a shape detector needs to be added here. The shape detector needs the files provided by the official website and puts the trained weight information in it, which can be used directly. The above function implementation code is as follows:

import dlib
import os
import cv2

predictor_path  = "E:/shape_predictor_68_face_landmarks.dat"#权重文件路径
png_path = "E:/ceshi.png"

detector = dlib.get_frontal_face_detector()
#相撞
predicator = dlib.shape_predictor(predictor_path)
win = dlib.image_window()
img =  dlib.load_rgb_image(png_path)
win.clear_overlay()
win.set_image(img1)

for k,d in enumerate(dets):
    print("Detection {}  left:{}  Top: {} Right {}  Bottom {}".format(
        k,d.left(),d.top(),d.right(),d.bottom()
    shape = predicator(img,d)
    #Get the landmarks/parts for face in box d
    print("Part 0:{},Part 1 :{}".format(shape.part(0),shape.part(1)))
    win.add_overlay(shape)

win.add_overlay(dets)
dlib.hit_enter_to_continue()

The coordinate point can be obtained through the part(index) function

The above coordinate points can also be outlined on the original image through OpenCV and annotated with the text. The effect is as follows:

Snipaste_2020-06-02_15-02-38.png

Attach the complete code:

import dlib
import os
import cv2


predictor_path  = "E:/shape_predictor_68_face_landmarks.dat"
png_path = "E:/ceshi.png"


detector = dlib.get_frontal_face_detector()
#相撞
predicator = dlib.shape_predictor(predictor_path)
win = dlib.image_window()
img1 = cv2.imread(png_path)

dets = detector(img1,1)
print("Number of faces detected : {}".format(len(dets)))
for k,d in enumerate(dets):
    print("Detection {}  left:{}  Top: {} Right {}  Bottom {}".format(
        k,d.left(),d.top(),d.right(),d.bottom()
    ))
    lanmarks = [[p.x,p.y] for p in predicator(img1,d).parts()]
    for idx,point in enumerate(lanmarks):
        point = (point[0],point[1])
        cv2.circle(img1,point,5,color=(0,0,255))
        font = cv2.FONT_HERSHEY_COMPLEX_SMALL
        cv2.putText(img1,str(idx),point,font,0.5,(0,255,0),1,cv2.LINE_AA)
        #对标记点进行递归;

cv2.namedWindow("img",cv2.WINDOW_NORMAL)
cv2.imshow("img",img1)
cv2.waitKey(0)

4. Small summary

The above is a brief introduction to the dlib package. In terms of face recognition applications, dlib performs quite well. If you are interested, you can go to the official website to see the detailed introduction of dlib, and follow it in your spare time.

The next article will introduce the use of OpenCV to achieve face fusion technology, which will use the 68 feature point coordinates of the face mentioned in this article, which is an extension of this article, and interested friends can pay attention to a wave in advance.

The method of obtaining the weight file mentioned in the article can be obtained from the official account : Xiao Zhang Python backstage reply keyword: dlib

Reference link:

1,http://dlib.net/face_landmark_detection.py.html

2,https://blog.csdn.net/qq_39438636/article/details/79304130

3 , http: //dlib.net/

Guess you like

Origin blog.csdn.net/weixin_42512684/article/details/106582447