文章目录

  • 基本步骤
    • 1、定位图片中的脸
    • 2、在面部ROI中检测出关键的面部结构
        • 什么是ROI
      • 补充函数rect_to_bb,将rect转成坐标点
      • 补充函数shape__to__np
      • 补充函数resive
    • 主要代码
      • 导入相关的包
      • 初始化面部检测器和面部特征预测器
      • 打开图片并读取,将之转换为的灰度图片,固定大小
      • 调用加载好的检测器,对目标进行检测
      • 遍历所有是别人出来的人脸
      • 输出修改之后的图片
    • 最终的代码
    • 实验效果
    • 分析与总结

基本步骤

1、定位图片中的脸

  • 面部检测可以使用很多方式实现,比如说OpenCV内嵌的Haar级联,预训练好的HOG+ 先行SVM对象检测,或者使用深度学习算法进行面部识别。无论什么方法,我们最终都是要获得标定面部的选区

2、在面部ROI中检测出关键的面部结构

  • 主要给出对应面部选区,我们就可以进行面部特征点的检测。有很多不同的面部特征点检测器,但是所有的方法基本上都是针对以下几个器官:嘴、左右眼睫毛、左右眼、鼻子、下巴等。
  • Dlib库中常用的面部特征检测器是One millisecond face alignment with an ensemble of regression trees。这个方法使用了一个人工标注的测试集,标注的内容是围绕面部特征的特定的坐标,坐标表示的是像素点之间的距离。有了这个训练集,就可以训练出来一个集成的回归树,用来检测面部的特征。这个特征检测器的可以进行实时高精度检测。
  • 如果想要的更加深入的了解这个技术,可以通过连接,读相关的文章,配合Dlib的官方文档。
  • 文章的连接
  • Dlib官方文档
  • 在Dlib库中的预先训练好的面部特征检测是针对人脸结构上的68个特征点,分布如下。

什么是ROI
  • 图像处理中,从被处理图像以方框、圆、椭圆等不规则多边形方式勾勒出的需要处理的区域,成为感兴趣区域,ROI。
补充函数rect_to_bb,将rect转成坐标点
  • 描述:将检测器检测出来的rect转换成具体的长方形框的坐标点
  • 原理:detecor返回的值是rect,数据的形式是(x,y,height,width)
def rect_to_bb(rect):# take a bounding predicted by dlib and convert it# to the format (x, y, w, h) as we would normally do# with OpenCVx = rect.left()y = rect.top()w = rect.right() - xh = rect.bottom() - y# return a tuple of (x, y, w, h)return (x, y, w, h)
补充函数shape__to__np
  • 描述:将包含的68个面部区域的坐标点的shape转为numpy数组
def shape_to_np(shape, dtype="int"):# initialize the list of (x, y)-coordinatescoords = np.zeros((68, 2), dtype=dtype)# loop over the 68 facial landmarks and convert them# to a 2-tuple of (x, y)-coordinatesfor i in range(0, 68):coords[i] = (shape.part(i).x, shape.part(i).y)# return the list of (x, y)-coordinatesreturn coords
补充函数resive
  • 描述:将图片按照要求设定大小
  • 参数:image是cv2.imread的对象
  •      width和height是新指定的大小参数
    
def resize(image, width=None, height=None, inter=cv2.INTER_AREA):# initialize the dimensions of the image to be resized and# grab the image sizedim = None(h, w) = image.shape[:2]# if both the width and height are None, then return the# original imageif width is None and height is None:return image# check to see if the width is Noneif width is None:# calculate the ratio of the height and construct the# dimensionsr = height / float(h)dim = (int(w * r), height)# otherwise, the height is Noneelse:# calculate the ratio of the width and construct the# dimensionsr = width / float(w)dim = (width, int(h * r))# resize the imageresized = cv2.resize(image, dim, interpolation=inter)# return the resized imagereturn resized

主要代码

导入相关的包
# import the necessary packages
import numpy as np
import argparse
import dlib
import cv2
初始化面部检测器和面部特征预测器
# initialize dlib's face detector (HOG-based) and then create
# the facial landmark predictor
# 初始化基于HOG预测的预先训练好的检测器
detector = dlib.get_frontal_face_detector()
# 使用的shape-predictor去加载的下载的面部特征训练器
# 括号里的是检测器的路径
predictor = dlib.shape_predictor("the path of the detector")
打开图片并读取,将之转换为的灰度图片,固定大小
# 使用opencv打开图片
image = cv2.imread("1.jpg")
# 统一图片的大小
image = imutils.resize(image,width = 500)
# 将图片转为灰度图片,将BGR图片转为灰度图片
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
  • 具体resize函数
def resize(image, width=None, height=None, inter=cv2.INTER_AREA):# initialize the dimensions of the image to be resized and# grab the image sizedim = None(h, w) = image.shape[:2]# if both the width and height are None, then return the# original imageif width is None and height is None:return image# check to see if the width is Noneif width is None:# calculate the ratio of the height and construct the# dimensionsr = height / float(h)dim = (int(w * r), height)# otherwise, the height is Noneelse:# calculate the ratio of the width and construct the# dimensionsr = width / float(w)dim = (width, int(h * r))# resize the imageresized = cv2.resize(image, dim, interpolation=inter)# return the resized imagereturn resized
  • imread的具体的输出,每一个像素点是以(r,g,b)的形式进行保存的,结果如下


原图片的shape输出,对应的是heightweightchannel,总共是rgb三个颜色的通道,像素点是711*474

  • 修改之后的图片尺寸

  • 转换之后的灰度图片,仅仅只有一个单通道

调用加载好的检测器,对目标进行检测
  • 第一个参数是需要检测的图片
  • 第二个参数是图片的层数,这里是单层图片,只有一个灰度层
# detect face in the grayscale face
# detecting the bounding box of faces in our image
# the second parameter is the number of image pyramid layer
# prior applying the detector we must upscaling the image
rects = detector(gray,1)

  • rects的结果是的坐标和weight和height两对参数
遍历所有是别人出来的人脸
for (i,rect) in enumerate(rects):# i对应的是目标的索引,rect对应是每一个框的起始点坐标和长宽# 定位人脸的关键点,返回的定位之后的68个关键点的位置shape = predictor(gray,rect)# shape是输出之后坐标点,是(68,2),68个点,每个点都是二维的,将所有的坐标点转为numpy数组shape = face_utils.shape_to_np(shape)# 将rect检测人脸的边框转为绘制矩形框的具体位置(x,y,w,h) = face_utils.rect_to_bb(rect)# 绘制人脸的矩形框cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)# 设置矩形框的文字部分cv2.putText(image,"Face #{}".format(i+1),(x-10,y-10),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,255,0),2)# 循环遍历所有的点,将之在原图上进行标注for (x,y) in shape:cv2.circle(image,(x,y),1,(0,0,255),-1)
  • 具体的函数face_utils.shape_to_np的具体代码,将shape的输出结果转为numpy数组
def shape_to_np(shape, dtype="int"):# initialize the list of (x, y)-coordinatescoords = np.zeros((68, 2), dtype=dtype)# loop over the 68 facial landmarks and convert them# to a 2-tuple of (x, y)-coordinatesfor i in range(0, 68):coords[i] = (shape.part(i).x, shape.part(i).y)# return the list of (x, y)-coordinatesreturn coords
输出修改之后的图片
# show the output image with the face detections + facial landmarks
cv2.imshow("Output",image)
cv2.waitKey(0)

最终的代码

# import the necessary packages# import argparse
import cv2
import dlibimport imutils
# the package below from the writer
from imutils import face_utils# intialize dlib face detector adn then create the facial landmark predictor
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")# load the image,resize it and convert it into grayscale
# resice the size of the image into 500 width
# image = cv2.imread(args["image"])
image = cv2.imread("1.jpg")
image = imutils.resize(image,width = 500)
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)# detect face in the grayscale face
# detecting the bounding box of faces in our image
# the second parameter is the number of image pyramid layer
# prior applying the detector we must upscaling the image
rects = detector(gray,1)# Given the coordinates of the face in the image
# loop over the face detections
for (i,rect) in enumerate(rects):# determine the facial landmarks for the face region# convert the coordiantes of the facial landmark to numpy array# predictor is to detect the facila landmarkshape = predictor(gray,rect)# convert the dlib objects to a numpy arrayshape = face_utils.shape_to_np(shape)# convert dlib's rectangle to a OpenCV-style bounding box(x,y,w,h)# then draw the face bounding box(x,y,w,h) = face_utils.rect_to_bb(rect)cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)# show the face numbercv2.putText(image,"Face #{}".format(i+1),(x-10,y-10),cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,255,0),2)# loop over the (x,y)-coordinates for the facial lanmarks# and draw the on the imagefor (x,y) in shape:cv2.circle(image,(x,y),1,(0,0,255),-1)# show the output image with the face detections + facial landmarks
cv2.imshow("Output",image)
cv2.waitKey(0)

实验效果

分析与总结

  • 里面有一个的imutils的包下载地址,不过下不了也没关系,我已经把对应原函数附在了对应调用的地方,你们可以自己改一下

表情识别(一)——使用Dlib、opencv和Python识别面部特征相关推荐

  1. 使用 OpenCV 和 Python 识别信用卡号

    使用 OpenCV 和 Python 识别信用卡号 在之前的博文中,我们学习了如何安装 Tesseract 二进制文件并将其用于 OCR. 然后我们学习了如何使用基本的图像处理技术来清理图像以提高 T ...

  2. 人脸检测进阶:使用 dlib、OpenCV 和 Python 检测面部标记

    使用 dlib.OpenCV 和 Python 检测面部标记 今天,我们将使用dlib和OpenCV来检测图像中的面部标记.dlib的安装教程: https://wanghao.blog.csdn.n ...

  3. 使用 OpenCV 和 Python 识别数字

    使用 OpenCV 和 Python 识别数字 本文演示如何使用 OpenCV 和 Python 识别图像中的数字. 在本教程的第一部分,我们将讨论什么是七段显示器,以及我们如何应用计算机视觉和图像处 ...

  4. python表情识别程序_Python+Dlib+Opencv实现人脸采集并表情判别功能的代码

    一.dlib以及opencv-python库安装 介于我使用的是jupyter notebook,所以在安装dlib和opencv-python时是在 这个命令行安装的 dlib安装方法: 1.若可以 ...

  5. python人脸识别考勤系统 dlib+OpenCV和Pyqt5、数据库sqlite 人脸识别系统 计算机 毕业设计 源码

    一.项目介绍 Python语言.dlib.OpenCV.Pyqt5界面设计.sqlite3数据库 本系统使用dlib作为人脸识别工具,dlib提供一个方法可将人脸图片数据映射到128维度的空间向量,如 ...

  6. python人脸识别理论_使用OpenCV和Python进行人脸识别

    介绍 人脸识别是什么?或识别是什么?当你看到一个苹果时,你的大脑会立刻告诉你这是一个苹果.在这个过程中,你的大脑告诉你这是一个苹果水果,用简单的语言来说就是识别.那么什么是人脸识别呢?我肯定你猜对了. ...

  7. python人脸识别门禁_Python+Opencv+Tkinter指纹识别与人脸识别的门禁兼考勤(一)

    一.设计目标:旨在PC端上搭建一款具有指纹识别与人脸识别功能的门禁兼考勤系统.该系统同时具备普通用户模式.管理员模式与超级管理员模式,下面具体介绍每种模式下的功能. 1)普通用户模式 该模式可分为收集 ...

  8. python视频图片识别算法_python利用Opencv进行人脸识别(视频流+图片)

    首先:需要在在自己本地安装opencv具体步骤可以问度娘 如果从事于开发中的话建议用第三方的人脸识别(推荐阿里) 1.视频流中进行人脸识别 # -*- coding: utf-8 -*- import ...

  9. python人脸识别opencv_python中使用Opencv进行人脸识别

    上一节讲到人脸检测,现在讲一下人脸识别.具体是通过程序采集图像并进行训练,并且基于这些训练的图像对人脸进行动态识别. 人脸识别前所需要的人脸库可以通过两种方式获得:1.自己从视频获取图像 2.从人脸数 ...

  10. python识别人脸多种属性_用Python识别人脸,人种等各种信息

    最近几天了解了一下人脸识别,应用场景可以是图片标注,商品图和广告图中有没有模特,有几个模特,模特的性别,年龄,颜值,表情等数据的挖掘. 基础的识别用dlib来实现,dlib是一个机器学习的包,主要用C ...

最新文章

  1. GAN 为什么需要如此多的噪声?
  2. 让UpdatePanel支持文件上传(2):服务器端组件
  3. 乌当区利用大数据织密环境监测保护网
  4. linux下in命令
  5. Fencing the Cows [USACO]
  6. python中不能使用下标运算的有哪些_Python中最常见的10个问题(列表)
  7. 面试官系统精讲Java源码及大厂真题 - 48 一起看过的 Java 源码和面试真题
  8. 若依集成knife4j实现swagger文档增强
  9. ca蜘蛛特效nvas-nest.js | Bootstrap中文网开源项目免费 CDN 服务
  10. Matlab学习(可以用MATLAB作曲)
  11. 凯撒密码的Java实现
  12. C# SolidWorks二次开发-工程图-更换工程图图纸格式/模板
  13. 两波形相位差的计算值_有功功率、无功功率和视在功率该怎么计算?
  14. 16-大数据处理技巧--数据分析
  15. MeterSphere关联TAPD
  16. 查找一个字符串中的所有子串的位置
  17. HTML5基础之代码入门
  18. 遗传算法(二)——编码
  19. 高云fpga.Tang Nano 4k(GW1NSR-4C)呼吸灯
  20. 南京信息工程大学第二届程序设计大赛团队赛:L-三元对

热门文章

  1. sql2008转到sqk2000的步骤
  2. 独家丨我在北工大看王校长吃热狗
  3. Logistics人口模型
  4. 7天连锁酒店郑南雁:顺势创业者无为管理人
  5. 51单片机毕业设计题目大全
  6. GNSS NMEA-0183协议解析
  7. 三维软件Skyline开发初步
  8. 手把手教你protel 99se 入门
  9. 工业自动化控制软件SCADA数据模型的使用方法实例
  10. 【C语言】指针(野指针)