preface

Recently I met the scene of photo recognition, and I just used the face_recognition project to share with you. Face_recognition project can do a lot, face detection function is also available, is a relatively mature project. Github address for the project: Github Repository

This article is mainly used for the installation of the project, and will update a code I wrote to achieve face detection, which can be used directly.

Environmental installation

First let’s take a look at the official face recognition renderings

Let’s take a look at README’s information about the installation environment

The official list of installable operating systems is Mac and Linux, but I want to install it on Windows, so read on.

Windows is not officially supported, but it can be installed as a dlib. All right, let’s get started.

We will install requirements_dev.txt directly, and note that PIP is removed.

Cmake is required to install dlib as follows:

pip install cmake -i pypi.douban.com/simple

In addition, the project also needs to install Opencv-python, the installation command is as follows:

pip install opencv-python -i pypi.douban.com/simple 

Code using

Note: When using face_recognition, you can choose to install the face_recognition command to run the mode, or you can use the face_recognition module to build code to run. For secondary development, I still try the code first, mainly try the face recognition module.

The official code is as follows:

import face_recognition

# Load the jpg files into numpy arrays
biden_image = face_recognition.load_image_file("biden.jpg")
obama_image = face_recognition.load_image_file("obama.jpg")
unknown_image = face_recognition.load_image_file("obama2.jpg")

# Get the face encodings for each face in each image file
# Since there could be more than one face in each image, it returns a list of encodings.
# But since I know each image only has one face, I only care about the first encoding in each image, so I grab index 0.
try:
    biden_face_encoding = face_recognition.face_encodings(biden_image)[0]
    obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
    unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]
except IndexError:
    print("I wasn't able to locate any faces in at least one of the images. Check the image files. Aborting...")
    quit()

known_faces = [
    biden_face_encoding,
    obama_face_encoding
]

# results is an array of True/False telling if the unknown face matched anyone in the known_faces array
results = face_recognition.compare_faces(known_faces, unknown_face_encoding)

print("Is the unknown face a picture of Biden? {}".format(results[0]))
print("Is the unknown face a picture of Obama? {}".format(results[1]))
print("Is the unknown face a new person that we've never seen before? {}".format(not True in results))

Copy the code

Code description:

1. First you see the two faces added to the known_faces list.

2. Then use the unknown graph data for identification and judgment.

Take a look at the photo added to Known_faces

Take a look at the photos you need to identify

Take a look at the execution result

We can see false for Biden identification and true for Obama identification. One thing to note here is that we look at the compare_faces method parameter.

The best parameter of tolerance is 0.6, the lower the tolerance is, the more strict it is, so it can be adjusted according to one’s own needs.

conclusion

After many tests, when the face shape is relatively close, there will still be errors, which need to be adjusted according to their own situation.

The heaviest burden of life is not work, but boredom. — Romain Rolland

If this article helped you, please give it a thumbs up. Thanks!

My CSDN home page address: Swordsman Aliang’s home page

Learn together and make progress together.