#!/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 人脸检测的资料请关注其它相关文章!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“python使用dlib进行人脸检测和关键点的示例”评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新动态
2025年10月28日
2025年10月28日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]


