OpenCV视频人脸检测
OpenCV人脸检测分类器
OpenCV预训练了一系列分类器,位于opencv/data/haarcascades/
文件夹下,可以直接调用使用
比如用于人脸检测的分类器存储于haarcascade_frontalface_default.xml
中,直接将文件中的分类器
1 | face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') |
detectMultiScale
函数的参数如下:
- image : Matrix of the type CV_8U containing an image where objects are detected.
- scaleFactor : Parameter specifying how much the image size is reduced at each image scale.
Picture source: Viola-Jones Face Detection
This scale factor is used to create scale pyramid as shown in the picture. Suppose, the scale factor is 1.03, it means we’re using a small step for resizing, i.e. reduce size by 3 %, we increase the chance of a matching size with the model for detection is found, while it’s expensive. - minNeighbors : Parameter specifying how many neighbors each candidate rectangle should have to retain it. This parameter will affect the quality of the detected faces: higher value results in less detections but with higher quality. We’re using 5 in the code.
- flags : Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. It is not used for a new cascade.
- minSize : Minimum possible object size. Objects smaller than that are ignored.
- maxSize : Maximum possible object size. Objects larger than that are ignored.
If faces are found, it returns the positions of detected faces as Rect(x,y,w,h). Once we get these locations, we can create a ROI for the face and apply eye detection on this ROI.
参考教程:
程序代码
1 | #! /usr/local/bin/python |
运行结果
视频实时人脸检测
多张人脸检测
由于缺少大量实验的活人小伙伴,我用一张图像测试了同时检测多张人脸的功能
经测试f发现scaleFactor=1.1
时检测最准确,能够正确检测中此图中所有人脸