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使用PyFetion来发送短信的例子
Apr 22 Python
python解析xml文件实例分析
May 27 Python
Python实现的下载网页源码功能示例
Jun 13 Python
浅谈Python实现贪心算法与活动安排问题
Dec 19 Python
django使用LDAP验证的方法示例
Dec 10 Python
详解opencv Python特征检测及K-最近邻匹配
Jan 21 Python
Django框架设置cookies与获取cookies操作详解
May 27 Python
Python操作远程服务器 paramiko模块详细介绍
Aug 07 Python
python实现批量处理将图片粘贴到另一张图片上并保存
Dec 12 Python
总结Pyinstaller的坑及终极解决方法(小结)
Sep 21 Python
使用numpy nonzero 找出非0元素
May 14 Python
python解析照片拍摄时间进行图片整理
Jul 23 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
总集篇&特番节目先行播出!《SAO Alicization War of Underworld》第2季度TV动画4月25日放送!
2020/03/06 日漫
使用bcompiler对PHP文件进行加密的代码
2010/08/29 PHP
基于xcache的配置与使用详解
2013/06/18 PHP
php中curl、fsocket、file_get_content三个函数的使用比较
2014/05/09 PHP
PHP实现通用alert函数的方法
2015/03/11 PHP
php一个文件搞定微信jssdk配置
2016/12/12 PHP
详解PHP 7.4 中数组延展操作符语法知识点
2019/07/19 PHP
php实现将数组或对象写入到文件的方法小结【三种方法】
2020/04/22 PHP
LBS blog sql注射漏洞[All version]-官方已有补丁
2007/08/26 Javascript
IE下使用cloneNode注意事项分享
2012/11/22 Javascript
js实现字符串和数组之间相互转换操作
2016/01/12 Javascript
微信小程序 同步请求授权的详解
2017/08/04 Javascript
js实现轮播图的两种方式(构造函数、面向对象)
2017/09/30 Javascript
Angular4.0中引入laydate.js日期插件的方法教程
2017/12/25 Javascript
VUE 全局变量的几种实现方式
2018/08/22 Javascript
vue-router传参用法详解
2019/01/19 Javascript
Vue Prop属性功能与用法实例详解
2019/02/23 Javascript
Vue-cli项目部署到Nginx服务器的方法
2019/11/01 Javascript
vue项目中使用bpmn为节点添加颜色的方法
2020/04/30 Javascript
vue 通过base64实现图片下载功能
2020/12/19 Vue.js
python微信跳一跳系列之自动计算跳一跳距离
2018/02/26 Python
Python Numpy 数组的初始化和基本操作
2018/03/13 Python
python的pytest框架之命令行参数详解(下)
2019/06/27 Python
Python中print函数简单使用总结
2019/08/05 Python
python模拟键盘输入 切换键盘布局过程解析
2019/08/15 Python
浅谈Django+Gunicorn+Nginx部署之路
2019/09/11 Python
Python基于mediainfo批量重命名图片文件
2020/12/29 Python
python爬取抖音视频的实例分析
2021/01/19 Python
CSS3 实现footer 固定在底部(无论页面多高始终在底部)
2019/10/15 HTML / CSS
Html5 new XMLHttpRequest()监听附件上传进度
2021/01/14 HTML / CSS
中国领先的专业演出票务网:永乐票务
2016/08/29 全球购物
英国皇家造币厂:The Royal Mint
2018/10/05 全球购物
2015年教师自我评价范文
2015/03/04 职场文书
2015年全国爱眼日活动方案
2015/05/05 职场文书
导游词之青岛太清宫
2019/12/13 职场文书
Golang的继承模拟实例
2021/06/30 Golang