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

作者:我永远不知道我是谁 时间:2022-08-23 22:59:07 

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

国外身份证项目新增需求,检测出身份证正面的人脸。最开始考虑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库的人脸检测的实现

来源:https://blog.csdn.net/weixin_42486139/article/details/102931386

标签:python,dlib,人脸检测
0
投稿

猜你喜欢

  • python中的np.argmax() 返回最大值索引号

    2022-08-07 14:22:57
  • pytorch关于Tensor的数据类型说明

    2023-08-22 20:39:27
  • Bootstrap table的使用方法

    2023-09-10 13:34:47
  • Python编写memcached启动脚本代码实例

    2023-02-13 19:59:51
  • 分享一个Emeditor压缩样式的宏

    2010-08-16 12:30:00
  • MySQL20个高性能架构设计原则(值得收藏)

    2024-01-25 04:07:02
  • Vue $emit()不能触发父组件方法的原因及解决

    2024-05-28 16:10:26
  • Python中的左斜杠、右斜杠(正斜杠和反斜杠)

    2022-02-08 07:00:34
  • 学习Python,你还不知道main函数吗

    2022-11-02 10:20:43
  • Python的Flask框架应用调用Redis队列数据的方法

    2023-04-20 14:14:38
  • MYSQL教程:索引和查询优化程序

    2009-02-27 15:52:00
  • ThinkPHP采用GET方式获取中文参数查询无结果的解决方法

    2023-11-23 10:16:36
  • Python常用库Numpy进行矩阵运算详解

    2023-01-04 02:45:52
  • 基于python实现坦克大战游戏

    2023-01-29 01:48:30
  • MySQL获取当前时间的多种方式总结

    2024-01-21 02:29:34
  • MySQL性能优化神器Explain的基本使用分析

    2024-01-19 21:56:15
  • Mysql InnoDB的锁定机制实例详解

    2024-01-23 17:32:27
  • pygame实现简单五子棋游戏

    2022-03-22 08:50:57
  • web.py在SAE中的Session问题解决方法(使用mysql存储)

    2024-01-28 02:17:15
  • Django 添加静态文件的两种实现方法(必看篇)

    2021-09-03 23:53:58
  • asp之家 网络编程 m.aspxhome.com