python 基于dlib库的人脸检测的实现
Python  /  管理员 发布于 7年前   222
本周暂时比较清闲,可以保持每日一更的速度。
国外身份证项目新增需求,检测出身份证正面的人脸。最开始考虑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 dlibimport cv2import 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_detectordetector = 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)
随便网上找了张图测试,效果如下
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号