Recently, I studied an open source framework face_recognition. The compilation needs to rely on dlib, Boost, cmake and other related environments. When compiling, I step on a lot of pits. Most of the information on the web is not very comprehensive, and the configuration of Windows environment itself is a bit more difficult than Liunx or Mac configuration. If you’re compiling for Windows, get ready to stomp

System environment

  • Windows 7
  • Python 2.7.14
  • VS2015

Installation steps

1. The firstGithub.com/davisking/d…Download the entire ZIP

git clone https://github.com/davisking/dlib](https://github.com/davisking/dlib
Copy the code

2. Install scipy, numpy, and MKL in python.www.lfd.uci.edu/~gohlke/pyt…Go here to find the version of wheel and use easy_install to KO

3. Install the BoostSourceforge.net/projects/bo…Boost_1_XX_X :\local\boost_1_XX_X :\local\boost_1_XX_X

4. This step also seems to be able to create a new one without adding system variables to the position of VS2015

X: Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\Copy the code

5. Go to Boost, double-click bootstrap.bat and run OK to find the following two files in boost_1_66_0 tools build

Then open a command line, navigate to the folder, and run the command:

b2 install
Copy the code

After running, execute the following command:

 b2 -a --with-python address-model=64 toolset=msvc runtime-link=static 
Copy the code

The stage folder will be found after success

6. Configure the following two environment variables in the system environment

BOOST_ROOT=C:\local\boost_X_XX_X

BOOST_LIBRARYDIR=C:\local\boost_X_XX_X\stage\lib 

Copy the code

Finally, run the following command:

python setup.py install --yes USE_AVX_INSTRUCTIONS --yes DLIB_USE_CUDA
Copy the code

CUDA this is mainly used for machine learning with graphics cards, if not can be added

Done! Open the Python shell and try import dlib

pip install face_recognition
Copy the code

With the installation successful, we can import face_recognition as normal in Python

Write face detection program

This demo mainly shows the recognition of the designated picture in the face of the feature data, the following is the face of the eight features, we are to obtain the feature data

    'chin',
    'left_eyebrow',
    'right_eyebrow',
    'nose_bridge',
    'nose_tip',
    'left_eye',
    'right_eye',
    'top_lip',
    'bottom_lip'
Copy the code

Face detection code

# -*- coding: UTF-8 -*- # Automatic face recognition # filename: Find_facial_features_in_picture. py 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 = Face_recognize.load_image_file ("mayun.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

Running results:

Automatically recognize the face in the picture and recognize its features

The original:

Operation effect:

Write face recognition programs

Note: Python-OpencV is used here, so opencV must be configured to run successfully.

Opencv can select a version that matches your Python version at www.lfd.uci.edu/~gohlke/pyt… WHL (my Python version is 2.7, so I chose to install this version). After the installation is complete, open CMD and type import cv2. If no error is displayed, the installation is successful.

# -*- coding: Img = face_recognition. Load_image_file ("mayun.jpeg") Face_locations = face_recognition. Face_locations (img) print face_locations # Cv2.imread ("mayun.jpeg") cv2.namedWindow(" 影 片 ") cv2.imshow(" 影 片 ", img) FaceNum = len(face_locations) for I in range(0, faceNum): top = face_locations[i][0] right = face_locations[i][1] bottom = face_locations[i][2] left = face_locations[i][3] start = (top, top) end = (right, bottom) color = (55,255,155) thickness = 3 cv2. Rectangle (img, start, end, color) Thickness cv2.namedWindow(" face recognition ") cv2.imshow(" face recognition ", img) cv2.waitKey(0) cv2.destroyallWindows ()Copy the code

Running results:

The program will read the current directory under the specified picture, and then identify the face, and annotate each face.

The camera recognizes faces in real time

Since there is no camera on the company’s desktop, the configuration of the demo is the same on the Mac. Once the environment is configured, just run the following code

# -*- coding: Utf-8 -*- # camera real-time face recognition import face_recognition import cv2 video_capture = cv2.videocapture (0) # load local image xhb_img = Face_recognisation.load_image_file ("xhb.jpg") xhb_face_encoding = face_recognisation.face_encodings (xhb_img)[0] # Initialize the variable face_locations = [] face_encodings = [] face_names = [] process_this_frame = True while True: Ret, frame = video_capture. Read () Small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) if process_this_frame: face_locations = face_recognition.face_locations(small_frame) face_encodings = face_recognition.face_encodings(small_frame, face_locations) face_names = [] for face_encoding in face_encodings: Tolerance is set to 0.5 and the default is 0.6. It is too small to be recognized. Match = face_recognition.com Pare_faces ([xHB_face_encoding], face_encoding,tolerance=0.5) if match[0]: name = "xiaohaibin" else: name = "unknown" face_names.append(name) process_this_frame = not process_this_frame for (top, right, bottom, left), name in zip(face_locations, face_names): top *= 4 right *= 4 bottom *= 4 left *= 4 cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), Font = cv2.FONT_HERSHEY_DUPLEX cv2.putText(frame, name, (left+6, bottom-6), font, 1.0, (255, 255, 255), 1) cv2.imshow('Video', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break video_capture.release() cv2.destroyAllWindows()Copy the code

Running Result Display

The relevant data

  • face_recognition
  • OpenCv
  • Boost C++ Libraries
  • Python face recognition for artificial Intelligence — Face_recognition module
  • Application of an open source face recognition library based on Python face_recognition

Hit the pit

1. Solve the problem that cl.exe cannot be found

When installing VS2015, the default is not to install C++. You need to re-run setup, then select modify, select C++ in language, and start installing

– from stackoverflow.com/

2. PIP install face_recognition fails to be executed, and SSL Error is reported

Solution 1 (recommended) : Create a Python file (.py) in the PIP installation folder.

import os  

ini="""[global] 
index-url = https://pypi.doubanio.com/simple/ 
[install] 
trusted-host=pypi.doubanio.com 
"""  
pippath=os.environ["USERPROFILE"]+"\\pip\\"  

if not os.path.exists(pippath):  
    os.mkdir(pippath)  

with open(pippath+"pip.ini","w+") as f:  
    f.write(ini)  
Copy the code

Run the.py file on CMD

This is followed by a very fast download using the PIP Install installation directive

Solution 2: Increase the timeout period

pip --default-timeout=100 install -U pip
Copy the code

Then execute the following instructions to install

pip --default-timeout=100 install -U face_recognition
Copy the code

3. The Python script fails AttributeError: ‘Module’ object has no attribute ‘XXX’

AttributeError: ‘Module’ object has no attribute ‘XXX ‘”. This is actually a problem with the.pyc file.

Problem location:

Check the source file of the import library and find that the source file exists without errors, and the.pyc file of the source file also exists

Solution:

  1. Do not name py scripts the same as Python reserved words, module names, etc
  2. Remove the library’s. Pyc file (because the py script generates a. Pyc file every time it is run; If.pyc files have been generated, the runtime will still run pyc if the code is not updated, so delete the.pyc file) and re-run the code; Or find an environment where you can run the code and copy and replace the.pyc files on the current machine
If you think the article will help you, you can follow my wechat public account which I run with my friends. We will push high-quality technical articles regularly, asking for your attention

Welcome to join the “Big Talk Android” technology exchange group to share and make progress together