python使用dlib进行人脸检测和关键点的示例


Posted in Python onDecember 05, 2020
#!/usr/bin/env python
# -*- coding:utf-8-*-
# file: {NAME}.py
# @author: jory.d
# @contact: dangxusheng163@163.com
# @time: 2020/04/10 19:42
# @desc: 使用dlib进行人脸检测和人脸关键点

import cv2
import numpy as np
import glob
import dlib

FACE_DETECT_PATH = '/home/build/dlib-v19.18/data/mmod_human_face_detector.dat'
FACE_LANDMAKR_5_PATH = '/home/build/dlib-v19.18/data/shape_predictor_5_face_landmarks.dat'
FACE_LANDMAKR_68_PATH = '/home/build/dlib-v19.18/data/shape_predictor_68_face_landmarks.dat'


def face_detect():
  root = '/media/dangxs/E/Project/DataSet/VGG Face Dataset/vgg_face_dataset/vgg_face_dataset/vgg_face_dataset'
  imgs = glob.glob(root + '/**/*.jpg', recursive=True)
  assert len(imgs) > 0

  detector = dlib.get_frontal_face_detector()
  predictor = dlib.shape_predictor(FACE_LANDMAKR_68_PATH)
  for f in imgs:
    img = cv2.imread(f)
    # The 1 in the second argument indicates that we should upsample the image
    # 1 time. This will make everything bigger and allow us to detect more
    # faces.
    dets = detector(img, 1)
    print("Number of faces detected: {}".format(len(dets)))
    for i, d in enumerate(dets):
      x1, y1, x2, y2 = d.left(), d.top(), d.right(), d.bottom()
      print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
        i, x1, y1, x2, y2))

      cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 1)

      # Get the landmarks/parts for the face in box d.
      shape = predictor(img, d)
      print("Part 0: {}, Part 1: {} ...".format(shape.part(0), shape.part(1)))
      # # Draw the face landmarks on the screen.
      '''
      # landmark 顺序: 外轮廓 - 左眉毛 - 右眉毛 - 鼻子 - 左眼 - 右眼 - 嘴巴
      '''
      for i in range(shape.num_parts):
        x, y = shape.part(i).x, shape.part(i).y
        cv2.circle(img, (x, y), 2, (0, 0, 255), 1)
        cv2.putText(img, str(i), (x, y), cv2.FONT_HERSHEY_COMPLEX, 0.3, (0, 0, 255), 1)

    cv2.resize(img, dsize=None, dst=img, fx=2, fy=2)
    cv2.imshow('w', img)
    cv2.waitKey(0)


def face_detect_mask():
  root = '/media/dangxs/E/Project/DataSet/VGG Face Dataset/vgg_face_dataset/vgg_face_dataset/vgg_face_dataset'
  imgs = glob.glob(root + '/**/*.jpg', recursive=True)
  assert len(imgs) > 0

  detector = dlib.get_frontal_face_detector()
  predictor = dlib.shape_predictor(FACE_LANDMAKR_68_PATH)
  for f in imgs:
    img = cv2.imread(f)
    # The 1 in the second argument indicates that we should upsample the image
    # 1 time. This will make everything bigger and allow us to detect more
    # faces.
    dets = detector(img, 1)
    print("Number of faces detected: {}".format(len(dets)))
    for i, d in enumerate(dets):
      x1, y1, x2, y2 = d.left(), d.top(), d.right(), d.bottom()
      print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
        i, x1, y1, x2, y2))

      cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 1)

      # Get the landmarks/parts for the face in box d.
      shape = predictor(img, d)
      print("Part 0: {}, Part 1: {} ...".format(shape.part(0), shape.part(1)))
      # # Draw the face landmarks on the screen.
      '''
      # landmark 顺序: 外轮廓 - 左眉毛 - 右眉毛 - 鼻子 - 左眼 - 右眼 - 嘴巴
      '''
      points = []
      for i in range(shape.num_parts):
        x, y = shape.part(i).x, shape.part(i).y
        if i < 26:
          points.append([x, y])
        # cv2.circle(img, (x, y), 2, (0, 0, 255), 1)
        # cv2.putText(img, str(i), (x,y),cv2.FONT_HERSHEY_COMPLEX, 0.3 ,(0,0,255),1)

      # 只把脸切出来
      points[17:] = points[17:][::-1]
      points = np.asarray(points, np.int32).reshape(-1, 1, 2)
      img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      black_img = np.zeros_like(img)
      cv2.polylines(black_img, [points], 1, 255)
      cv2.fillPoly(black_img, [points], (1, 1, 1))
      mask = black_img
      masked_bgr = img * mask

      # 位运算时需要转化成灰度图像
      mask_gray = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
      masked_gray = cv2.bitwise_and(img_gray, img_gray, mask=mask_gray)

    cv2.resize(img, dsize=None, dst=img, fx=2, fy=2)
    cv2.imshow('w', img)
    cv2.imshow('mask', mask)
    cv2.imshow('mask2', masked_gray)
    cv2.imshow('mask3', masked_bgr)
    cv2.waitKey(0)


if __name__ == '__main__':
  face_detect()

python使用dlib进行人脸检测和关键点的示例

python使用dlib进行人脸检测和关键点的示例

python使用dlib进行人脸检测和关键点的示例

以上就是python使用dlib进行人脸检测和关键点的示例的详细内容,更多关于python 人脸检测的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python中的字典详细介绍
Sep 18 Python
python中文编码问题小结
Sep 28 Python
Python实现类的创建与使用方法示例
Jul 25 Python
机器学习10大经典算法详解
Dec 07 Python
tensorflow 中对数组元素的操作方法
Jul 27 Python
对Python发送带header的http请求方法详解
Jan 02 Python
Python将列表数据写入文件(txt, csv,excel)
Apr 03 Python
python 3.6.7实现端口扫描器
Sep 04 Python
Pycharm配置PyQt5环境的教程
Apr 02 Python
Python如何对齐字符串
Jul 30 Python
python中os.remove()用法及注意事项
Jan 31 Python
Python insert() / append() 用法 Leetcode实战演示
Mar 31 Python
python从ftp获取文件并下载到本地
Dec 05 #Python
python基于socket模拟实现ssh远程执行命令
Dec 05 #Python
Python实现PS滤镜中的USM锐化效果
Dec 04 #Python
python 模拟登陆github的示例
Dec 04 #Python
python中round函数保留两位小数的方法
Dec 04 #Python
python中pow函数用法及功能说明
Dec 04 #Python
python对输出的奇数偶数排序实例代码
Dec 04 #Python
You might like
PHP安装全攻略:APACHE
2006/10/09 PHP
转换中文日期的PHP程序
2006/10/09 PHP
thinkphp路由规则使用示例详解和伪静态功能实现(apache重写)
2014/02/24 PHP
PHP CURL获取返回值的方法
2014/05/04 PHP
PHP图像处理类库及演示分享
2015/05/17 PHP
php实现图片等比例缩放代码
2015/07/23 PHP
如何解决PHP无法实现多线程的问题
2015/09/25 PHP
解决Laravel blade模板转义html标签的问题
2019/09/03 PHP
Valerio 发布了 Mootools
2006/09/23 Javascript
清除网页历史记录,屏蔽后退按钮!
2008/12/22 Javascript
使用jQuery判断IE浏览器版本的代码
2014/06/14 Javascript
jQuery回调函数的定义及用法实例
2014/12/23 Javascript
javascript模拟C#格式化字符串
2015/08/26 Javascript
详解Bootstrap四种图片样式
2016/01/04 Javascript
jQuery实现查找最近父节点的方法
2016/06/23 Javascript
BootStrap 超链接变按钮的实现方法
2016/09/25 Javascript
JS实现焦点图轮播效果的方法详解
2016/12/19 Javascript
Vue.js路由vue-router使用方法详解
2017/03/20 Javascript
详解如何去除vue项目中的#——History模式
2017/10/13 Javascript
angular基于ng-alain定义自己的select组件示例
2018/02/23 Javascript
js实现各浏览器全屏代码实例
2018/07/03 Javascript
Vue 中 a标签上href无法跳转的解决方式
2019/11/12 Javascript
python中使用urllib2获取http请求状态码的代码例子
2014/07/07 Python
快速了解Python中的装饰器
2018/01/11 Python
对python numpy数组中冒号的使用方法详解
2018/04/17 Python
和孩子一起学习python之变量命名规则
2018/05/27 Python
Python实现串口通信(pyserial)过程解析
2019/09/25 Python
Python SQLAlchemy库的使用方法
2020/10/13 Python
CSS3提交意见输入框样式代码
2014/10/30 HTML / CSS
Omio葡萄牙:全欧洲低价大巴、火车和航班搜索和比价
2019/02/09 全球购物
LightInTheBox法国站:中国跨境电商
2020/03/05 全球购物
Molton Brown美国官网:奢华美容、香水、沐浴和身体护理
2020/09/02 全球购物
《胡杨》教学反思
2014/02/16 职场文书
《美丽的彩虹》教学反思
2014/02/25 职场文书
初中班主任经验交流材料
2014/05/16 职场文书
贷款工作证明模板
2015/06/12 职场文书