Abstract: A line of code to achieve face recognition

  • First you need to provide a folder with pictures of all the people you want the system to know. Each of them had a picture named after the person.
  • Next, you need to prepare another folder with the images you want to identify.
  • You can then run the face_recognition command, pass in the two folders as arguments, and the command will return who is in the image you need to recognize. One line of code is enough!

Body:

  • Environmental requirements:

Ubuntu17.10

Python 2.7.14

  • Environment building:

1. Install Ubuntu17.10 > the installation steps are here

2. Install Python2.7.14 (Ubuntu17.10 default Python version 2.7.14)

Install git, cmake, and python-pip

Git $sudo apt-get install -y git $sudo apt-get install -y git $sudo apt-get install -y python-pipCopy the code

4. Install and compile dlib

Before installing face_recognition, you need to install and compile the Dlib

Boost $sudo apt-get install libboost-all-dev $git clone https://github.com/davisking/dlib.git $ cd dlib $ mkdir build $ cd build $ cmake .. -ddlib_use_cuda =0 -duse_avx_instructions =1 $cmake --build. $CD.. $ python setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDACopy the code

5. Install face_recognition

The face_recognition process will automatically install NUMpy, scipy, etcCopy the code

After setting up the environment, run the face_recognition command on the terminal to check whether the environment is set up successfully

Face recognition: Example one (1 line of code to achieve face recognition)

1. First you need to provide a folder with pictures of all the people you want the system to know. Each of them had a picture named after the person:

Known_people folder contains babe, Jackie Chan, Joey Yung

2. Next, you need to prepare another folder with the images you want to identify:

Unknown_pic folder contains the pictures to be identified, among which Han Hong is unknown to the machine

3. Then you can run the face_recognition command, pass in the two folders as parameters, and the command will return who is in the image you need to recognize:

Identification successful!!

Example 2 (Identify all faces in the picture and display them)

# filename : find_faces_in_picture.py # -*- coding: Apt-get install python-imaging from pil import Image PIP install face_recognition import face_recognition # load JPG file into numpy array image = Load_image_file ("/opt/face/unknown_pic/all_star.jpg") # Use the default given HOG model to find all faces in the image # this method is pretty accurate, But it's still not as accurate as the CNN model because GPU acceleration is not used. # See also: Find_faces_in_picture_cnn. py face_locations = face_recognition. Face_locations (image Face_recognition. Face_locations (image, number_of_times_to_upsample=0, model=" CNN ") Print ("I found {} face(s) in this photograph.". Format (len(face_locations))) # loop for all faces found face_location in face_locations: Print ("A face is located at Pixel location top: {}, left: {}, Bottom: {}, Right: {}". Format (top, left, bottom, right) # Face_image = image[top:bottom, left:right] pil_image = image.fromarray (face_image) pil_image.show()Copy the code

The following picture is used for identification

$python find_faces_in_picture.pyCopy the code

7 faces were identified from the picture and displayed as shown below

Example 3 (Automatic Face Recognition)

# filename : find_facial_features_in_picture.py # -*- coding: Apt-get install python-imaging from pil import Image, ImageDraw # recogn_recogntion module PIP install face_recognition import face_recognition # load JPG file into numpy array image = Load_image_file ("biden.jpg") # Find all facial features of all faces in the image face_landmarks_list = face_recognition.face_landmarks(image) print("I found {} face(s) in this photograph.".format(len(face_landmarks_list))) for face_landmarks in face_landmarks_list: Facial_features = [' Chin ', 'Left_EYEBROW ',' Right_EYEBROW ', 'Nose_bridge ',' Nose_tip ', 'left_eye', 'right_eye', 'top_lip', 'bottom_lip' ] for facial_feature in facial_features: print("The {} in this face has the following points: {}". Format (facial_feature, face_landmarks[facial_feature]) # pil_image = Image.fromarray(image) d = ImageDraw.Draw(pil_image) for facial_feature in facial_features: d.line(face_landmarks[facial_feature], width=5) pil_image.show()Copy the code

Automatic recognition of face features (contours)

Example 4 (Face identification is who)

# filename : recognize_faces_in_pictures.py # -*- conding: Utf-8 -*- # Import the face_recogntion module, PIP install face_recognition import face_recognition # load JPG file into numpy array babe_image = face_recognition.load_image_file("/opt/face/known_people/babe.jpeg") Rong_zhu_er_image = face_recognition.load_image_file("/opt/face/known_people/Rong zhu er.jpg") unknown_image = Load_image_file ("/opt/face/unknown_pic/babe2.jpg") # Get the facial code for each face in each image file # Return a list of codes since there may be multiple faces in each image. # But since I know that each image has only one face, I only care about the first encoding in each image, so I take index 0. babe_face_encoding = face_recognition.face_encodings(babe_image)[0] Rong_zhu_er_face_encoding = face_recognition.face_encodings(Rong_zhu_er_image)[0] unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0] known_faces = [ babe_face_encoding, Rong_zhu_er_face_encoding] # result is array of True/false, Known_faces Results = face_recognition.com Pare_FACES (known_faces, Unknown_face_encoding) print(" unknown_face_encoding ".format(results[0]) print(" Unknown_face_encoding ".format(results[1]) Print (" Is this a new face we haven't seen before? {}". Format (not True in results))Copy the code

The following figure shows the result

Example 5 (Face Recognition and Beauty)

# filename : digital_makeup.py # -*- coding: Apt-get install python-imaging from pil import Image, ImageDraw # recogn_recogntion module PIP install face_recognition import face_recognition # load JPG file into numpy array image = Load_image_file ("biden.jpg") # Find all facial features of all faces in the image face_landmarks_list = face_recognition.face_landmarks(image) for face_landmarks in face_landmarks_list: pil_image = Image.fromarray(image) d = ImageDraw.Draw(pil_image, 'RGBA') # making eyebrows a nightmare D.Polygon (face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128)) d.polygon(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 128)) d.line(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 150), width=5) d.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), D. polygon(face_landmarks['top_lip'], fill=(150, 0, 0, 128)) d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128)) d.line(face_landmarks['top_lip'], fill=(150, 0, 0, 64), width=8) d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), D. polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30)) D. polygon(face_landmarks['right_eye'], Fill =(255, 255, 255, 30) # Apply some eyeliner D.line (face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=6) d.line(face_landmarks['right_eye'] + [face_landmarks['right_eye'][0]], fill=(0, 0, 0, 110), width=6) pil_image.show()Copy the code

Before and after beauty, such as the picture below:

The end:

That’s all for this article, please give a thumbs up if you like!

Finally the complete code has been packaged, there is a need for small partners, you can click on this line of font, or small private letter!