基于dlib的人脸检测

dlib的人脸检测模块要比OpenCV的效果好一些,归功于其使用的是HOG特征。

dlib安装

在windows下我的环境是vs2013和cmake3.2,cmake版本低了会导致编译失败。下载dlib的zip包,然后解压,然后执行如下,就把c++和python的配置好了。注意其中的CMakeLists.txt文件中的OpenCV宏与新版本的OpenCV不相配,需要改成OpenCV REQUIRED,否则webcam_face_pose_ex这个与摄像头有关的模块不会执行成功。

cd examples
vim CMakeLists.txt
######################[open CMakeList.txt]
# find_package(OpenCV) -->
find_package(OpenCV REQUIRED)
######################[close CMakeList.txt]
mkdir build
cd build
cmake .. -DUSE_AVX_INSTRUCTIONS=ON
cmake --build . --config Release
cd ../../
python setup.py install --yes USE_AVX_INSTRUCTIONS # AVX为object detector的加速开关

人脸检测demo

exmaples/faces文件夹下面新建文件face_detector.py,根据官网例程添加代码:

import sys

import dlib
from skimage import io

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

for f in sys.argv[1:]:
    print("Processing file: {}".format(f))
    img = io.imread(f)
    # The 1 in the second argument indicates that we should upsample the image
    # 1 time.  This will make everything bigger and allow us to detect more
    # faces.
    dets = detector(img, 1)
    print("Number of faces detected: {}".format(len(dets)))
    for i, d in enumerate(dets):
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            i, d.left(), d.top(), d.right(), d.bottom()))

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


# Finally, if you really want to you can ask the detector to tell you the score
# for each detection.  The score is bigger for more confident detections.
# Also, the idx tells you which of the face sub-detectors matched.  This can be
# used to broadly identify faces in different orientations.
if (len(sys.argv[1:]) > 0):
    img = io.imread(sys.argv[1])
    dets, scores, idx = detector.run(img, 1)
    for i, d in enumerate(dets):
        print("Detection {}, score: {}, face_type:{}".format(
            d, scores[i], idx[i]))

然后执行如下命令即可看到人脸检测效果:

python face_detector.py *.jpg

补充

C++环境也是一样的,执行cmake build后在examples/build目录下生成可执行文件face_detection_ex,执行

face_detection_ex ../faces/*.jpg

注意C++接口要想得到矩形框对应的置信度的话也是可以的,也可以根据置信度设置矩形框筛选的阈值,默认阈值为0,下面为两种接口的函数声明:

template <
    typename image_type
    >
std::vector<rectangle> operator() (
    const image_type& img,
    double adjust_threshold = 0
);

template <
    typename image_type
    >
void operator() (
    const image_type& img,
    std::vector<std::pair<double, rectangle> >& final_dets,
    double adjust_threshold = 0
);

dlib的人脸关键点检测

下载文件http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2解压后将shape_predictor_68_face_landmarks.dat放到examples/build目录,执行

webcam_face_pose_ex shape_predictor_68_face_landmarks.dat