Dlib is a popular open source library for face recognition. It is written in C++ and contains many machine learning algorithms. It can also be used in python. Dlib maintains a good update rhythm, and the documents are written quite clearly. The resources involved are marked where to download. It is an excellent open source library for face recognition.
Install Dlib's python library under ubuntu
Python is an important language for machine learning and is relatively easy to use. Although Dlib is written in C++, it can be easily used when compiled into Python.
The installation process is also relatively simple:
The boost library is used in the development of Dlib, and cmake is used during compilation. These two must be installed before installation. If not installed, an error will be reported:
Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-FnblaA/dlib/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-BoT1jf-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-FnblaA/dlib/
Install boost and cmake as follows:
apt-get install libboost-python-dev cmake
First install the libraries scikit-image and cv2 involved in Dlib
pip install scikit-image
pip install opencv-python
You can install Dlib as follows:
pip install dlib
The most important thing in recognition is to extract the features of the object. The same is true for face recognition. It is necessary to extract the features of the face. After the features are available, they can be used for recognition.
code show as below:
#!/usr/bin/python
#coding=utf-8
# 先检测人脸,提取关键点向量
# 人脸关键点检测器 shape_predictor_5_face_landmarks.dat 在 http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2 下载
# 人脸识别模 dlib_face_recognition_resnet_model_v1.dat 在 http://dlib.net/files/dlib_face_recognition_resnet_model_v1.dat.bz2 下载
import sys,os,dlib,glob,numpy
from skimage import io
# 模型
predictor_path = 'shape_predictor_5_face_landmarks.dat'
face_rec_model_path = 'dlib_face_recognition_resnet_model_v1.dat'
# 需要检测的文件
faces_folder_path = 'faceto'
detector = dlib.get_frontal_face_detector()
sp = dlib.shape_predictor(predictor_path)
facerec = dlib.face_recognition_model_v1(face_rec_model_path)
descriptors = []
for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
print("Processing file: {}".format(f))
img = io.imread(f)
# 先检测人脸
dets = detector(img, 1)
print("Number of faces detected: {}".format(len(dets)))
for k, d in enumerate(dets):
shape = sp(img, d)
# 在人脸区域中,提取关键点向量,128D向量
face_descriptor = facerec.compute_face_descriptor(img, shape)
# 转换为numpy array
v = numpy.array(face_descriptor)
print 'face_descriptor',v.shape
print v
Install Dlib's python library under windows
Under Windows, two dependencies also need to be resolved, cmake and boost libraries. Of course, python has been installed on this machine.
cmake is relatively simple, just go to https://cmake.org/download/ to install the corresponding version.
The boost library needs to be compiled
Download the required version at http://www.boost.org/users/history/,
I am using vs2015 and enter the development command line:
C:\Program Files (x86)\Microsoft Visual Studio 14.0>
Unzip the downloaded boost, put it in a specific directory, enter the Boost directory, execute bootstrap.bat, no error is reported, it will let you execute the ./b2 command, you can compile boost, a stage directory will be generated, and the environment variable BOOST_ROOT = D:\boost_1_59_0 and BOOST_LIBRARYDIR = D:\boost_1_59_0\stage\lib
Then compile the python library
b2 -a --with-python address-model=32 toolset=msvc runtime-link=static
At this point, cmake and boost libraries have been installed. Next, just like ubuntu, install scikit-image, cv2 and dlib.
pip install scikit-image
pip install opencv-python
pip install dlib
If an error occurs when installing scikit-image,
Command "c:\python27\python.exe -u -c "import setuptools, tokenize;__file__='c:\\users\\\xc1\xd6\xc7\xe5\xc6\xaf\\appdata\\local\\temp\\pip-build-v_gl7g\\scikit-image\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record c:\users\\appdata\local\temp\pip-juqjlo-record\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in c:\users\appdata\local\temp\pip-build-v_gl7g\scikit-image\
Solution:
Save stdint.h to C:\Users\userName\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\include\
Install again
Download the source code of Dlib on github. The face_detector.py under python_examples is used to mark faces. The annotations of the code are also very detailed. The operation is as follows:
./face_detector.py ../examples/faces/*.jpg
Compile Dlib code with vs2015
You need to study the code of Dlib. Sometimes you need to compile Dlib. Dlib is written in c++. The compiled code on the official website is as follows:
Enter the path to Dlib
mkdir build
cd build
cmake -G "Visual Studio 14 2015 Win64" ..
cmake --build . --config Release
The compilation of Dlib is completed. The Release version is compiled here, and the Release version is also used in the process of use. Many examples of Dlib use opencv, and the path and link library need to be added.
Add under the reference path:
D:\work\dlib-master
D:\work\opencv\build\include
Add c++ preprocessor:
DLIB_JPEG_SUPPORT
Add library link:
D:\work\dlib-master\build\dlib\Release\dlib.lib
D:\work\opencv\build\x64\vc14\lib\opencv_world331.lib
Find face_detection_ex.cpp in examples and add it to the project. You can see the same results as python just now.
Compile Dlib under non-cmake
When you need to further understand the source code of Dlib, you need to look at the code while debugging. This cannot be done when compiling into lib, so we need to add dlib to the project.
Download the dlib code at http://dlib.net/compile.html, unzip it to a certain path, and wait until the project needs to reference it.
Create a new empty win32 console application project for vs2015 and name it Dlib_test.
1. Right-click the project->Properties->C/C++>General->Additional include directories
Add four directories as follows:
D:\work\dlib-master\dlib\external\libjpeg
D:\work\dlib-master\dlib\external\libpng
D:\work\dlib-master\dlib\external\zlib
D:\work\dlib-master
2.Set the library directory
Project->Properties->Linker->General->Additional Library Directory
D:\work\dlib-master\dlib
D:\work\dlib-master\dlib\external
3. Add support for JPG and PNG images:
Properties->C/C++>Preprocessor->Preprocessor Definition
DLIB_JPEG_SUPPORT
DLIB_PNG_SUPPORT
4. Turn off SDL checking
When SDL checking is enabled, the compiler will strictly detect buffer overflows, which will cause some functions to fail to compile.
Project Properties->Configuration Properties->C/C++>SDL Check, select No.
5. Add resource files
Add all files under the following three folders to the resource file
dlib\external\libjpeg
dlib\external\libpng
dlib\external\zlib
6. Add source files
Add two source files to the project:
dlib\all\source.cpp
examples\face_landmark_detection_ex.cpp
The project is as follows:
The main() function face_landmark_detection_ex.cpp
is below . When debugging, enter from this main project, and you can see the two most important algorithms for face recognition, face detection and feature extraction shape_predictor
.