Loading...  > 本文的内容是对人脸检测的主流方法做一个简单综述,简要介绍这些主流方法的原理、优缺点,以及部分算法的开源实现。限于文章篇幅,我只给出简单/主要的代码以及coding思路,具体实现请访问我的[GitHub repo]()。 > > 从问题的领域来看,人脸检测属于目标检测领域,目标检测通常分为两类: > > - 通用目标检测:图像中有多个类别目标。n(目标)+1(背景)=n+1分类问题。 > - 特定类别目标检测:人脸检测、行人检测、车辆检测等。这类问题核心是1(目标)+1(背景)=2分类问题。 > > 从发展历史来看,人脸检测主要分为非深度学习阶段和深度学习阶段: > > - 非深度学习阶段:主要包括基于rigid-templates的方法(Viola-Jones (VJ)的Haar+AdaBoost分类器,LBP+AdaBoost、HOG+SVM),以及基于可变组件模型(Deformable Part Model, DPM)的方法,DPM是深度学习之前最有效的人脸检测算法。 > - 深度学习阶段:这段时期内的人脸检测算法都是基于通用目标检测提出的,比如R-CNN、SSD等。这些CNN都是多分类方法,但都可以用来解决单分类问题。既然YOLO、SSD这些都可以进行目标检测,那么自然就有问题说为什么不拿这些方法直接进行人脸检测呢,还要专门研究人脸检测的CNN? 虽然多分类网络效果好,但这类方法速度慢、在GPU上都不一定能达到实时,无法满足人脸检测实时性要求。而且类似SSD单阶段目标检测算法对小目标的检测比较差,而人脸刚好是密集的小目标,因此如何提高密集小目标人脸检测性能也是需要研究的问题。 > > Dependencies(necessary) > > 1. Python 3.x > 2. opencv-python lasted version > > Dependencies(optional) > > 1. dlib > 2. opencv-contrib-python > 3. mtcnn(keras + tensorflow) [toc] 完整代码实现在我的[GitHub repo]() ## 1、Traditional Stage(rigid-templates + DPM) ### 1.1 Haar Cascade Classifier in OpenCV `Viola-Jones`对象检测框架,是2004年计算机视觉研究者`Paul Viola`和`Michael Jones`在论文[Rapid Object Detection using a Boosted Cascade of Simple Features](https://www.cs.cmu.edu/~efros/courses/LBMV07/Papers/viola-cvpr-01.pdf)中提出的。它们开发了一个可以实时检测各种物体的框架,但主要的动机是人脸检测,在论文中也主要以人脸检测作为例子展开。`Haar`特征级联分类器主要有四部分构成: 1. 选择`Haar-like`特征 2. 创建积分图`Integral image` 3. 训练`AdaBoost`分类器 4. 创建级联分类器 关于`Haar Cascade Classifier`的更多内容我会在之后的文章中讲解。 #### Advantages - 在CPU上可以实时运行 - 简单的结构 - 可以检测不同尺度的人脸 #### Disadvantages - 只能检测正面人脸,侧脸检测不到 - 会有很多错误检测 - 有遮挡的场景检测效果很差 #### Code - Haar Cascade Classifier ```python haar_face_cascade=cv2.CascadeClassifier('data/haarcascades/haarcascade_frontalface_default.xml') gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY) faces = haar_face_cascade.detectMultiScale(gray, scaleFactor=scaleFactor, minNeighbors=3,flags=0, minSize=(10,10),maxSize=(50,50)) print("faces number:", len(faces)) for (x, y, w, h) in faces: cv2.rectangle(img_copy, (x, y), (x+w, y+h), (0, 255, 0), 2) ``` 上面的代码片段首先加载harr cascade模型文件,并将其应用于灰度图像。输出是包含检测到人脸的一个list。list的每个成员包含四个元素,分别是左上角(x,y)坐标以及检测到人脸的宽度和高度。  ### 1.2 LBP Cascade Classifier in OpenCV `LBP`是纹理描述子,人脸由微纹理图案组成。提取LBP特征,形成特征向量,对人脸和非人脸进行分类。`LBP`级联分类器的算法基本步骤如下: 1. `LBP Labelling`,图像二值化 2. `Feature Vector`,图像被划分为子区域每个子区域构造一个标签直方图。然后将子区域直方图拼接成一个大直方图,形成一个特征向量。 3. `AdaBoost Learning`,利用AdaBoost构造强分类器,取出特征向量中无用的部分。 4. `Cascade Classifier`,由上面获得的`AdaBoost`组成级联分类器。从简单的分类器到强分类器对图像子区域进行评价。如果在该阶段任何一个分类器分类失败,则在该迭代处丢弃。只有面部区域可以通过所有的分类阶段。 #### Advantages - 计算简单、快速 - 比Haar更短的训练时间 - 对局部照明改变更鲁棒 - 对遮挡场景更鲁棒 #### Disadvantages - 相比Haar准确率更低 - 有比较高的错误检测率 #### Code - LBP Cascade Classifier ```python lbp_face_cascade=cv2.CascadeClassifier('data/lbpcascades/lbpcascade_frontalface.xml') gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY) faces = lbp_face_cascade.detectMultiScale(gray, scaleFactor=scaleFactor, minNeighbors=3,flags=0, minSize=(10,10),maxSize=(50,50)) print("faces number:", len(faces)) for (x, y, w, h) in faces: cv2.rectangle(img_copy, (x, y), (x+w, y+h), (0, 255, 0), 2) ``` 与haar人脸检测代码基本相同,只不过加载的是LBP cascade模型文件,然后将其应用于灰度图像。输出是包含检测到人脸的一个list。list的每个成员包含四个元素,分别是左上角(x,y)坐标以及检测到人脸的宽度和高度。  ### 1.3 HOG and SVM in Dlib 方向梯度直方图(Histogram of Oriented Gradient,HOG),是一种全局图像特征描述子。它通过计算和统计图像局部区域的直方图来构成特征。基于`HOG`特征和`SVM`分类器的人脸检测模型被广泛地使用。 #### Advantages - 可以很好的检测正面人脸,和轻度的侧脸。 - 在轻度的遮挡场景下也可以使用。 #### Disadvantages - 主要的缺点是它不检测小的脸。因为在它的训练集中最小的脸是80x80(dlib)。不过,你可以在自己的训练数据上训练HOG人脸检测模型来检测更小的人脸。 - 边框往往排除部分额头,甚至有时候会缺少部分下巴。 - 再严重遮挡场景下不能工作 - 不能检测过度侧脸,和极端非正脸,如向下或向上看。 #### Code - HOG and SVM in Dlib ```python detector = dlib.get_frontal_face_detector() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = detector(gray, 2) # result #to draw faces on image print("faces number:", len(faces)) for result in faces: x = result.left() y = result.top() x1 = result.right() y1 = result.bottom() cv2.rectangle(img, (x, y), (x1, y1), (0, 0, 255), 2) ``` 在上面的代码中,我们首先加载人脸检测器,然后将灰度图像传给检测器,第二个参数是我们要方放大图像的倍数,这个参数越大,越有机会检测到更小的人脸,但对计算的速度会产生相当大的影响(真的很大)。输出是包含检测到人脸的一个list。list的每个成员包含四个元素,分别是左上角坐标和右下角坐标。  ## 2、Deep Learning Stage ### 2.1 mmod in Dlib 这个方法采用了基于CNN的[最大化边缘目标检测器](https://arxiv.org/pdf/1502.00046.pdf)(Maximum-Margin Objet Detector,MMOD)。这个模型是dlib社区的开发者写的。模型可以从[dlib-models](https://github.com/davisking/dlib-models)仓库下载。它使用了一个作者手工标记的数据集,由来自ImageNet、PASCAL VOC、VGG、WIDER等不同数据集的图像组成。它包含7220个图像,数据集下载地址:[dlib_face_detection_dataset](http://dlib.net/files/data/dlib_face_detection_dataset-2016-09-30.tar.gz) #### Advantages - 适用于不同的面部朝向 - 抗遮挡能力强 - 在GPU上运算很快 #### Disadvantages - 在CPU上运行相当相当慢(Python Dlib貌似不支持在GPU上运行) - 和HOG一样,不能检测小于80x80的人脸 - 边界框甚至比HOG检测器的还小。 #### Code - mmod in Dlib ```python dnnFaceDetector=dlib.cnn_face_detection_model_v1("data/mmod_human_face_detector.dat") faceRects = dnnFaceDetector(img, 2) print("faces number:", len(faceRects)) for faceRect in faceRects: x1 = faceRect.rect.left() y1 = faceRect.rect.top() x2 = faceRect.rect.right() y2 = faceRect.rect.bottom() cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2) ``` 代码结构和HOG检测器的基本类似,只不过需要加载`mmod`人脸检测模型。 ### 2.2 DNN Face in OpenCV 自OpenCV3.3版本之后,contrib包含了dnn模块。当然,也有基于DNN的人脸检测模块。基于[SSD](https://arxiv.org/abs/1512.02325)(Single-Shot-Multibox Detector)并且使用ResNet-10结构作为网络主干。 #### Advantages - 比上述四种方法准确率都高 - 在CPU上也可以实时运行(视CPU性能而定) - 适用于不同的面部方向(上、下、左、右、侧面) - 适用于严重遮挡场景 - 可以检测不同尺度的人脸 #### Disadvantages - 不支持GPU #### Code - DNN Face in OpenCV ```python prototxt_path = "data/deploy.prototxt" weight_path = "data/res10_300x300_ssd_iter_140000.caffemodel" net = cv2.dnn.readNetFromCaffe(prototxt_path, weight_path) (h, w) = image.shape[:2] blob = cv2.dnn.blobFromImage(image, 1.0, image.shape[:2], [104, 117, 123], False, False) # Actually, if the size of the output image is set as (300,300), the result could not be good. # blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 117.0, 123.0)) print("[INFO] computing object detections...") net.setInput(blob) detections = net.forward() face_num = detections[:,:,:,2].reshape(detections.shape[2]) print("face numbers:", face_num[face_num>set_confidence].shape[0]) # loop over the detections for i in range(0, detections.shape[2]): # prediction confidence = detections[0, 0, i, 2] if confidence > set_confidence: box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), 2) ``` 首先加载所需要的模型,在这里我使用Caffe的模型。然后将图像转为`blob`,并使用`forward()`函数进行前向计算。输出是一个4-d矩阵。其中第三维可以用于迭代输出人脸检测结果,第四维包含边界框[0,0,i,2]和置信度[0,0,i,3:7]。  ### 2.3 mtcnn 2016年, 由[kaipen Zang](https://kpzhang93.github.io/)等人提出的[使用多任务级联卷积网络的联合人脸检测和对齐](https://arxiv.org/abs/1604.02878)(Joint Face Detection and Alignment using Multi-task Cascaded Convolutional Networks,MTCNN)一度成为人脸检测最受欢迎的方法之一。 `MTCNN`之所以受欢迎,是因为它在一系列基准数据集上获得了当时最先进的结果,而且它还能够识别眼睛、嘴巴、鼻子等其他面部特征。 网络采用三级级联结构,首先将图像重新缩放到不同大小范围(图像金字塔),然后第一个模型`P-Net`提出候选面部区域,第二个模型`R-Net`过滤边界框,第三个模型`O-Net`提取面部其他特征。(MTCNN主要包含三个阶段,第一阶段,通过浅层CNN快速生成候选窗口。然后通过更复杂的CNN对窗口进行挑选、细化。最后,使用一个更强大的CNN来细化结果,并输出面部其他特征的位置。) 下面的图片展示了从上到下三个阶段以及从左到右每个阶段的输出。 <img src="https://cdn.nachr.top/20220828172518.png" style="zoom:50%;" /> `MTCNN`的结构要实现起来相当复杂。但幸运的是,该结构有很多开源的实现,可以在新的数据集上进行训练,也可以使用预训练模型直接进行人脸检测。也许最好的第三方实现是由ipazc实现的[MTCNN](https://github.com/ipazc/mtcnn),而且它还提供`pip`的安装版本(基于keras和tensorflow后端)。 #### Code - MTCNN > 你可以使用pip install MTCNN。MTCNN依赖于2.0以上版本,同时要使用tensorflow作为后端。 ```python detector = MTCNN() img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) faces = detector.detect_faces(img)# result print("face number:", len(faces)) for result in faces: x, y, w, h = result['box'] keypoints = result['keypoints'] x1, y1 = x + w, y + h cv2.rectangle(img, (x, y), (x1, y1), (0, 0, 255), 2) cv2.circle(img,(keypoints['left_eye']), 2, (0,155,255), 2) cv2.circle(img,(keypoints['right_eye']), 2, (0,155,255), 2) ``` 首先构建一个`MTCNN`对象。然后可以通过`detect_faces()`函数直接用于检测照片中的人脸。输出结果是一个list对象,每一个对象为一个字典,通过key-value对应检测结果: - `box`:包含边界框左上角(x,y)坐标,以及边界框的宽和高。 - `confidence`:该人脸预测置信度。 - `keypoings`:提供`left_eye`、`right_eye`、`nose`、`mouth_left`、`mouth_right`等信息的dict。  ## 3、Face Detection Benchmark Datasets ### 3.1 FDDB 网址:[http://vis-www.cs.umass.edu/fddb/results.html](http://vis-www.cs.umass.edu/fddb/results.html) FDDB数据集包含2845张图像中的5171张标注人脸。FDDB包含遮挡、不同姿态、低分辨率、失去对焦的人脸。  ### 3.2 WIDER FACE 网址:[http://mmlab.ie.cuhk.edu.hk/projects/WIDERFace/WiderFace_Results.html](http://mmlab.ie.cuhk.edu.hk/projects/WIDERFace/WiderFace_Results.html) WIDER FACE数据集包含32,203张图片中393,703张标注人脸。Wider Face 数据集基于 61 个事件类进行组织。对于每个事件类,他们随机选择了 40%、10%、50% 的数据作为训练、验证和测试集。  ### 3.3 PASCAL Face PASCAL数据集包含851张图片中1335张标注人脸。 ### 3.4 MALF 网址:[http://www.cbsr.ia.ac.cn/faceEvaluation](http://www.cbsr.ia.ac.cn/faceEvaluation/#) 由李子青老师的研究组创立和维护的,包括5250张图片中11931张标注人脸。其性能评估更细致,分析不同分辨率、角度、性别、年龄等条件下的算法准确率。除了边界框注释,每个面还包含其他注释,例如:偏航、俯仰和滚转的姿势变形级别(小、中、大)。  ### 3.5 UFDD UFDD包含6425张图像中10,897张标注人脸。它涉及关键的退化,包括:雨、雪、阴霾、镜头障碍、模糊、照明变化和干扰物。  ## 4、Further Reading ### 4.1 Traditional methods #### 4.1.1 Haar 【1】[Traditional Face Detection With Python](https://realpython.com/traditional-face-detection-python/#further-reading) 【2】[目标检测的图像特征提取之(三)Haar特征](https://blog.csdn.net/zouxy09/article/details/7929570) 【3】[浅析人脸检测之Haar分类器方法:Haar特征、积分图、 AdaBoost 、级联](https://blog.csdn.net/zouxy09/article/details/7922923) 【4】[计算机视觉目标检测的框架与过程](https://blog.csdn.net/zouxy09/article/details/7928771) #### 4.1.2 LBP 【1】[图像特征提取之LBP特征](https://senitco.github.io/2017/06/12/image-feature-lbp/) #### 4.1.3 HOG 【1】[目标检测的图像特征提取之(一)HOG特征](https://blog.csdn.net/zouxy09/article/details/7929348/) 【2】[图像的全局特征--HOG特征、DPM特征](https://www.cnblogs.com/wishchin/p/9199974.html) 【3】[Application: A Face Detection Pipeline](https://jakevdp.github.io/PythonDataScienceHandbook/05.14-image-features.html) #### 4.1.4 DPM 【1】[Discriminatively trained deformable part models](http://www.rossgirshick.info/latent/) 【2】[ccv](https://libccv.org/tutorial/) ### 4.2 Deep Learning in Face Detection 随着2012年神经网络在图像分类中的应用以及受深度学习在计算机视觉领域发展的影响,许多基于深度学习框架的人脸检测方法在过去几年如雨后春笋般涌现。提出了许多使用不同深度学习架构进行人脸检测的方法。为了更好地更全面地总结现有模型,我们将这些模型分为几个突出的类别: - Cascade-CNN Based Models - R-CNN Based Models - Single Shot Detector Models - Feature Pyramid Network Based Models - Transformers Based Models - Other Architectures 基于 Cascade-CNN 的模型专为高效的高性能人脸检测器而设计,可能适合部署在边缘设备或相机捕捉应用程序中。然而,这些模型很难在具有低图像质量、非标准姿势和照明的具有挑战性的情况下带来最好的准确性。 另一方面,由于使用的模型架构具有更大的容量和学习能力,因此 R-CNN 等基于通用对象的两阶段检测器有更好的准确性。 SSD等单阶段检测器在精度和效率之间提供了很好的权衡。 对于主干架构,基于特征金字塔的架构已被证明在对象检测方面优于标准的 CNN 主干,而且效率没有太大下降。 此外,最近基于Transformer的架构在对象检测方面显示了最先进的结果,并为现有模型的未来扩展提供了潜在的有前途的途径,以将人脸检测的性能提升到一个新的水平。 #### 4.2.1Cascade-CNN Based Models 【1】Zhu, Jun-Yan, et al. "Unpaired image-to-image translation using cycle-consistent adversarial networks." *Proceedings of the IEEE international conference on computer vision*. 2017. 【2】Li, Haoxiang, et al. "A convolutional neural network cascade for face detection." *Proceedings of the IEEE conference on computer vision and pattern recognition*. 2015. #### 4.2.2 R-CNN and Faster-RCNN Based Models 【1】H. Wang, Z. Li, X. Ji, and Y. Wang, “Face r-cnn,” arXiv preprint arXiv:1706.01061, 2017. 【2】Wang, Hao, et al. "Face r-cnn." *arXiv preprint arXiv:1706.01061* (2017). 【3】Wang, Yitong, et al. "Detecting faces using region-based fully convolutional networks." *arXiv preprint arXiv:1709.05256* (2017). #### 4.2.3 SSD based Models 【1】Najibi, Mahyar, et al. "Ssh: Single stage headless face detector." *Proceedings of the IEEE international conference on computer vision*. 2017. 【2】Zhang, Shifeng, et al. "S3fd: Single shot scale-invariant face detector." *Proceedings of the IEEE international conference on computer vision*. 2017. 【3】Zhang, Shifeng, et al. "Refineface: Refinement neural network for high performance face detection." *IEEE Transactions on Pattern Analysis and Machine Intelligence* (2020). #### 4.2.4 Feature Pyramid NetWork Based Models 【1】Zhang, Jialiang, et al. "Feature agglomeration networks for single stage face detection." *Neurocomputing* 380 (2020): 180-189. 【2】Tang, Xu, et al. "Pyramidbox: A context-assisted single shot face detector." *Proceedings of the European Conference on Computer Vision (ECCV)*. 2018. 【3】Chi, Cheng, et al. "Selective refinement network for high performance face detection." *Proceedings of the AAAI Conference on Artificial Intelligence*. Vol. 33. No. 01. 2019. 【4】Li, Jian, et al. "DSFD: dual shot face detector." *Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition*. 2019. 【5】Li, Zhihang, et al. "Pyramidbox++: High performance detector for finding tiny face." *arXiv preprint arXiv:1904.00386* (2019). 【6】Deng, Jiankang, et al. "Retinaface: Single-stage dense face localisation in the wild." *arXiv preprint arXiv:1905.00641* (2019). 【7】Zhu, Yanjia, et al. "TinaFace: Strong but Simple Baseline for Face Detection." *arXiv preprint arXiv:2011.13183* (2020). ### Evaluation Metrics 【1】[准确率(Precision)、召回率(Recall)以及F值(F-Measure)](https://blog.csdn.net/yechaodechuntian/article/details/37394967) ### Overview 【1】[Going Deeper Into Face Detection: A Survey](https://arxiv.org/abs/2103.14983) 【2】[A survey on face detection in the wild: Past, present and future](https://www.sciencedirect.com/science/article/abs/pii/S1077314215000727) 【3】[Face Detection: A Survey](https://www.sciencedirect.com/science/article/abs/pii/S107731420190921X) 【4】[人脸检测算法性能评估、综述及进展](http://jinmeng.github.io/2016/04/25/face-detection-evaluation) 【5】[人脸检测背景介绍和发展现状](https://zhuanlan.zhihu.com/p/32702868) ## References 【1】[Face Detection – OpenCV, Dlib and Deep Learning ( C++ / Python )](https://learnopencv.com/face-detection-opencv-dlib-and-deep-learning-c-python/) 【2】[Real Python -- Traditional Face Detection With Python](https://realpython.com/traditional-face-detection-python/#further-reading) 【3】[pyimagesearch -- Face detection with dlib (HOG and CNN)](https://www.pyimagesearch.com/2021/04/19/face-detection-with-dlib-hog-and-cnn/) 【4】[Github -- Face-Detrection-OpenCV](https://github.com/informramiz/Face-Detection-OpenCV) 【5】[How to Perform Face Detection with Deep Learning](https://machinelearningmastery.com/how-to-perform-face-detection-with-classical-and-deep-learning-methods-in-python-with-keras/) Last modification:August 28th, 2022 at 05:26 pm © 允许规范转载
One comment
感恩,学习到很多,最近刚好在做face recognition