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