简介

关于人脸检测算法python库的初步认识2

使用CNN的实现人脸检测

简单说明

The example loads a pretrained model and uses it to find faces in images. 这个是使用已经训练好的模型
CNN model is much more accurate than the HOG based model shown in the face_detector.py CNN训练的模型相对于HOG有更好的精准性。 放在GPU上才能达到更好的速度。

code

#!/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
#
#   Compiling dlib should work on any operating system so long as you have
#   CMake installed.  On Ubuntu, this can be done easily by running the
#   command:
#       sudo apt-get install cmake
#
#   Also note that this example requires Numpy which can be installed
#   via the command:
#       pip install numpy

import glob
from imageio import imread
import dlib

paths=glob.glob('faces/*.jpg')
cnn_face_detector = dlib.cnn_face_detection_model_v1('mmod_human_face_detector.dat')
win = dlib.image_window()
for path in paths:
    img = imread(path)
    dets = cnn_face_detector(img, 1)
    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()

训练好的CNN模型

mmod_human_face_detector.dat
下载地址 http://dlib.net/files/mmod_human_face_detector.dat.bz2

posted on 2020-06-18 10:59  HDU李少帅  阅读(222)  评论(0编辑  收藏  举报