python 基于dlib库的人脸检测的实现


Posted in Python onNovember 08, 2019

本周暂时比较清闲,可以保持每日一更的速度。

国外身份证项目新增需求,检测出身份证正面的人脸。最开始考虑mobilenet-ssd,经同事提醒,有现成的人脸库dlib,那就用传统方法尝试一下。

dlib安装

dlib的安装小费一波周折,我的python版本是3.6,直接pip install dlib安装失败。https://pypi.org/project/dlib/19.6.0/找到python3.6对应的whl文件下载安装或者直接pip install dlib==19.6.0 提示Successfully installed dlib-19.6.0安装成功。事情没那么简单,import dlib时报错: ImportError: DLL load failed: 找不到指定的模块。

还是版本的问题,查找最新版本的whl :https://pypi.org/simple/dlib/

下载 dlib-19.8.1-cp36-cp36m-win_amd64.whl  然后cd到相应的目录下 pip install dlib-19.8.1-cp36-cp36m-win_amd64.whl

代码

import dlib
import cv2
import os
 
def resize(img, width=None, height=None, inter=cv2.INTER_AREA):
  """
  initialize the dimensions of the input image and obtain
  the image size
  """
 
  dim = None
  (h, w) = img.shape[:2]
 
  if width is None and height is None:
    return img
  if width is None:
    r = height / float(h)
    dim = (int(w * r), height)
  else:
    r = width / float(w)
    dim = (width, int(h * r))
  # resize the image
  resized = cv2.resize(img, dim, interpolation=inter)
  # return the resized image
  return resized
 
# 使用 Dlib 的正面人脸检测器 frontal_face_detector
detector = dlib.get_frontal_face_detector()
 
# 图片所在路径
imgs_path = 'test/'
filelist = os.listdir(imgs_path)
# 使用 detector 检测器来检测图像中的人脸
for img_path in filelist:
  img = cv2.imread(imgs_path + img_path)
  img = resize(img, width=512)
  faces = detector(img, 1)
  print("人脸数 / Faces in all: ", len(faces))
  for i, d in enumerate(faces):
    w = d.right() - d.left()
    h = d.bottom() - d.top()
    d_left = int(d.left() - w * 0.25)
    d_right = int(d.right() + w * 0.25)
    d_top = int(d.top() - w * 0.70)
    d_bottom = int(d.bottom() + w * 0.2)
    print("第", i + 1, "个人脸的矩形框坐标:",
       "left:", d_left, "right:", d_right, "top:", d_top, "bottom:", d_bottom)
    cv2.rectangle(img, tuple([d_left, d_top]), tuple([d_right, d_bottom]), (0, 255, 255), 2)
  cv2.imshow("img", img)
  cv2.waitKey(0)
  cv2.imwrite('./result.jpg',img)

随便网上找了张图测试,效果如下

python 基于dlib库的人脸检测的实现

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现上传样本到virustotal并查询扫描信息的方法
Oct 05 Python
Python中暂存上传图片的方法
Feb 18 Python
Python中的ceil()方法使用教程
May 14 Python
python处理xml文件的方法小结
May 02 Python
python之DataFrame实现excel合并单元格
Feb 22 Python
Python中property函数用法实例分析
Jun 04 Python
python使用__slots__让你的代码更加节省内存
Sep 05 Python
Python I/O与进程的详细讲解
Mar 08 Python
Windows上安装tensorflow  详细教程(图文详解)
Feb 04 Python
python scatter函数用法实例详解
Feb 11 Python
django form和field具体方法和属性说明
Jul 09 Python
Python中常见的反爬机制及其破解方法总结
Jun 10 Python
numpy数组做图片拼接的实现(concatenate、vstack、hstack)
Nov 08 #Python
python实现身份证实名认证的方法实例
Nov 08 #Python
Python Django框架模板渲染功能示例
Nov 08 #Python
Python Django中间件,中间件函数,全局异常处理操作示例
Nov 08 #Python
Django框架下静态模板的继承操作示例
Nov 08 #Python
python中自带的三个装饰器的实现
Nov 08 #Python
python反转列表的三种方式解析
Nov 08 #Python
You might like
How do I change MySQL timezone?
2008/03/26 PHP
php伪静态之APACHE篇
2014/06/02 PHP
laravel 数据迁移与 Eloquent ORM的实现方法
2019/04/12 PHP
PHP多进程简单实例小结
2019/11/09 PHP
Mac系统下搭建Nginx+php-fpm实例讲解
2020/12/15 PHP
用js生产批量批处理执行命令
2008/07/28 Javascript
javascript 无提示关闭窗口脚本
2009/08/17 Javascript
页面按钮禁用与解除禁用的方法
2014/02/19 Javascript
JavaScript人脸识别技术及脸部识别JavaScript类库Tracking.js
2015/09/14 Javascript
js给table赋值的实例代码
2016/10/13 Javascript
javascript实现圣旨卷轴展开效果(代码分享)
2017/03/23 Javascript
Vue.js表单标签中的单选按钮、复选按钮和下拉列表的取值问题
2017/11/22 Javascript
vue 项目如何引入微信sdk接口的方法
2017/12/18 Javascript
mpvue+vuex搭建小程序详细教程(完整步骤)
2018/09/30 Javascript
Vue源码分析之Vue实例初始化详解
2019/08/25 Javascript
Vue中添加滚动事件设置的方法详解
2020/09/14 Javascript
vue移动端下拉刷新和上滑加载
2020/10/27 Javascript
[54:08]LGD女子刀塔学院 DOTA2炼金术士教学
2014/01/09 DOTA
[37:37]DAC2018 4.4 淘汰赛 Optic vs Mineski 第二场
2018/04/05 DOTA
[58:11]守擂赛第二周擂主赛 DeMonsTer vs Leopard
2020/04/28 DOTA
Django中的“惰性翻译”方法的相关使用
2015/07/27 Python
详解Python装饰器由浅入深
2016/12/09 Python
Python使用django搭建web开发环境
2017/06/09 Python
Python基础教程之异常详解
2019/01/10 Python
使用python对多个txt文件中的数据进行筛选的方法
2019/07/10 Python
浅谈django2.0 ForeignKey参数的变化
2019/08/06 Python
Python获取一个用户名的组ID过程解析
2019/09/03 Python
Python 经典算法100及解析(小结)
2019/09/13 Python
pycharm显示远程图片的实现
2019/11/04 Python
Python request中文乱码问题解决方案
2020/09/17 Python
用缩写的指针比较"if(p)" 检查空指针是否可靠?如果空指针的内部表达不是0会怎么样?
2014/01/05 面试题
2014年迎新年活动方案
2014/02/19 职场文书
2014镇党委班子对照检查材料思想汇报
2014/09/23 职场文书
受资助学生感谢信
2015/01/21 职场文书
廉洁自律个人总结
2015/02/14 职场文书
Memcached介绍及php-memcache扩展安装
2021/04/01 PHP