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 25 Python
Django中模版的子目录与include标签的使用方法
Jul 16 Python
Python数据结构与算法之完全树与最小堆实例
Dec 13 Python
python 3.5实现检测路由器流量并写入txt的方法实例
Dec 17 Python
Python测试人员需要掌握的知识
Feb 08 Python
Python实用技巧之利用元组代替字典并为元组元素命名
Jul 11 Python
python2.7和NLTK安装详细教程
Sep 19 Python
基于python实现名片管理系统
Nov 30 Python
Django使用中间键实现csrf认证详解
Jul 22 Python
使用Python实现图像标记点的坐标输出功能
Aug 14 Python
Python 解析简单的XML数据
Jul 24 Python
MoviePy简介及Python视频剪辑自动化
Dec 18 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代码
2013/03/24 PHP
Linux下安装oracle客户端并配置php5.3
2014/10/12 PHP
JQuery的Alert消息框插件使用介绍
2010/10/09 Javascript
深入理解JavaScript系列(6) 强大的原型和原型链
2012/01/15 Javascript
jQuery+AJAX实现网页无刷新上传
2015/02/22 Javascript
在JavaScript的正则表达式中使用exec()方法
2015/06/16 Javascript
jQuery实现文本框输入同步的方法
2015/06/20 Javascript
Nodejs的express使用教程
2015/11/23 NodeJs
javascript动态获取登录时间和在线时长
2016/02/25 Javascript
原生JS实现的放大镜效果实例代码
2016/10/15 Javascript
jQuery实现倒计时功能 jQuery实现计时器功能
2017/09/19 jQuery
vue中子组件传递数据给父组件的讲解
2019/01/27 Javascript
Electron vue的使用教程图文详解
2019/07/05 Javascript
js实现简单掷骰子小游戏
2019/10/24 Javascript
Vue实现Layui的集成方法步骤
2020/04/10 Javascript
解决antd 下拉框 input [defaultValue] 的值的问题
2020/10/31 Javascript
[55:44]OG vs NAVI 2019国际邀请赛小组赛 BO2 第一场 8.15
2019/08/17 DOTA
python封装对象实现时间效果
2020/04/23 Python
Python最长公共子串算法实例
2015/03/07 Python
python使用calendar输出指定年份全年日历的方法
2015/04/04 Python
Django重装mysql后启动报错:No module named ‘MySQLdb’的解决方法
2018/04/22 Python
python用fsolve、leastsq对非线性方程组求解
2018/12/15 Python
Python解析、提取url关键字的实例详解
2018/12/17 Python
PyQt5 QTableView设置某一列不可编辑的方法
2019/06/25 Python
python不同系统中打开方法
2020/06/23 Python
pycharm激活码2020最新分享适用pycharm2020最新版亲测可用
2020/11/22 Python
HTML5实现自带进度条和滑块滑杆效果
2018/04/17 HTML / CSS
奇怪的鱼:Weird Fish
2018/03/18 全球购物
中学教师岗位职责
2013/11/26 职场文书
网页美工求职信
2014/02/15 职场文书
幼儿园保育员岗位职责
2014/04/13 职场文书
《望庐山瀑布》教学反思
2014/04/22 职场文书
大二学生学年自我鉴定
2014/09/12 职场文书
介绍信格式
2015/01/30 职场文书
Python基础学习之奇异的GUI对话框
2021/05/27 Python
浅析CSS在DevTools 中架构演变
2021/10/05 HTML / CSS