python使用dlib进行人脸检测和关键点的示例
作者:dangxusheng 发布时间:2021-12-14 20:56:04
标签:python,dlib,人脸检测
#!/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()
来源:https://www.cnblogs.com/dxscode/p/12676293.html
0
投稿
猜你喜欢
- 目录一、两个模块二、SMTP端口三、四大步骤1、构造邮件内容2、连接邮件服务器3、登陆邮件服务器4、发送邮件四、常用场景1、纯文本邮件2、发
- 三种遍历列表里面序号和值的方法:最近学习python这门语言,感觉到其对自己的工作效率有很大的提升,特在情人节这一天写下了这篇博客,下面废话
- 本文实例讲述了PHP实现更改hosts文件的方法。分享给大家供大家参考,具体如下:有这样一个需求,我有多个网址希望在不同的时候对应不同的 i
- 以下是几个文件操作过程,创建文件,删除文件,修改文件:
- 在python中,文件使用十分频繁,本文将向大家介绍python文件路径的操作:得到指定文件路径、得到当前文件名、判断文件路径是否存在、获得
- 从CNNIC在2009年的报告中可以看到,超过80%的网民购物之前都要看评论(包括本站、其他站评论),超过80%的网民都比较信任口碑(包括网
- 本文实例讲述了python实现自动重启本程序的方法。分享给大家供大家参考。具体实现方法如下:#!/usr/local/bin/python#
- 如何使用数据绑定控件实现不换页提交数据?Chunfeng.html' 提交页面< html><
- 异步 innerHTMLinnerHTML 插入节点的性能的问题,通常是我们最关注的。在回答这问题时,James Padolsey 给出了他
- 1、应该将 CSS 放置于结构的上方(一般放置于 head 元素内)。CSS 是解释型语言,Firefox 和 IE 在等待 CSS 传输完
- 前言undefined 和 null 的区别是个老生常谈的话题了,之前我对二者的区别只是简单理解,例如二者转成 Boolean 类型都是 f
- jQuery之父John Resig写过一篇《Sub-Pixel Problems in CSS》,一个50px宽的div中有4个float
- 第一类工厂相当于mootools的Native方法,用于创建一些具有扩展能力的类,但这些类并没有继承能力,此类工厂也不能以某个类作为蓝本创建
- 在做网站产品展示页面时,一般会用到缩略图,好处当然是直观醒目让人一目了然。点击进入然后看到大图及具体的介绍。但是缩略图在实现上带来了两个问题
- 1. 把数字转换成字符串,应用"" + 1,虽然看起来比较丑一点,但事实上这个效率是最高的,性能上来说:("&
- 本教程主要介绍css的基础知识,将逐个讲解css的各个属性,过程可能比较枯燥,但会尽力多举例说明.作者的网站:http://jorux.co
- 在跨业务、跨网站发送数据或者业务升级的时候,我们有的时候需要指定发送数据的编码方式,比如页面是utf-8编码的,而发送出去的数据却是GB23
- 昨天又翻了下前段时间WD内部培训的幻灯片,发现了kejun推荐的一篇好文:Javascript Closures,看了之后受益匪浅。这篇文章
- 即text-overflow:ellipsis,需要配合white-space:nowrap使用。运行代码:<div style=&q
- 析构函数:当某个对象成为垃圾或者当对象被显式销毁时执行。GC(Garbage Collector) 在PHP中,没有任何变量指向这个对象时,