ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Dlib 构建人脸检测

2020-05-15 16:07:49  阅读:288  来源: 互联网

标签:predictor img win dlib shape 构建 人脸 dets Dlib


 一、检测整张脸

  步骤:

    1. 安装cmake编译工具: pip install -i http://pypi.douban.com/simple --trusted-host pypi.douban.com cmake 

    2. 安装Dlib库:pip install -i http://pypi.douban.com/simple --trusted-host pypi.douban.com dlib

    3. 编写代码

  

"""使用Dlib检测整张脸"""
import dlib
from imageio import imread
import glob

# 创建人脸检测器对象
detector = dlib.get_frontal_face_detector()
# 创建显示窗口
win = dlib.image_window()
# 图像路径
paths = glob.glob('../images/faceimg/*.jpg')

for path in paths:
    img = imread(path)
    # 1 表示将图片放大一倍,便于检测到更多人脸
    dets = detector(img, 1)
    print('检测到了 %d 个人脸' % len(dets))
    for i, d in enumerate(dets):
        print('- %d:Left %d Top %d Right %d Bottom %d' % (i, d.left(), d.top(), d.right(), d.bottom()))
    
    win.clear_overlay()
    win.set_image(img)
    win.add_overlay(dets)
    dlib.hit_enter_to_continue()

 

二、检测面部关键点(五官)

  步骤:

    1. 配置环境(按一中的步骤)

    2. 下载shape_predictor_68_face_landmarks.dat 模型,此模型为训练数据集得到的68个关键点模型

    3. 编写代码

  

"""使用Dlib检测 面部 + 关键点"""
import dlib
from imageio import imread
import glob

# 创建人脸检测器对象
detector = dlib.get_frontal_face_detector()
# 创建显示窗口
win = dlib.image_window()
# 图像路径
paths = glob.glob('../images/faceimg/*.jpg')
# 下载好的模型:68个脸部关键点训练模型
predictor_path = './shape_predictor_68_face_landmarks.dat'
# 加载模型
predictor = dlib.shape_predictor(predictor_path)

for path in paths:
    img = imread(path)
    win.clear_overlay()
    win.set_image(img)
    
    # 1 表示将图片放大一倍,便于检测到更多人脸
    dets = detector(img, 1)
    print('检测到了 %d 个人脸' % len(dets))
    for i, d in enumerate(dets):
        print('- %d: Left %d Top %d Right %d Bottom %d' % (i, d.left(), d.top(), d.right(), d.bottom()))
        shape = predictor(img, d)
        # 第 0 个点和第 1 个点的坐标
        print('Part 0: {}, Part 1: {}'.format(shape.part(0), shape.part(1)))
        win.add_overlay(shape)
    
    win.add_overlay(dets)
    dlib.hit_enter_to_continue()

 

  

标签:predictor,img,win,dlib,shape,构建,人脸,dets,Dlib
来源: https://www.cnblogs.com/leafchen/p/12895423.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有