没有合适的资源?快使用搜索试试~ 我知道了~
首页python使用dlib进行人脸检测和关键点的示例
python使用dlib进行人脸检测和关键点的示例
6 下载量 68 浏览量
更新于2023-03-03
评论 1
收藏 800KB PDF 举报
#!/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
资源详情
资源评论
资源推荐
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)
weixin_38681218
- 粉丝: 10
- 资源: 945
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
- SPC统计方法基础知识.pptx
- MW全能培训汽轮机调节保安系统PPT教学课件.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0