Dlib库实现人脸关键点检测(Opencv实现)
文章目录
- 1.dlib实现人脸实时检测
- 2.dlib采用检测人脸的68个关键点
- 3.相关文件的下载
- 4.代码实战
- (1)导入库
- (2)人脸关键点的整合
- (3)加载dlib库的人脸检测和人脸关键点检测文件
- (4)对人脸画框
- (5)对检测得到的人脸关键点坐标重新整理
- (6)对人脸的关键点绘制点
- (7)整体代码
- (9)单张图片的人脸关键点检测
1.dlib实现人脸实时检测
https://mydreamambitious.blog.csdn.net/article/details/124851743
2.dlib采用检测人脸的68个关键点
3.相关文件的下载
下载地址:http://dlib.net/files/
4.代码实战
(1)导入库
import os
import cv2
import dlib
import numpy as np
from collections import OrderedDict
(2)人脸关键点的整合
#对于68个检测点,将人脸的几个关键点排列成有序,便于后面的遍历
shape_predictor_68_face_landmark=OrderedDict([('mouth',(48,68)),('right_eyebrow',(17,22)),('left_eye_brow',(22,27)),('right_eye',(36,42)),('left_eye',(42,48)),('nose',(27,36)),('jaw',(0,17))
])
(3)加载dlib库的人脸检测和人脸关键点检测文件
# 加载人脸检测与关键点定位
#http://dlib.net/python/index.html#dlib_pybind11.get_frontal_face_detector
detector = dlib.get_frontal_face_detector()
#http://dlib.net/python/index.html#dlib_pybind11.shape_predictor
criticPoints = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
(4)对人脸画框
#绘制人脸画矩形框
def drawRectangle(detected,frame):margin = 0.2img_h,img_w,_=np.shape(frame)if len(detected) > 0:for i, locate in enumerate(detected):x1, y1, x2, y2, w, h = locate.left(), locate.top(), locate.right() + 1, locate.bottom() + 1, locate.width(), locate.height()xw1 = max(int(x1 - margin * w), 0)yw1 = max(int(y1 - margin * h), 0)xw2 = min(int(x2 + margin * w), img_w - 1)yw2 = min(int(y2 + margin * h), img_h - 1)cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)face = frame[yw1:yw2 + 1, xw1:xw2 + 1, :]cv2.putText(frame, 'Person', (locate.left(), locate.top() - 10),cv2.FONT_HERSHEY_SIMPLEX, 1.2, (255, 0, 0), 3)return frame
(5)对检测得到的人脸关键点坐标重新整理
#对检测之后获取的人脸关键点坐标进行转换
def predict2Np(predict):# 创建68*2关键点的二维空数组[(x1,y1),(x2,y2)……]dims=np.zeros(shape=(predict.num_parts,2),dtype=np.int)#遍历人脸的每个关键点获取二维坐标length=predict.num_partsfor i in range(0,length):dims[i]=(predict.part(i).x,predict.part(i).y)return dims
(6)对人脸的关键点绘制点
#遍历预测框,进行人脸的关键点绘制
def drawCriticPoints(detected,frame):for (step,locate) in enumerate(detected):#对获取的人脸框再进行人脸关键点检测#获取68个关键点的坐标值dims=criticPoints(frame,locate)#将得到的坐标值转换为二维dims=predict2Np(dims)#通过得到的关键点坐标进行关键点绘制# 从i->j这个范围内的都是同一个区域:比如上面的鼻子就是从27->36for (name,(i,j)) in shape_predictor_68_face_landmark.items():#对每个部位进行绘点for (x,y) in dims[i:j]:cv2.circle(img=frame,center=(x,y),radius=2,color=(0,255,0),thickness=-1)return frame
(7)整体代码
import os
import cv2
import dlib
import numpy as np
from collections import OrderedDict
#https://mydreamambitious.blog.csdn.net/article/details/123535760
#对于68个检测点,将人脸的几个关键点排列成有序,便于后面的遍历
shape_predictor_68_face_landmark=OrderedDict([('mouth',(48,68)),('right_eyebrow',(17,22)),('left_eye_brow',(22,27)),('right_eye',(36,42)),('left_eye',(42,48)),('nose',(27,36)),('jaw',(0,17))
])#绘制人脸画矩形框
def drawRectangle(detected,frame):margin = 0.2img_h,img_w,_=np.shape(frame)if len(detected) > 0:for i, locate in enumerate(detected):x1, y1, x2, y2, w, h = locate.left(), locate.top(), locate.right() + 1, locate.bottom() + 1, locate.width(), locate.height()xw1 = max(int(x1 - margin * w), 0)yw1 = max(int(y1 - margin * h), 0)xw2 = min(int(x2 + margin * w), img_w - 1)yw2 = min(int(y2 + margin * h), img_h - 1)cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)face = frame[yw1:yw2 + 1, xw1:xw2 + 1, :]cv2.putText(frame, 'Person', (locate.left(), locate.top() - 10),cv2.FONT_HERSHEY_SIMPLEX, 1.2, (255, 0, 0), 3)return frame#对检测之后获取的人脸关键点坐标进行转换
def predict2Np(predict):# 创建68*2关键点的二维空数组[(x1,y1),(x2,y2)……]dims=np.zeros(shape=(predict.num_parts,2),dtype=np.int)#遍历人脸的每个关键点获取二维坐标length=predict.num_partsfor i in range(0,length):dims[i]=(predict.part(i).x,predict.part(i).y)return dims# 加载人脸检测与关键点定位
#http://dlib.net/python/index.html#dlib_pybind11.get_frontal_face_detector
detector = dlib.get_frontal_face_detector()
#http://dlib.net/python/index.html#dlib_pybind11.shape_predictor
criticPoints = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")#遍历预测框,进行人脸的关键点绘制
def drawCriticPoints(detected,frame):for (step,locate) in enumerate(detected):#对获取的人脸框再进行人脸关键点检测#获取68个关键点的坐标值dims=criticPoints(frame,locate)#将得到的坐标值转换为二维dims=predict2Np(dims)#通过得到的关键点坐标进行关键点绘制# 从i->j这个范围内的都是同一个区域:比如上面的鼻子就是从27->36for (name,(i,j)) in shape_predictor_68_face_landmark.items():#对每个部位进行绘点for (x,y) in dims[i:j]:cv2.circle(img=frame,center=(x,y),radius=2,color=(0,255,0),thickness=-1)return frame#单张图片的人脸关键点检测
def signal_detect(img_path='images/face1.jpg'):img=cv2.imread(img_path)detected=detector(img)frame=drawRectangle(detected,img)frame = drawCriticPoints(detected, img)cv2.imshow('frame',frame)cv2.waitKey(0)cv2.destroyAllWindows()#实时的人脸关键点检测
def detect_time():cap=cv2.VideoCapture(0)while cap.isOpened():ret,frame=cap.read()detected = detector(frame)frame = drawRectangle(detected, frame)frame=drawCriticPoints(detected,frame)cv2.imshow('frame', frame)key=cv2.waitKey(1)if key==27:breakcap.release()cv2.destroyAllWindows()if __name__ == '__main__':print('Pycharm')signal_detect()# detect_time()
(9)单张图片的人脸关键点检测
注:实时检测部分读者可以自己测试。
Dlib库实现人脸关键点检测(Opencv实现)相关推荐
- Opencv与dlib联合进行人脸关键点检测与识别
前言 依赖库:opencv 2.4.9 /dlib 19.0/libfacedetection 本篇不记录如何配置,重点在实现上.使用libfacedetection实现人脸区域检测,联合dlib标记 ...
- OpenCV基于dlib进行人脸关键点检测(摄像头)
1. dlib.get_frontal_face_detector()获取人脸检测器 2. dlib.shape_predictor()预测人脸关键点 人脸关键点模型,下载地址: http://dli ...
- 【dlib库】进行人脸检测+人脸关键点检测+人脸对齐
原图像: 1. 人脸检测 import cv2 import dlib import matplotlib.pyplot as plt # 获取图片 my_img = cv2.imread('my_i ...
- 基于Python,dlib实现人脸关键点检测
@代码实现及安装过程 基于Python,dlib实现人脸关键点检测 dilb 在做人脸检测人脸识别方面用到比较多的.face_recognition就是基于dlib实现的. 这篇文章将使用Python ...
- 使用opencv的LBF算法进行人脸关键点检测
首先下载最新的opencv 和opencv_contrib, 然后按照在Windows下编译扩展OpenCV 3.1.0 + opencv_contrib的方法进行编译,其中核心一点就是先编译open ...
- caffe 人脸关键点检测_人脸检测关键点新增至81个,比Dlib更精准、更贴边
人脸关键点检测是人脸识别和分析领域中的关键一步,它是诸如自动人脸识别.表情分析.三维人脸重建及三维动画等其它人脸相关问题的前提和突破口. 虽然人脸的结构是确定的,由眉毛.眼睛.鼻子和嘴等部位组成,近似 ...
- 使用OpenCV进行人脸关键点检测
使用OpenCV进行人脸关键点检测 1 双脸效果 2 单脸效果
- OpenCV实战之人脸美颜美型(三)——人脸关键点检测
目录 前言 标注方式 4点/5点/6点标注 68点标注 人脸关键点评价指标 检测方法概述 使用OpenCV实现人脸关键点检测 Facemark API 基于LBF的人脸关键点检测 API调用 前言 人 ...
- 人脸识别,人脸关键点检测算法
from http://blog.csdn.net/sloanqin/article/details/48193119 1 Face++:http://www.faceplusplus.com.cn/ ...
- 基于人脸关键点检测的驾驶员睡意检测系统
摘要 驾驶员注意力不集中或者分心是道路交通事故的主要原因. 为了减少道路交通事故,设计开发驾驶员疲劳检测系统至关重要. 本研究利用人脸关键点检测方法提出了驾驶员睡意检测系统,目的是使驾驶更安全. 一. ...
最新文章
- nagios监控安装
- 企业桌面安全管理解决方案
- 文巾解题 面试题 03.06. 动物收容所
- 【数学建模】MATLAB应用实战系列(七十九)-因子分析法(附MATLAB 和Python代码实现)
- SHE PWM的simulink仿真谐波分析
- 开源you-get项目爬虫,以及基于python+selenium的自动测试利器
- Qt工作笔记-ListWidget拖动(拖拽)到QGraphicsScene
- [笔记]前端 - 下拉菜单的实现
- A股开盘:深证区块链50指数跌0.52%,华闻集团跌停
- 【Flutter--实战】Dart 语言快速入门
- Android TextView 右上角文字角标(TM, ©,® )
- win7系统提示此windows副本不是正版怎么办?
- 短信中心号码iphone_如何在iPhone上阻止来自特定号码的呼叫
- 服务器每天自动变密码,Windows自动修改系统密码分享
- 【前端】Vue+Element UI案例:通用后台管理系统-用户管理:Table表格增删查改、Pagination分页、搜索框
- The bean ‘Xxx‘ could not be injected as a ‘Xxx‘ because it is a JDK dynamic proxy that implements:x
- 市场模式缭乱,合适的模式脱颖而出?众筹卖货模式在线分享分析
- Thread优先级之优先级别
- 【video frame interpolation系列1】背景知识: forward and backward image warping (图像扭曲/变换)
- Premiere背后的视频剪辑原理