Let it recognize you
— Using OpencV fast entry face detection and face recognition
Opencv, as the name implies, “Open source, computer vision”. OpenCV is such a special framework, a group of people around their own time, made an open source computer vision framework. With it we can quickly create computer vision applications. We’re going to use opencv-Python, the Python interface to OpenCV, which is much faster. If you already have Python and PIP installed, simply execute “PIP install opencv-python”. If you don’t have Python installed, it’s easy to download Anaconda3 and install it, which comes with a near-complete Python scientific computing environment. Continue with PIP after that.
With the help of the above tools, we simply need to use dozens of lines of code can quickly achieve face detection and face recognition. Let it recognize you.
Face detection
Face detection technology is one of the basic technology of computer vision. Even in the pre-deep learning era, the technology was highly developed. In the era of deep learning, face recognition generally uses convolutional neural network for supervised learning, that is, to create a useful convolutional kernel (a matrix) by letting the algorithm (neural network) discover the rules by itself, and then use it to find faces in pictures and videos. Before that, people had to design their own algorithms to find faces. But then people invented a face search algorithm similar to deep learning idea, that is haAR algorithm.
To put it simply, this algorithm is to calculate the gray difference between different pixels in a region to determine whether it is a face. The principle is a regular image, such as an object, whether the light is bright or not, the pixel difference between different areas of the object will always have a certain rule. It is with this special idea that scientists have for the first time created a highly effective face recognition algorithm. And its computational performance requirements are really low. It runs fast even on hardware with extremely low computing performance, such as the Raspberry PI 2.
OpenCV internal integration of face detection algorithm, and OpenCV provides a trained face detection HaAR model, save using YML. We only need to simply call the class library can be on the photo or video face detection. Below we can use OpenCV camera call and picture processing module and face detection module to create a real-time detection of face data from the camera, and the relevant data cutting saved procedures. With it, we can create a database of tagged faces that can then be used for face recognition.
Code implementation
Step 1: Load haar data. The trained data can be downloaded from Github, or from my Github: github.com/FontTian/DS…
face_cascade =cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
face_cascade.load('./haarcascades/haarcascade_frontalface_default.xml')
Copy the code
Step two: We need to access the camera and read the data. At the same time, since we need real-time display, we create a loop in which we constantly call to get the picture from the camera and do subsequent processing.
# Get camera
camera = cv2.VideoCapture(0)
# counter for subsequent file saving
count = 0
# create loop
while (True) :# Call the camera to get the image
ret, frame = camera.read()
# Determine if the image is real
if ret == True:...else:
print("no ret")
# Use OpenCV to get keyboard input, exit window if you type q
if cv2.waitKey(int(1000 / 12)) & 0xFF= =ord('q') :break
Copy the code
Step 3: We need to create a grayscale image, and then use a face detector to detect the face of the processed image. At the same time, we use the data returned by the face detector to draw a box in the original picture and display it in real time.
# Convert to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Call face detector detection
faces = face_cascade.detectMultiScale(gray, 1.3.5)
# Draw all faces detected
for (x, y, w, h) in faces:
# picture box
img = cv2.rectangle(frame, (x, y), (x + w, y + h), (255.0.0), 2)
# get face, and convert to 200,200 unified format
The raw data returns a square
f = cv2.resize(gray[y:y + h, x:x + w], (200.200))
# Save images
cv2.imwrite('./data/at/tfs/%s.pgm' % str(count), f)
count += 1
# Show pictures
cv2.imshow('camera', frame)
Copy the code
After doing this, we have a database of faces, and we identify images by folder names.
Face recognition
After completing face detection, we get a face database that can be used for face recognition. Then we can take the face recognition method in OpenCV for face recognition, but there are many ways to realize face recognition. If I were to talk about deep learning now, IT would be a long talk. Therefore, we will use three non-deep learning methods in OpenCV for face recognition, and introduce its use in detail, and briefly introduce its principle.
Code implementation
OpenCV face recognition in the face library, its own three kinds of face recognition. We can directly call the relevant class library for training and testing and use. To do this, we first create a linked list and load the images as a matrix.
import numpy as np
Create an empty list
images = []
# Use OpenCV to load several photos of Fonttian's faces
images.append(cv2.imread("data/at/fonttian/15.pgm",cv2.IMREAD_GRAYSCALE))
images.append(cv2.imread("data/at/fonttian/17.pgm",cv2.IMREAD_GRAYSCALE))
images.append(cv2.imread("data/at/fonttian/55.pgm",cv2.IMREAD_GRAYSCALE))
# Use OpenCV to load several chapters of ZJZ face photos
images.append(cv2.imread("data/at/zjz/15.pgm",cv2.IMREAD_GRAYSCALE))
images.append(cv2.imread("data/at/zjz/17.pgm",cv2.IMREAD_GRAYSCALE))
images.append(cv2.imread("data/at/zjz/60.pgm",cv2.IMREAD_GRAYSCALE))
# create a tag
labels = [0.0.0.1.1.1]
Copy the code
And then we can get different face recognizers to do face recognition. OpenCV comes with three face recognizers — LBPH, EigenFace, and Fisher. We first create a method to show the confidence of the judgment results and output of different face recognizers. The range of confidence selection will be described in detail in the next step.
Create test file path
imgPath = "data/at/fonttian/570.pgm"
# Pass test picture path, recognizer, original face database, face database tag
def showConfidence(imgPath,recognizer,images,labels) :
# Train the recognizer
recognizer.train(images,np.array(labels))
# Load test images
predict_image=cv2.imread(imgPath,cv2.IMREAD_GRAYSCALE)
# Predict and print the results
labels,confidence = recognizer.predict(predict_image)
print("label=",labels)
print("conficence=",confidence)
Copy the code
The above steps are the general steps of a face recognizer, which is essentially the same as a general machine learning model. You need the labeled data, and then you do the classification. So first you need to train with labeled data and make predictions with new data. The key of face recognition is how to extract features from face and use features for face recognition. It was, and is, the eternal pattern of facial recognition.
EigenFace is a face recognizer based on PCA. It is generally considered reliable if the confidence level is below 5,000. PCA is principal component analysis, and its role in face recognition is to extract the main information of a face to identify different people.
LBPH is a histogram of local binary mode. Local binary refers to the size difference between a pixel and its surroundings. 0 and 1 refer to its size relationship. Its core principle is similar to that of HaAR, which is to calculate the difference relation between local pixels for image recognition.
Fisher is a face recognizer based on linear discrimination. Linear discrimination is widely used in many fields of machine learning, classification, clustering, recommendation are stored in its figure. Here its basic idea is consistent with PCA, are dimensionality reduction to obtain the face features of the face and then face recognition through face features.
The specific confidence is shown below. If we directly select print labels, the program will directly give the best result, which may be enough for the test. But a better way to do this is to use confidence.
# EigenFace(PCA, under 5000)
recognizer = cv2.face.EigenFaceRecognizer_create()
showConfidence(imgPath,recognizer,images,labels)
# LBPH (local binary pattern histogram, 0 perfectly matches, below 50 acceptable, 80 unreliable)
recognizer = cv2.face.LBPHFaceRecognizer_create()
showConfidence(imgPath,recognizer,images,labels)
# Fisher(Line discriminant analysis, judgment below 5000 is reliable)
recognizer = cv2.face.FisherFaceRecognizer_create()
showConfidence(imgPath,recognizer,images,labels)
Copy the code
Now running the top one will tell us the final result. Here is the result
Face recognition, face detection technology although has been developing, accuracy and calculation are constantly improving. But as an application or as a field, it’s all about the same. We can start from dozens of lines of OpenCV code, and gradually master face recognition under deep learning.