Thanks for sharing -bjbsair.com/2020-04-03/…

What is face recognition

Face recognition is a kind of biometric identification technology based on facial feature information. Using a camera or camera to collect images or video streams containing faces, and automatically detect and track faces in the image, and then carry out face recognition on the detected face of a series of related technologies, usually called portrait recognition, face recognition.

The current face recognition technology has been very mature, but also developed into 3D face recognition. And now the major manufacturers have also provided face recognition API interface for us to call, can say that a few lines of code can complete face recognition. But face recognition is based on image processing. The most powerful image processing library in Python is OpenCV.

Introduction of OpenCV

OpenCV is a cross-platform computer vision library distributed under the BSD license that runs on Linux, Windows, Android, and Mac OS operating systems. OpenCV can be used to develop real-time image processing, computer vision, and pattern recognition programs. It is lightweight and efficient — it consists of a series of C functions and a small number of C++ classes. It also provides interfaces to Python, Ruby, MATLAB and other languages and implements many common algorithms in image processing and computer vision.

Basic use of OpenCV

The installation

pip install opencv-python  # base libraries
pip install opencv-contrib-python  # extension libraries
pip install opencv-python-headless  

Copy the code

Read the pictures

Reading and displaying images is the most basic operation. OpenCV uses imread and imshow to implement this operation

import cv2 as cv  
  
The path cannot contain the Chinese name, otherwise the image can not be read
image = cv.imread('1111.jpg')  
  
# display images
cv.imshow('image', image)  
  
# wait for keyboard input in milliseconds, 0 indicates infinite wait
cv.waitKey(0)  
  
# because the C++ object is called, we need to free the memory
cv.destroyAllWindows()  

Copy the code

Turn the image to grayscale

OpenCV hundreds of methods for converting between different color controls. At present, there are three most commonly used: gray scale, BGR, HSV.

  • Gray color space is transformed into gray scale by removing color information. Gray scale will greatly reduce the color processing in image processing, which is very effective for face recognition.
  • Each pixel of BGR is represented by a triplet, coded blue, green, and red. There is also a library PIL in Python that reads images in RGB, which is the same, but in a different color order
  • HSV, where H is the hue, S is the saturation and V is the degree of darkness converts the image to grayscale
import cv2 as cv  
  
The path cannot contain the Chinese name, otherwise the image can not be read
image = cv.imread('1111.jpg')  
  
# cv2 reads images through BGR.
# PIL read the picture channel is RGB
# code selects COLOR_BGR2GRAY, which is BGR to GRAY
gray_image = cv.cvtColor(image, code=cv.COLOR_BGR2GRAY)  
  
# display images
cv.imshow('image', gray_image)  
  
# wait for keyboard input in milliseconds, 0 indicates infinite wait
cv.waitKey(0)  
  
# because the C++ object is called, we need to free the memory
cv.destroyAllWindows()  

Copy the code

Draw a rectangle

image = cv.imread('1111.jpg')  
  
x, y, w, h = 50, 50, 80, 80  
  
# draw rectangle
cv.rectangle(image, (x, y, x+w, y+h), color=(0, 255, 0), thickness=2)  
  
# draw circle
cv.circle(image, center=(x + w//2, y + h//2), radius=w//2, color=(0, 0, 255), thickness=2)  
  
cv.imshow('image', image)  
cv.waitKey(0)  
cv.destroyAllWindows()  

Copy the code

Face detection

Face detection is actually feature extraction from images. Haar feature is a feature used to realize real-time face tracking. Each Haar feature describes the contrast pattern of adjacent image regions. Edges, fixed points, and thin lines, for example, can generate discriminating features. OpenCV provides us with Haar feature data, in the cv2/data directory, Def detectMultiScale(self, image, scaleFactor=None, minNeighbors=None, flags=None, minSize=None, maxSize=None) def detectMultiScale(self, image, scaleFactor=None, minSize=None, maxSize=None)

  • ScaleFactor: Specifies how much each image is scaled down
  • MinNeighbors: Specifies how many neighbors must be reserved for each candidate rectangle. A larger value indicates a higher precision
  • MinSize: minimum rectangle size detected
  • MaxSize: The maximum rectangle size detected

Detect faces in pictures

import os  
import cv2 as cv  
  
def face_detect_demo(image):  
    # Convert the image to grayscale
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)  
  
    # Load characteristic data
    face_detector = cv.CascadeClassifier(os.path.join(cv.data.haarcascades, 'haarcascade_frontalface_default.xml'))  
    faces = face_detector.detectMultiScale(gray)  
    for x, y, w, h in faces:  
        cv.rectangle(image, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=2)  
  
The path cannot contain the Chinese name, otherwise the image can not be read
image = cv.imread('2222.jpg')  
  
face_detect_demo(image)  
  
# display images
cv.imshow('image', image)  
  
# wait for keyboard input in milliseconds, 0 indicates infinite wait
cv.waitKey(0)  
  
# because the C++ object is called, we need to free the memory
cv.destroyAllWindows()  

Copy the code

Default parameters are used to detect incomplete face data. Parameters of the detectMultiScale function need to be adjusted. Adjusted for faces = face_detector. DetectMultiScale (gray, scaleFactor = 1.02, minNeighbors = 3)

We found that in addition to face data detected, there are some other dirty data, this time can print the detected face data location and size

Faces = face_detector. DetectMultiScale (gray, scaleFactor = 1.02, minNeighbors = 3)for x, y, w, h in faces:  
    print(x, y, w, h) Print the location and size of each detected data
    cv.rectangle(image, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=2)  

Copy the code

From the size, we can see that the two largest rectangles, which happen to be face data, and the rest are dirty data, Then continue to modify the function parameter faces. = face_detector detectMultiScale (gray, scaleFactor = 1.02, minNeighbors = 3, minSize = (80, 80))

Detect faces in video

Video is composed of a picture, in the video frame above repeat this process can complete the video face detection. VideoCapture OpenCV provides a function called VideoCapture, which can be a video file or 0 (to call the camera).

import cv2 as cv  
  
# Face detection
def face_detect_demo(image):  
    try:  
        # Convert the image to grayscale
        gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)  
  
        # Load characteristic data
        face_detector = cv.CascadeClassifier(os.path.join(cv.data.haarcascades, 'haarcascade_frontalface_default.xml'))  
        faces = face_detector.detectMultiScale(gray)  
        for x, y, w, h in faces:  
            print(x, y, w, h)  
            cv.rectangle(image, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=2)  
    except Exception as e:  
        pass  
  
cap = cv.VideoCapture('Face Recognition. Mp4')  
while cap.isOpened():  
    flag, frame = cap.read()  
  
    face_detect_demo(frame)  
    cv.imshow('result', frame)  
    if ord('q') == cv.waitKey(5):  
        break  
  
cap.realse()  
cv.destroyAllWindows()  

Copy the code

This we are to do face recognition, how to identify hobbies, so advanced? Well, obviously that’s not what we’re looking for, because hobbies can only be kept to ourselves, not if you test them for me. So we have to optimize. OpenCV provides us with a little module of machine learning that we can train to recognize only the parts we need and not guess.

Training data

The training data is when we give the training model some pictures to familiarize it with, so that it can identify the same pictures more accurately. The training data can generally be searched from the Internet: face recognition database, or save the data of beautiful frames from the video as the training set. All face recognition algorithms have two arguments in their train() function: an image array and a label array. These tags indicate the face ID of the person being identified.

Get training set

Take a picture from the video every 5 frames and save it as a picture

import cv2  
  
cap = cv2.VideoCapture('Face Recognition. Mp4')  
  
number = 100  
count = 1  
while cap.isOpened() and number > 0:  
    flag, frame = cap.read()  
    if not flag:  
        break  
  
    if count % 5 == 0:  
        # Crop according to the general position of the face in the video image, and only the part of the face is taken
        img = frame[70:280, 520:730]  
        cv2.imwrite('data/{}.png'.format(number), img)  
        number -= 1  
    count += 1  
  
cap.release()  
cv2.destroyAllWindows()  

Copy the code

Training model using LBPH

def getImageAndLabels(path_list):  
    faces = []  
    ids = []  
    image_paths = [os.path.join(path_list, f) for f in os.listdir(path_list) if f.endswith('.png')]  
    face_detector = cv.CascadeClassifier(os.path.join(cv.data.haarcascades, 'haarcascade_frontalface_default.xml'))  
    for image in image_paths:  
        img = cv.imread(image)  
        gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)  
        faces = face_detector.detectMultiScale(gray)  
        _id = int(os.path.split(image)[1].split('. ') [0])for x, y, w, h in faces:  
            faces.append(gray[y:y+h, x:x+w])  
            ids.append(_id)  
    return faces, ids  
  
faces, ids = getImageAndLabels('data')  
  
# training
recognizer = cv.face.LBPHFaceRecognizer_create()  
recognizer.train(faces, np.array(ids))  
  
# Save training features
recognizer.write('trains/trains.yml')  

Copy the code

Face recognition based on LBPH

LBPH divides the detected faces into small units and compares them with their counterparts in the model, generating a histogram of the matching values for each region. The predict function is called from the adjusted area, which returns an array of two elements, the first being the individual tags identified and the second being the confidence score. All algorithms have a confidence score threshold, which measures the gap between the image and the model, with 0 indicating a perfect match. LBPH has a good identification reference value below 50. The basic steps are as follows:

  • CV.VideoCapture Reads the video
  • Haar algorithm detects face data
  • Based on the LBPH training set to get accurate face data, and output to mark who the person is
  • According to the confidence of high accuracy of the face mark out
import os  
import cv2 as cv  
  
def face_detect_demo(image):  
    try:  
        global number  
        # Convert the image to grayscale
        gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)  
  
        # Load characteristic dataFaces = face_detector. DetectMultiScale (gray, scaleFactor = 1.02, minNeighbors = 3)for x, y, w, h in faces:  
            If the value is greater than 80, the value is incorrect
            _id, confidence = recognizer.predict(gray[y:y + h, x:x + w])  
            if confidence < 80:  
                cv.rectangle(image, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=2)  
    except Exception as e:  
        pass  
  
  
def check_face():  
    cap = cv.VideoCapture('Face Recognition. Mp4')  
    while cap.isOpened():  
        flag, frame = cap.read()  
        if not flag:  
            break  
        face_detect_demo(frame)  
        cv.imshow('img', frame)  
        cv.waitKey(2)  
  
    cv.destroyAllWindows()  
  
if __name__ == '__main__':  
    Load the training data file
    recognizer = cv.face.LBPHFaceRecognizer_create()  
    recognizer.read('trains/trains.yml')  
  
    face_detector = cv.CascadeClassifier(os.path.join(cv.data.haarcascades, 'haarcascade_frontalface_default.xml'))  
    check_face()  

Copy the code

conclusion

Through the above step by step learning, you do not have a basic understanding of OpenCV face recognition? However, we also see that the main algorithm of face recognition is still based on Haar, and the accuracy is not particularly high, mainly because it will detect a lot of non-face data. LBPH asks us to tag someone’s face and tell us who it is, but it doesn’t improve the actual detection accuracy. Now machine learning is very popular, machine learning face recognition based on OpenCV is also very accurate, next time we will compare several machine learning face recognition libraries. Thanks for sharing -bjbsair.com/2020-04-03/…

What is face recognition

Face recognition is a kind of biometric identification technology based on facial feature information. Using a camera or camera to collect images or video streams containing faces, and automatically detect and track faces in the image, and then carry out face recognition on the detected face of a series of related technologies, usually called portrait recognition, face recognition.

The current face recognition technology has been very mature, but also developed into 3D face recognition. And now the major manufacturers have also provided face recognition API interface for us to call, can say that a few lines of code can complete face recognition. But face recognition is based on image processing. The most powerful image processing library in Python is OpenCV.

Introduction of OpenCV

OpenCV is a cross-platform computer vision library distributed under the BSD license that runs on Linux, Windows, Android, and Mac OS operating systems. OpenCV can be used to develop real-time image processing, computer vision, and pattern recognition programs. It is lightweight and efficient — it consists of a series of C functions and a small number of C++ classes. It also provides interfaces to Python, Ruby, MATLAB and other languages and implements many common algorithms in image processing and computer vision.

Basic use of OpenCV

The installation

pip install opencv-python  # base libraries
pip install opencv-contrib-python  # extension libraries
pip install opencv-python-headless  

Copy the code

Read the pictures

Reading and displaying images is the most basic operation. OpenCV uses imread and imshow to implement this operation

import cv2 as cv  
  
The path cannot contain the Chinese name, otherwise the image can not be read
image = cv.imread('1111.jpg')  
  
# display images
cv.imshow('image', image)  
  
# wait for keyboard input in milliseconds, 0 indicates infinite wait
cv.waitKey(0)  
  
# because the C++ object is called, we need to free the memory
cv.destroyAllWindows()  

Copy the code

Turn the image to grayscale

OpenCV hundreds of methods for converting between different color controls. At present, there are three most commonly used: gray scale, BGR, HSV.

  • Gray color space is transformed into gray scale by removing color information. Gray scale will greatly reduce the color processing in image processing, which is very effective for face recognition.
  • Each pixel of BGR is represented by a triplet, coded blue, green, and red. There is also a library PIL in Python that reads images in RGB, which is the same, but in a different color order
  • HSV, where H is the hue, S is the saturation and V is the degree of darkness converts the image to grayscale
import cv2 as cv  
  
The path cannot contain the Chinese name, otherwise the image can not be read
image = cv.imread('1111.jpg')  
  
# cv2 reads images through BGR.
# PIL read the picture channel is RGB
# code selects COLOR_BGR2GRAY, which is BGR to GRAY
gray_image = cv.cvtColor(image, code=cv.COLOR_BGR2GRAY)  
  
# display images
cv.imshow('image', gray_image)  
  
# wait for keyboard input in milliseconds, 0 indicates infinite wait
cv.waitKey(0)  
  
# because the C++ object is called, we need to free the memory
cv.destroyAllWindows()  

Copy the code

Draw a rectangle

image = cv.imread('1111.jpg')  
  
x, y, w, h = 50, 50, 80, 80  
  
# draw rectangle
cv.rectangle(image, (x, y, x+w, y+h), color=(0, 255, 0), thickness=2)  
  
# draw circle
cv.circle(image, center=(x + w//2, y + h//2), radius=w//2, color=(0, 0, 255), thickness=2)  
  
cv.imshow('image', image)  
cv.waitKey(0)  
cv.destroyAllWindows()  

Copy the code

Face detection

Face detection is actually feature extraction from images. Haar feature is a feature used to realize real-time face tracking. Each Haar feature describes the contrast pattern of adjacent image regions. Edges, fixed points, and thin lines, for example, can generate discriminating features. OpenCV provides us with Haar feature data, in the cv2/data directory, Def detectMultiScale(self, image, scaleFactor=None, minNeighbors=None, flags=None, minSize=None, maxSize=None) def detectMultiScale(self, image, scaleFactor=None, minSize=None, maxSize=None)

  • ScaleFactor: Specifies how much each image is scaled down
  • MinNeighbors: Specifies how many neighbors must be reserved for each candidate rectangle. A larger value indicates a higher precision
  • MinSize: minimum rectangle size detected
  • MaxSize: The maximum rectangle size detected

Detect faces in pictures

import os  
import cv2 as cv  
  
def face_detect_demo(image):  
    # Convert the image to grayscale
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)  
  
    # Load characteristic data
    face_detector = cv.CascadeClassifier(os.path.join(cv.data.haarcascades, 'haarcascade_frontalface_default.xml'))  
    faces = face_detector.detectMultiScale(gray)  
    for x, y, w, h in faces:  
        cv.rectangle(image, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=2)  
  
The path cannot contain the Chinese name, otherwise the image can not be read
image = cv.imread('2222.jpg')  
  
face_detect_demo(image)  
  
# display images
cv.imshow('image', image)  
  
# wait for keyboard input in milliseconds, 0 indicates infinite wait
cv.waitKey(0)  
  
# because the C++ object is called, we need to free the memory
cv.destroyAllWindows()  

Copy the code

Default parameters are used to detect incomplete face data. Parameters of the detectMultiScale function need to be adjusted. Adjusted for faces = face_detector. DetectMultiScale (gray, scaleFactor = 1.02, minNeighbors = 3)

We found that in addition to face data detected, there are some other dirty data, this time can print the detected face data location and size

Faces = face_detector. DetectMultiScale (gray, scaleFactor = 1.02, minNeighbors = 3)for x, y, w, h in faces:  
    print(x, y, w, h) Print the location and size of each detected data
    cv.rectangle(image, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=2)  

Copy the code

From the size, we can see that the two largest rectangles, which happen to be face data, and the rest are dirty data, Then continue to modify the function parameter faces. = face_detector detectMultiScale (gray, scaleFactor = 1.02, minNeighbors = 3, minSize = (80, 80))

Detect faces in video

Video is composed of a picture, in the video frame above repeat this process can complete the video face detection. VideoCapture OpenCV provides a function called VideoCapture, which can be a video file or 0 (to call the camera).

import cv2 as cv  
  
# Face detection
def face_detect_demo(image):  
    try:  
        # Convert the image to grayscale
        gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)  
  
        # Load characteristic data
        face_detector = cv.CascadeClassifier(os.path.join(cv.data.haarcascades, 'haarcascade_frontalface_default.xml'))  
        faces = face_detector.detectMultiScale(gray)  
        for x, y, w, h in faces:  
            print(x, y, w, h)  
            cv.rectangle(image, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=2)  
    except Exception as e:  
        pass  
  
cap = cv.VideoCapture('Face Recognition. Mp4')  
while cap.isOpened():  
    flag, frame = cap.read()  
  
    face_detect_demo(frame)  
    cv.imshow('result', frame)  
    if ord('q') == cv.waitKey(5):  
        break  
  
cap.realse()  
cv.destroyAllWindows()  

Copy the code

This we are to do face recognition, how to identify hobbies, so advanced? Well, obviously that’s not what we’re looking for, because hobbies can only be kept to ourselves, not if you test them for me. So we have to optimize. OpenCV provides us with a little module of machine learning that we can train to recognize only the parts we need and not guess.

Training data

The training data is when we give the training model some pictures to familiarize it with, so that it can identify the same pictures more accurately. The training data can generally be searched from the Internet: face recognition database, or save the data of beautiful frames from the video as the training set. All face recognition algorithms have two arguments in their train() function: an image array and a label array. These tags indicate the face ID of the person being identified.

Get training set

Take a picture from the video every 5 frames and save it as a picture

import cv2  
  
cap = cv2.VideoCapture('Face Recognition. Mp4')  
  
number = 100  
count = 1  
while cap.isOpened() and number > 0:  
    flag, frame = cap.read()  
    if not flag:  
        break  
  
    if count % 5 == 0:  
        # Crop according to the general position of the face in the video image, and only the part of the face is taken
        img = frame[70:280, 520:730]  
        cv2.imwrite('data/{}.png'.format(number), img)  
        number -= 1  
    count += 1  
  
cap.release()  
cv2.destroyAllWindows()  

Copy the code

Training model using LBPH

def getImageAndLabels(path_list):  
    faces = []  
    ids = []  
    image_paths = [os.path.join(path_list, f) for f in os.listdir(path_list) if f.endswith('.png')]  
    face_detector = cv.CascadeClassifier(os.path.join(cv.data.haarcascades, 'haarcascade_frontalface_default.xml'))  
    for image in image_paths:  
        img = cv.imread(image)  
        gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)  
        faces = face_detector.detectMultiScale(gray)  
        _id = int(os.path.split(image)[1].split('. ') [0])for x, y, w, h in faces:  
            faces.append(gray[y:y+h, x:x+w])  
            ids.append(_id)  
    return faces, ids  
  
faces, ids = getImageAndLabels('data')  
  
# training
recognizer = cv.face.LBPHFaceRecognizer_create()  
recognizer.train(faces, np.array(ids))  
  
# Save training features
recognizer.write('trains/trains.yml')  

Copy the code

Face recognition based on LBPH

LBPH divides the detected faces into small units and compares them with their counterparts in the model, generating a histogram of the matching values for each region. The predict function is called from the adjusted area, which returns an array of two elements, the first being the individual tags identified and the second being the confidence score. All algorithms have a confidence score threshold, which measures the gap between the image and the model, with 0 indicating a perfect match. LBPH has a good identification reference value below 50. The basic steps are as follows:

  • CV.VideoCapture Reads the video
  • Haar algorithm detects face data
  • Based on the LBPH training set to get accurate face data, and output to mark who the person is
  • According to the confidence of high accuracy of the face mark out
import os  
import cv2 as cv  
  
def face_detect_demo(image):  
    try:  
        global number  
        # Convert the image to grayscale
        gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)  
  
        # Load characteristic dataFaces = face_detector. DetectMultiScale (gray, scaleFactor = 1.02, minNeighbors = 3)for x, y, w, h in faces:  
            If the value is greater than 80, the value is incorrect
            _id, confidence = recognizer.predict(gray[y:y + h, x:x + w])  
            if confidence < 80:  
                cv.rectangle(image, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=2)  
    except Exception as e:  
        pass  
  
  
def check_face():  
    cap = cv.VideoCapture('Face Recognition. Mp4')  
    while cap.isOpened():  
        flag, frame = cap.read()  
        if not flag:  
            break  
        face_detect_demo(frame)  
        cv.imshow('img', frame)  
        cv.waitKey(2)  
  
    cv.destroyAllWindows()  
  
if __name__ == '__main__':  
    Load the training data file
    recognizer = cv.face.LBPHFaceRecognizer_create()  
    recognizer.read('trains/trains.yml')  
  
    face_detector = cv.CascadeClassifier(os.path.join(cv.data.haarcascades, 'haarcascade_frontalface_default.xml'))  
    check_face()  

Copy the code

conclusion

Through the above step by step learning, you do not have a basic understanding of OpenCV face recognition? However, we also see that the main algorithm of face recognition is still based on Haar, and the accuracy is not particularly high, mainly because it will detect a lot of non-face data. LBPH asks us to tag someone’s face and tell us who it is, but it doesn’t improve the actual detection accuracy. Now machine learning is very popular, machine learning face recognition based on OpenCV is also very accurate, next time we will compare several machine learning face recognition libraries. Thanks for sharing -bjbsair.com/2020-04-03/…

What is face recognition

Face recognition is a kind of biometric identification technology based on facial feature information. Using a camera or camera to collect images or video streams containing faces, and automatically detect and track faces in the image, and then carry out face recognition on the detected face of a series of related technologies, usually called portrait recognition, face recognition.

The current face recognition technology has been very mature, but also developed into 3D face recognition. And now the major manufacturers have also provided face recognition API interface for us to call, can say that a few lines of code can complete face recognition. But face recognition is based on image processing. The most powerful image processing library in Python is OpenCV.

Introduction of OpenCV

OpenCV is a cross-platform computer vision library distributed under the BSD license that runs on Linux, Windows, Android, and Mac OS operating systems. OpenCV can be used to develop real-time image processing, computer vision, and pattern recognition programs. It is lightweight and efficient — it consists of a series of C functions and a small number of C++ classes. It also provides interfaces to Python, Ruby, MATLAB and other languages and implements many common algorithms in image processing and computer vision.

Basic use of OpenCV

The installation

pip install opencv-python  # base libraries
pip install opencv-contrib-python  # extension libraries
pip install opencv-python-headless  

Copy the code

Read the pictures

Reading and displaying images is the most basic operation. OpenCV uses imread and imshow to implement this operation

import cv2 as cv  
  
The path cannot contain the Chinese name, otherwise the image can not be read
image = cv.imread('1111.jpg')  
  
# display images
cv.imshow('image', image)  
  
# wait for keyboard input in milliseconds, 0 indicates infinite wait
cv.waitKey(0)  
  
# because the C++ object is called, we need to free the memory
cv.destroyAllWindows()  

Copy the code

Turn the image to grayscale

OpenCV hundreds of methods for converting between different color controls. At present, there are three most commonly used: gray scale, BGR, HSV.

  • Gray color space is transformed into gray scale by removing color information. Gray scale will greatly reduce the color processing in image processing, which is very effective for face recognition.
  • Each pixel of BGR is represented by a triplet, coded blue, green, and red. There is also a library PIL in Python that reads images in RGB, which is the same, but in a different color order
  • HSV, where H is the hue, S is the saturation and V is the degree of darkness converts the image to grayscale
import cv2 as cv  
  
The path cannot contain the Chinese name, otherwise the image can not be read
image = cv.imread('1111.jpg')  
  
# cv2 reads images through BGR.
# PIL read the picture channel is RGB
# code selects COLOR_BGR2GRAY, which is BGR to GRAY
gray_image = cv.cvtColor(image, code=cv.COLOR_BGR2GRAY)  
  
# display images
cv.imshow('image', gray_image)  
  
# wait for keyboard input in milliseconds, 0 indicates infinite wait
cv.waitKey(0)  
  
# because the C++ object is called, we need to free the memory
cv.destroyAllWindows()  

Copy the code

Draw a rectangle

image = cv.imread('1111.jpg')  
  
x, y, w, h = 50, 50, 80, 80  
  
# draw rectangle
cv.rectangle(image, (x, y, x+w, y+h), color=(0, 255, 0), thickness=2)  
  
# draw circle
cv.circle(image, center=(x + w//2, y + h//2), radius=w//2, color=(0, 0, 255), thickness=2)  
  
cv.imshow('image', image)  
cv.waitKey(0)  
cv.destroyAllWindows()  

Copy the code

Face detection

Face detection is actually feature extraction from images. Haar feature is a feature used to realize real-time face tracking. Each Haar feature describes the contrast pattern of adjacent image regions. Edges, fixed points, and thin lines, for example, can generate discriminating features. OpenCV provides us with Haar feature data, in the cv2/data directory, Def detectMultiScale(self, image, scaleFactor=None, minNeighbors=None, flags=None, minSize=None, maxSize=None) def detectMultiScale(self, image, scaleFactor=None, minSize=None, maxSize=None)

  • ScaleFactor: Specifies how much each image is scaled down
  • MinNeighbors: Specifies how many neighbors must be reserved for each candidate rectangle. A larger value indicates a higher precision
  • MinSize: minimum rectangle size detected
  • MaxSize: The maximum rectangle size detected

Detect faces in pictures

import os  
import cv2 as cv  
  
def face_detect_demo(image):  
    # Convert the image to grayscale
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)  
  
    # Load characteristic data
    face_detector = cv.CascadeClassifier(os.path.join(cv.data.haarcascades, 'haarcascade_frontalface_default.xml'))  
    faces = face_detector.detectMultiScale(gray)  
    for x, y, w, h in faces:  
        cv.rectangle(image, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=2)  
  
The path cannot contain the Chinese name, otherwise the image can not be read
image = cv.imread('2222.jpg')  
  
face_detect_demo(image)  
  
# display images
cv.imshow('image', image)  
  
# wait for keyboard input in milliseconds, 0 indicates infinite wait
cv.waitKey(0)  
  
# because the C++ object is called, we need to free the memory
cv.destroyAllWindows()  

Copy the code

Default parameters are used to detect incomplete face data. Parameters of the detectMultiScale function need to be adjusted. Adjusted for faces = face_detector. DetectMultiScale (gray, scaleFactor = 1.02, minNeighbors = 3)

We found that in addition to face data detected, there are some other dirty data, this time can print the detected face data location and size

Faces = face_detector. DetectMultiScale (gray, scaleFactor = 1.02, minNeighbors = 3)for x, y, w, h in faces:  
    print(x, y, w, h) Print the location and size of each detected data
    cv.rectangle(image, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=2)  

Copy the code

From the size, we can see that the two largest rectangles, which happen to be face data, and the rest are dirty data, Then continue to modify the function parameter faces. = face_detector detectMultiScale (gray, scaleFactor = 1.02, minNeighbors = 3, minSize = (80, 80))

Detect faces in video

Video is composed of a picture, in the video frame above repeat this process can complete the video face detection. VideoCapture OpenCV provides a function called VideoCapture, which can be a video file or 0 (to call the camera).

import cv2 as cv  
  
# Face detection
def face_detect_demo(image):  
    try:  
        # Convert the image to grayscale
        gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)  
  
        # Load characteristic data
        face_detector = cv.CascadeClassifier(os.path.join(cv.data.haarcascades, 'haarcascade_frontalface_default.xml'))  
        faces = face_detector.detectMultiScale(gray)  
        for x, y, w, h in faces:  
            print(x, y, w, h)  
            cv.rectangle(image, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=2)  
    except Exception as e:  
        pass  
  
cap = cv.VideoCapture('Face Recognition. Mp4')  
while cap.isOpened():  
    flag, frame = cap.read()  
  
    face_detect_demo(frame)  
    cv.imshow('result', frame)  
    if ord('q') == cv.waitKey(5):  
        break  
  
cap.realse()  
cv.destroyAllWindows()  

Copy the code

This we are to do face recognition, how to identify hobbies, so advanced? Well, obviously that’s not what we’re looking for, because hobbies can only be kept to ourselves, not if you test them for me. So we have to optimize. OpenCV provides us with a little module of machine learning that we can train to recognize only the parts we need and not guess.

Training data

The training data is when we give the training model some pictures to familiarize it with, so that it can identify the same pictures more accurately. The training data can generally be searched from the Internet: face recognition database, or save the data of beautiful frames from the video as the training set. All face recognition algorithms have two arguments in their train() function: an image array and a label array. These tags indicate the face ID of the person being identified.

Get training set

Take a picture from the video every 5 frames and save it as a picture

import cv2  
  
cap = cv2.VideoCapture('Face Recognition. Mp4')  
  
number = 100  
count = 1  
while cap.isOpened() and number > 0:  
    flag, frame = cap.read()  
    if not flag:  
        break  
  
    if count % 5 == 0:  
        # Crop according to the general position of the face in the video image, and only the part of the face is taken
        img = frame[70:280, 520:730]  
        cv2.imwrite('data/{}.png'.format(number), img)  
        number -= 1  
    count += 1  
  
cap.release()  
cv2.destroyAllWindows()  

Copy the code

Training model using LBPH

def getImageAndLabels(path_list):  
    faces = []  
    ids = []  
    image_paths = [os.path.join(path_list, f) for f in os.listdir(path_list) if f.endswith('.png')]  
    face_detector = cv.CascadeClassifier(os.path.join(cv.data.haarcascades, 'haarcascade_frontalface_default.xml'))  
    for image in image_paths:  
        img = cv.imread(image)  
        gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)  
        faces = face_detector.detectMultiScale(gray)  
        _id = int(os.path.split(image)[1].split('. ') [0])for x, y, w, h in faces:  
            faces.append(gray[y:y+h, x:x+w])  
            ids.append(_id)  
    return faces, ids  
  
faces, ids = getImageAndLabels('data')  
  
# training
recognizer = cv.face.LBPHFaceRecognizer_create()  
recognizer.train(faces, np.array(ids))  
  
# Save training features
recognizer.write('trains/trains.yml')  

Copy the code

Face recognition based on LBPH

LBPH divides the detected faces into small units and compares them with their counterparts in the model, generating a histogram of the matching values for each region. The predict function is called from the adjusted area, which returns an array of two elements, the first being the individual tags identified and the second being the confidence score. All algorithms have a confidence score threshold, which measures the gap between the image and the model, with 0 indicating a perfect match. LBPH has a good identification reference value below 50. The basic steps are as follows:

  • CV.VideoCapture Reads the video
  • Haar algorithm detects face data
  • Based on the LBPH training set to get accurate face data, and output to mark who the person is
  • According to the confidence of high accuracy of the face mark out
import os  
import cv2 as cv  
  
def face_detect_demo(image):  
    try:  
        global number  
        # Convert the image to grayscale
        gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)  
  
        # Load characteristic dataFaces = face_detector. DetectMultiScale (gray, scaleFactor = 1.02, minNeighbors = 3)for x, y, w, h in faces:  
            If the value is greater than 80, the value is incorrect
            _id, confidence = recognizer.predict(gray[y:y + h, x:x + w])  
            if confidence < 80:  
                cv.rectangle(image, (x, y), (x + w, y + h), color=(0, 255, 0), thickness=2)  
    except Exception as e:  
        pass  
  
  
def check_face():  
    cap = cv.VideoCapture('Face Recognition. Mp4')  
    while cap.isOpened():  
        flag, frame = cap.read()  
        if not flag:  
            break  
        face_detect_demo(frame)  
        cv.imshow('img', frame)  
        cv.waitKey(2)  
  
    cv.destroyAllWindows()  
  
if __name__ == '__main__':  
    Load the training data file
    recognizer = cv.face.LBPHFaceRecognizer_create()  
    recognizer.read('trains/trains.yml')  
  
    face_detector = cv.CascadeClassifier(os.path.join(cv.data.haarcascades, 'haarcascade_frontalface_default.xml'))  
    check_face()  

Copy the code

conclusion

Through the above step by step learning, you do not have a basic understanding of OpenCV face recognition? However, we also see that the main algorithm of face recognition is still based on Haar, and the accuracy is not particularly high, mainly because it will detect a lot of non-face data. LBPH asks us to tag someone’s face and tell us who it is, but it doesn’t improve the actual detection accuracy. Now machine learning is very popular, machine learning face recognition based on OpenCV is also very accurate, next time we will compare several machine learning face recognition libraries.