Article Directory
Haar characteristics
The Haar feature uses adjacent rectangles at a specified position in the detection window to calculate the sum of pixels of each rectangle and take the difference. Then use these differences to classify the sub-regions of the image.
There are several types of haar feature templates:
Take the first haar feature template as an example
Calculation
1. Feature = white-black (use the sum of pixels in the white area minus the symbolic sum of the black area)
2. Feature = entire area * weight + black * weight
Use haar templates to process images
Starting from the starting point of the image, use the haar template to traverse from left to right, traverse from top to bottom, and set the step size, while considering the image size and template size information
If we now have an 1080 * 720
image size, 10*10
the haar templates, and step 2, then I calculate the amount we need is: (1080/720 * 2/2) * * 100 * number of templates equal to about 50 Zoom 10 billion, too much calculation.
Integral graph
The use of integral graphs can greatly reduce the calculation time, in fact, the principle of prefix sum is used
Adaboost classifier
Adaboost is an iterative algorithm. Its core idea is to train different classifiers (weak classifiers) for the same training set, and then group these weak classifiers to form a stronger final classifier (strong classifier).
Algorithm flow
The algorithm is actually a simple weak classification algorithm improvement process. This process can improve the classification ability of data through continuous training. The whole process is as follows:
1. 先通过对N个训练样本的学习得到第一个弱分类器;
2. 将分错的样本和其他的新数据一起构成一个新的N个的训练样本,通过对这个样本的学习得到第二个弱分类器 ;
3. 将1和2都分错了的样本加上其他的新样本构成另一个新的N个的训练样本,通过对这个样本的学习得到第三个弱分类器;
4. 最终经过提升的强分类器。即某个数据被分为哪一类要由各分类器权值决定。
We need to download two Adaboost classifier files from the official website, which are the classifiers for face and eyes:
download address: https://github.com/opencv/opencv/tree/master/data/haarcascades
Code
The basic steps to realize face recognition:
1.加载文件和图片
2.进行灰度处理
3.得到haar特征
4.检测人脸
5.进行标记
We use cv2.CascadeClassifier()
to load the classifier we downloaded.
Then we use the detectMultiScale()
method to get the recognition result
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 1.加载文件和图片 2.进行灰度处理 3.得到haar特征 4.检测人脸 5.标记
face_xml = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_xml = cv2.CascadeClassifier('haarcascade_eye.xml')
img = cv2.imread('img.png')
cv2.imshow('img', img)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 1.灰色图像 2.缩放系数 3.目标大小
faces = face_xml.detectMultiScale(gray, 1.3, 5)
print('face = ',len(faces))
print(faces)
#绘制人脸,为人脸画方框
for (x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x + w, y + h), (255,0,0), 2)
roi_face = gray[y:y+h,x:x+w]
roi_color = img[y:y+h,x:x+w]
eyes = eye_xml.detectMultiScale(roi_face)
print('eyes = ',len(eyes))
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color, (ex,ey),(ex + ew, ey + eh), (0,255,0), 2)
cv2.imshow('dat', img)
cv2.waitKey(0)
face = 1
[[133 82 94 94]]
eyes = 2