本章内容

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

Haar Cascade 哈尔级联

核心原理 (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

Dlib是一个深度学习开源工具,基于C++开发,也支持Python开发接口。由于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直方图

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.关键点检测

意点:

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

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

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

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

文章来源

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。