【Opencv 系列】 第6章 人脸检测(Haar/dlib) & 关键点检测

本章内容

1.人脸检测,分别用Haar 和 dlib
目标:确定图片中人脸的位置,并画出矩形框
在这里插入图片描述

Haar Cascade 哈尔级联

  1. 核心原理
    (1)使用Haar-like特征做检测
    (2)Integral Image : 积分图加速特征计算
    (3)AdaBoost : 选择关键特征,进行人脸和非人脸分类
    (4)Cascade : 级联,弱分类器成为强分类器
    论文:Rapid Object Detection using a Boosted Cascade of Simple Features
    OpenCV 源码:https://github.com/opencv/opencv
    参考博文:https://www.cnblogs.com/zyly/p/9410563.html

(1)使用Haar-like特征做检测
注意:特征值为白色矩形像素和减去黑色矩形像素和
在这里插入图片描述
3. Haar cascade
它提供了四个级联分类器(针对人脸的正面),他只能解决正脸检测的问题,后续课程能够解决侧脸和偏转角脸的检测:
(1)haarcascade_frontalface_alt.xml (FA1):
22 stages and 20 x 20 haar features

(2)haarcascade_frontalface_alt2.xml (FA2):
20 stages and 20 x 20 haar features

(3)haarcascade_frontalface_alt_tree.xml (FAT):
47 stages and 20 x 20 haar features

(4)haarcascade_frontalface_default.xml (FD):
25 stages and 24 x 24 haar features

实际项目效果图
在这里插入图片描述
haar的方式对侧脸很不友好,检测不出来

# 1.导入库
import cv2
import numpy as np
import matplotlib.pyplot as plt

# 2.方法:显示图片
def show_image(image,title,pos):
    #BRG to RGB
    img_RGB = image[:,:,::-1]
    plt.subplot(2,2,pos)
    plt.title(title)
    plt.imshow(img_RGB)
    plt.axis("off")

# 3 方法:绘制图片中检测到的人脸
def plot_rectangle(image, faces):
    # 拿到检测到的人脸数据,返回4个值:坐标(x,y), 宽高width, height
    for (x, y, w, h) in faces:
        cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 3)
    return image

# 4 主函数
def main():
    # 5 读取一张图片
    image = cv2.imread("../images/family.jpg")
    # 6 转成灰度图
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    # 7 通过OpenCV自带的方法cv2.CascadeClassifier()加载级联分类器
    face_alt2 = cv2.CascadeClassifier("haarcascade_frontalface_alt2.xml")

    # 8 通过第7步,对图像中的人脸进行检测
    face_alt2_detect = face_alt2.detectMultiScale(gray)

    # 9 绘制图片中检测到的人脸
    face_alt2_result = plot_rectangle(image.copy(), face_alt2_detect)

    # 10 创建画布
    plt.figure(figsize=(9, 6))
    plt.suptitle("Face detection with Haar Cascade", fontsize=14, fontweight="bold")

    # 11 最终显示整个检测效果
    show_image(face_alt2_result, "face_alt2", 1)

    plt.show()

# 12 主程序入口
if __name__ == '__main__':
    main()

2.dlib人脸检测

dlib的方式就好很多,但是在 win上安装dlib库比较费劲。
linux 下用conda是很好安装的:conda install -c conda-forge dlib

  1. Dlib是一个深度学习开源工具,基于C++开发,也支持Python开发接口。
  2. 由于Dlib对于人脸特征提取支持很好,有很多训练好的人脸特征提取模型供开发者使用,所以Dlib人脸识别开发很适合做人脸项目开发。

官网地址:http://dlib.net
Github 源码库:https://github.com/davisking/dlib
核心代码就是

# 6 调用dlib库中的检测器----核心代码
    detector = dlib.get_frontal_face_detector()
    dets_result = detector(gray,0) # 1 :代表将图片放大一倍,0 代表不放大也不缩小
    print(dets_result)
	结果是左上、右下的坐标对,一对坐标一张人脸
    rectangles[[(830, 90) (1045, 305)], [(174, 194) (353, 373)], [(566, 194) (824, 452)]]
def main():
    # 4读取一张图片
    img = cv2.imread("family.jpg")
    # 5灰度转换, 为什么需要灰度转换?省计算量。 什么情况下能够省计算量,色彩不敏感的情况下,灰度图就是把每一个像素点的R+G+B/3的平均值
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    # 6 调用dlib库中的检测器----核心代码
    detector = dlib.get_frontal_face_detector()
    dets_result = detector(gray,0) # 1 :代表将图片放大一倍,0 代表不放大也不缩小
    print(dets_result)
    # 7 给监测出的人脸绘制矩形框
    img_result = plot_rectangle(img.copy(), dets_result)
    # 8 创建画布
    plt.figure(figsize=(9,6))
    plt.suptitle("face detection with dlib", fontsize=14, fontweight="bold")
    # 9 显示最终的检测效果
    show_image(img_result, "face detection")
    plt.show()

3. hog直方图

  1. HOG 方向梯度直方图(Histogram of Oriented Gradient)

(1)HOG是一种特征描述子,通常用于从图像数据中提取特征。它广泛用于计算机视觉任务的物体检测。

(2)特征描述子的作用:它是图像的简化表示,仅包含有关图像的最重要信息。

论文:《Histograms of Oriented Gradients for Human Detection》
地址:https://lear.inrialpes.fr/people/triggs/pubs/Dalal-cvpr05.pdf

4.关键点检测

在这里插入图片描述

意点:

  1. dlib.get_frontal_face_detector( ) 获取人脸检测器
  2. dlib.shape_predictor( ) 预测人脸关键点

人脸关键点模型,下载地址:
http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2.

5 face_recognition 基于face_recognition进行人脸关键点检测

  1. face_recognition 使用世界上最简单的人脸识别工具,它使用dlib最先进的人脸识别技术构建而成,并具有深度学习功能。

(1)Github 地址:https://github.com/ageitgey/face_recognition
(2)官方指南:
https://face-recognition.readthedocs.io/en/latest/readme.html
(3)源码实现:
https://face-recognition.readthedocs.io/en/latest/face_recognition.html

总结:

git 仓库:https://github.com/justinge/opencv_tutorial

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python人脸关键点检测是指使用Python编程语言进行人脸关键点的定位和检测。人脸关键点通常包括眼睛、鼻子、嘴巴等重要的面部特征点,通过检测这些关键点可以实现人脸识别、表情识别、姿态估计等应用。 在Python中,有多个库可以用于人脸关键点检测,其中比较常用的是dlibOpenCV。下面是一个使用dlib库进行人脸关键点检测的示例代码: ```python import dlib import cv2 # 加载dlib人脸检测器和关键点检测器 detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # 读取图像 image = cv2.imread("face.jpg") # 转换为灰度图像 gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 使用人脸检测器检测人脸 faces = detector(gray) # 遍历每个检测到的人脸 for face in faces: # 使用关键点检测器检测关键点 landmarks = predictor(gray, face) # 遍历每个关键点,并在图像上绘制出来 for n in range(0, 68): x = landmarks.part(n).x y = landmarks.part(n).y cv2.circle(image, (x, y), 2, (0, 255, 0), -1) # 显示结果图像 cv2.imshow("Face Landmarks", image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 上述代码中,首先使用dlib库加载了人脸检测器和关键点检测器。然后读取待检测的图像,并将其转换为灰度图像。接下来使用人脸检测器检测图像中的人脸,并使用关键点检测器检测每个人脸的关键点。最后,遍历每个关键点,并在图像上绘制出来。 需要注意的是,上述代码中使用的shape_predictor_68_face_landmarks.dat文件是一个已经训练好的模型,可以从dlib官方网站下载。此外,还可以根据具体需求对关键点进行进一步处理和应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值