OpenCV Face-Recognition

OpenCV视频人脸检测

OpenCV人脸检测分类器

OpenCV预训练了一系列分类器,位于opencv/data/haarcascades/文件夹下,可以直接调用使用

比如用于人脸检测的分类器存储于haarcascade_frontalface_default.xml中,直接将文件中的分类器

1
2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cv2.CascadeClassifier.detectMultiScale(image[, scaleFactor[, minNeighbors[, flags[, minSize[, maxSize]]]]])

detectMultiScale函数的参数如下:

  1. image : Matrix of the type CV_8U containing an image where objects are detected.
  2. scaleFactor : Parameter specifying how much the image size is reduced at each image scale.
    ImageScale.png
    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.
  3. 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.
  4. flags : Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. It is not used for a new cascade.
  5. minSize : Minimum possible object size. Objects smaller than that are ignored.
  6. 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.

参考教程:

https://www.bogotobogo.com/python/OpenCV_Python/python_opencv3_Image_Object_Detection_Face_Detection_Haar_Cascade_Classifiers.php

程序代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#! /usr/local/bin/python
# -*- coding: UTF-8 -*-
import numpy as np
import cv2
import pip
from matplotlib import pyplot as plt

#OpenCV预训练的人脸识别分类器
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

capture = cv2.VideoCapture(0) #打开视频,参数为路径;参数为0表示打开笔记本的内置摄像头
print capture.isOpened() #显示打开是否成功
while 1: #死循环
ret, img=capture.read() #按帧读取视频 ret-帧状态布尔值,img-图像矩阵
img = cv2.imread('shiyanshi10.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.1, 5)#参数见文档,可手动调整

for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]

cv2.imshow('Face Recognition',img)

key=cv2.waitKey(1) #等待键盘输入,间隔1ms waits for a key event infinitely (when [delay]< 0 ) or for [delay] milliseconds,
# print key 鼠标需要点一下视频窗口,使程序接收键盘输入而不是命令行接收键盘输入
if key == 27 : #ESC键的ASCII码
print "detect ESC"
break #退出while循环
capture.release() #释放笔记本摄像头
cv2.destroyAllWindows() #关闭所有图像窗口

运行结果

视频实时人脸检测

多张人脸检测

由于缺少大量实验的活人小伙伴,我用一张图像测试了同时检测多张人脸的功能

经测试f发现scaleFactor=1.1时检测最准确,能够正确检测中此图中所有人脸

0%