系统环境
win7旗舰sp1,64位机
python3.6.2
dlib19.7.0 (其他版本例子没有cnn_face_detector.py和里面对应的一些模块功能和函数,运行该实例会报错)
效果展示
官网
https://pypi.python.org/pypi/dlib#downloads
dlib-19.7.0-cp36-cp36m-win_amd64.whl (md5) 下载
https://github.com/davisking/dlib Git下载
软件、模型和数据下载
链接:http://pan.baidu.com/s/1eRC8PpC 密码:8cfp
列表
应用软件
dlib-19.7.0-cp36-cp36m-win_amd64.whl (pip本地安装使用)
dlib-19.7.0.tar.gz (解压,源文件和实例)
dlib19.7.0运行需要的模型(model)和训练、测试、测试数据(data)集合
dlib_face_recognition_resnet_model_v1.dat.bz2
mmod_human_face_detector.dat.bz2
shape_predictor_5_face_landmarks.dat.bz2
shape_predictor_68_face_landmarks.dat.bz2
说明:.bz2文件解压后得到.dat文件,勿修改文件名(直接删除文件名后缀.bz2),否则运行程序报错。
python各种库(whl格式)下载地址
http://www.lfd.uci.edu/~gohlke/pythonlibs/https://pypi.python.org/pypi?%3Aaction=index
说明:使用whl免去了下载、编译、匹配和调试的操作,节省了大量的时间。
运行命令
python C:/local/dlib-19.7/python_examples/cnn_face_detector.py C:/local/dlib-19.7/python_examples/mmod_human_face_detector.dat C:/local/dlib-19.7/examples/faces/2008_007676.jpg
运行输出
(test) C:\Users\Administrator>python C:/local/dlib-19.7/python_examples/cnn_face
_detector.py C:/local/dlib-19.7/python_examples/mmod_human_face_detector.dat C:/
local/dlib-19.7/examples/faces/2008_007676.jpg
Processing file: C:/local/dlib-19.7/examples/faces/2008_007676.jpg
Number of faces detected: 7
Detection 0: Left: 225 Top: 53 Right: 264 Bottom: 93 Confidence: 1.0600713491439
82
Detection 1: Left: 193 Top: 113 Right: 232 Bottom: 153 Confidence: 1.05377495288
84888
Detection 2: Left: 261 Top: 125 Right: 300 Bottom: 165 Confidence: 1.05047667026
51978
Detection 3: Left: 365 Top: 129 Right: 404 Bottom: 169 Confidence: 1.04934203624
72534
Detection 4: Left: 131 Top: 74 Right: 178 Bottom: 122 Confidence: 1.042161345481
8726
Detection 5: Left: 313 Top: 117 Right: 352 Bottom: 157 Confidence: 1.02432358264
9231
Detection 6: Left: 100 Top: 130 Right: 156 Bottom: 187 Confidence: 1.02354717254
63867
Hit enter to continue
源代码
cnn_face_detector.py
#!/usr/bin/python
# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
#
# This example shows how to run a CNN based face detector using dlib. The
# example loads a pretrained model and uses it to find faces in images. The
# CNN model is much more accurate than the HOG based model shown in the
# face_detector.py example, but takes much more computational power to
# run, and is meant to be executed on a GPU to attain reasonable speed.
#
# You can download the pre-trained model from:
# http://dlib.net/files/mmod_human_face_detector.dat.bz2
#
# The examples/faces folder contains some jpg images of people. You can run
# this program on them and see the detections by executing the
# following command:
# ./cnn_face_detector.py mmod_human_face_detector.dat ../examples/faces/*.jpg
#
#
# COMPILING/INSTALLING THE DLIB PYTHON INTERFACE
# You can install dlib using the command:
# pip install dlib
#
# Alternatively, if you want to compile dlib yourself then go into the dlib
# root folder and run:
# python setup.py install
# or
# python setup.py install --yes USE_AVX_INSTRUCTIONS --yes DLIB_USE_CUDA
# if you have a CPU that supports AVX instructions, you have an Nvidia GPU
# and you have CUDA installed since this makes things run *much* faster.
#
# Compiling dlib should work on any operating system so long as you have
# CMake and boost-python installed. On Ubuntu, this can be done easily by
# running the command:
# sudo apt-get install libboost-python-dev cmake
#
# Also note that this example requires scikit-image which can be installed
# via the command:
# pip install scikit-image
# Or downloaded from http://scikit-image.org/download.html.
import sys
import dlib
from skimage import io
if len(sys.argv) < 3:
print(
"Call this program like this:\n"
" ./cnn_face_detector.py mmod_human_face_detector.dat ../examples/faces/*.jpg\n"
"You can get the mmod_human_face_detector.dat file from:\n"
" http://dlib.net/files/mmod_human_face_detector.dat.bz2")
exit()
cnn_face_detector = dlib.cnn_face_detection_model_v1(sys.argv[1])
win = dlib.image_window()
for f in sys.argv[2:]:
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 = cnn_face_detector(img, 1)
'''
This detector returns a mmod_rectangles object. This object contains a list of mmod_rectangle objects.
These objects can be accessed by simply iterating over the mmod_rectangles object
The mmod_rectangle object has two member variables, a dlib.rectangle object, and a confidence score.
It is also possible to pass a list of images to the detector.
- like this: dets = cnn_face_detector([image list], upsample_num, batch_size = 128)
In this case it will return a mmod_rectangless object.
This object behaves just like a list of lists and can be iterated over.
'''
print("Number of faces detected: {}".format(len(dets)))
for i, d in enumerate(dets):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {} Confidence: {}".format(
i, d.rect.left(), d.rect.top(), d.rect.right(), d.rect.bottom(), d.confidence))
rects = dlib.rectangles()
rects.extend([d.rect for d in dets])
win.clear_overlay()
win.set_image(img)
win.add_overlay(rects)
dlib.hit_enter_to_continue()
遇到问题、分享经验欢迎加入QQ群:452205574