前言

调用训练好的卷积神经网络(CNN)模型进行人脸检测。
模型下载链接:http://dlib.net/files/mmod_human_face_detector.dat.bz2

程序

注:使用了opencv和dlib,需要自行配置环境。

# -*- coding: utf-8 -*-
import sys
import dlib
import cv2# 导入cnn模型
cnn_face_detector = dlib.cnn_face_detection_model_v1(sys.argv[1])for f in sys.argv[2:]:# opencv 读取图片,并显示img = cv2.imread(f, cv2.IMREAD_COLOR)# opencv的bgr格式图片转换成rgb格式b, g, r = cv2.split(img)img2 = cv2.merge([r, g, b])# 进行检测dets = cnn_face_detector(img, 1)# 打印检测到的人脸数print("Number of faces detected: {}".format(len(dets)))# 遍历返回的结果# 返回的结果是一个mmod_rectangles对象。这个对象包含有2个成员变量:dlib.rectangle类,表示对象的位置;dlib.confidence,表示置信度。for i, d in enumerate(dets):face = d.rectprint("Detection {}: Left: {} Top: {} Right: {} Bottom: {} Confidence: {}".format(i, face.left(), face.top(), face.right(), d.rect.bottom(), d.confidence))# 在图片中标出人脸left = face.left()top = face.top()right = face.right()bottom = face.bottom()cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 3)cv2.namedWindow(f, cv2.WINDOW_AUTOSIZE)cv2.imshow(f, img)k = cv2.waitKey(0)
cv2.destroyAllWindows()

程序中已经有注释,也可以参考python dlib学习(一):人脸检测。

运行结果

命令行下,输入:

python cnn_face_detector.py mmod_human_face_detector.dat jobs.jpg obama.jpg silicon_valley.jpg

目录下有以下文件:

结果截图:
(运行速度有点慢,要多等一下)
虽然dlib也支持cuda,怕我的笔记本扛不住还是用的CPU跑的。

官方例程

#!/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 ioif 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 objectThe 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()

python dlib学习(三):调用cnn人脸检测相关推荐

  1. python dlib 年龄 性别_python dlib学习(一):人脸检测

    1.环境安装 Windows: 旧版本安装pip install xxx.whl.以下是whl文件地址: ​ Python Package Index ​ 最新版本安装:不要嫌麻烦,先装上visual ...

  2. python dlib学习(六):训练模型

    前言 前面的博客都是使用dlib官方提供的训练好的模型,进行目标识别. - python dlib学习(一):人脸检测 - python dlib学习(二):人脸特征点标定 - python dlib ...

  3. python dlib学习(二):人脸特征点标定

    前言 上次介绍了人脸检测的程序(python dlib学习(一):人脸检测),这次介绍人脸特征点标定.dlib提供了训练好的模型,可以识别人脸的68个特征点. 下载链接:http://pan.baid ...

  4. python 摄像头标定_python 3利用Dlib 19.7实现摄像头人脸检测特征点标定

    Python 3 利用 Dlib 19.7 实现摄像头人脸检测特征点标定 0.引言 利用python开发,借助Dlib库捕获摄像头中的人脸,进行实时特征点标定: 图1 工程效果示例(gif) 图2 工 ...

  5. python dlib学习(九):人脸聚类

    前言 前面的博客介绍过使用dlib进行人脸检测.比对.检测特征点等等操作. python dlib学习(一):人脸检测 python dlib学习(二):人脸特征点标定 python dlib学习(五 ...

  6. python dlib学习(八):训练人脸特征点检测器

    前言 前面的博客(python dlib学习(二):人脸特征点标定)介绍了使用dlib识别68个人脸特征点,但是当时使用的是dlib官方给出的训练好的模型,这次要自己训练一个特征点检测器出来.当然,想 ...

  7. python dlib学习(七):人脸特征点对齐

    前言 前面的博客介绍过人脸特征点标定:python dlib学习(二):人脸特征点标定.这次试着使用这些人脸特征点来对人脸进行对齐. 完整工程链接附在文章最后. 程序 上代码,程序中使用了python ...

  8. python dlib学习(五):比对人脸

    前言 在前面的博客中介绍了,如何使用dlib标定人脸(python dlib学习(一):人脸检测),提取68个特征点(python dlib学习(二):人脸特征点标定).这次要在这两个工作的基础之上, ...

  9. 深度学习cnn人脸检测_用于对象检测的深度学习方法:解释了R-CNN

    深度学习cnn人脸检测 介绍 (Introduction) CNN's have been extensively used to classify images. But to detect an ...

最新文章

  1. 如何为Linux设置Docker和Windows子系统:爱情故事。 ?
  2. 提升机器学习数学,理论基础的7本著作(文末附资源下载!)
  3. javascript学习(三) 内置对象
  4. 机器视觉与深度神经网络—洗去浮华,一窥珠玑
  5. C语言煎饼排序Pancake sort算法(附完整源码)
  6. 11张图演进SeviceMesh服务网格
  7. 如何创建可扩展的Java应用程序
  8. 艾创机器人_世界教育机器人大赛 2019赛季世界锦标赛落幕曲靖代表队获多个奖项...
  9. SQL Server 性能优化之——系统化方法提高性能
  10. 位置采集[置顶] iPhone手机上的GPS位置信息采集与分享应用
  11. liunx 环境下docker安装mysql
  12. 【语音识别】基于matlab GUI MFCC+VQ说话人识别系统【含Matlab源码 1153期】
  13. 解决Flutter运行IOS报错:Podfile is out of date
  14. 网站的pv、uv、ip分别是什么意思
  15. python爬取拉勾网职位信息_python——拉勾网信息爬取
  16. C语言常见缩写和英文
  17. 深度学习框架江湖群侠传
  18. 更新linux yum源,CentOS 更新yum源
  19. linux怎么找宝塔地址,宝塔Linux面板安全入口地址忘了(方法一)
  20. 图解快速排序——通俗易懂(quick sort)

热门文章

  1. mysql同步row模式_ROW模式的SQL无法正常同步的问题总结
  2. 局部特征(local feature)
  3. Java HashMap工作原理深入探讨
  4. Git笔记(二)——[diff, reset]
  5. hadoop下实现kmeans算法——一个mapreduce的实现方法
  6. computer vision(计算机视觉)方面的期刊会议,学术必备
  7. Ubuntu安装smplayer播放器
  8. 2015年奇虎360服务器开发C++电话面试问题
  9. Java程序员从阿里、京东、美团面试回来,这些面试题你会吗?
  10. sql语句之where子句