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

作者:dangxusheng 时间:2021-12-14 20:56:04 


#!/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进行人脸检测和关键点的示例

来源:https://www.cnblogs.com/dxscode/p/12676293.html

标签:python,dlib,人脸检测
0
投稿

猜你喜欢

  • Go Gin实现文件上传下载的示例代码

    2023-06-21 15:11:13
  • PHP _construct()函数讲解

    2023-06-14 16:56:43
  • asp如何编写sql语句来查询|搜索数据记录

    2008-10-09 12:35:00
  • document.createElement()用法及注意事项

    2008-04-21 15:16:00
  • 仿淘宝首页商品分类列表效果

    2009-01-22 13:39:00
  • Python实现轻松切割MP3文件

    2023-09-23 21:40:32
  • Python 在局部变量域中执行代码

    2023-06-12 04:57:15
  • asp程序定义变量比不定义变量速度快一倍

    2012-12-04 20:06:32
  • 关于Python3 lambda函数的深入浅出

    2023-01-12 09:12:41
  • CSS样式表中SPAN和DIV的区别

    2007-10-21 08:47:00
  • ASP Cookies操作的详细介绍与实例代码

    2011-03-10 10:53:00
  • 巧制可全屏拖动的图片

    2008-05-09 19:34:00
  • 用CSS制作兼容多浏览量器的隐藏菜单

    2007-08-30 09:05:00
  • 用Dreamweaver MX制作导航下拉菜单

    2009-05-29 18:37:00
  • aspjpeg 添加水印教程及生成缩略图教程

    2011-04-04 11:04:00
  • Python解析命令行读取参数--argparse模块使用方法

    2023-06-28 22:48:45
  • window.open被浏览器拦截后的自定义提示

    2007-11-23 12:31:00
  • Dreamweaver如何防止及消除垃圾代码的产生

    2007-11-13 17:15:00
  • ASP连接MSSQL2005 数据库

    2009-03-08 19:20:00
  • 网页栅格系统研究:960的秘密

    2008-10-24 17:03:00
  • asp之家 网络编程 m.aspxhome.com