Python learning | 2021-09-17 face recognition program

Face recognition process: face detection + face alignment + feature point extraction + face matching  

1, Face detection

1. OpenCV face detection

OpenCV comes with a trained face Haar feature classifier, which is stored in the D: \ Python \ lib \ site packages \ CV2 \ data \ folder. Several. xml files are as follows:

Face detector (default): haarcascade_frontalface_default.xml

Face detector (fast Harr): haarcascade_frontalface_alt2.xml

Face detector (side view): haarcascade_profileface.xml

Eye detector (left eye): haarcascade_lefteye_2splits.xml

Eye detector (right eye): haarcascade_righteye_2splits.xml

Body detector: haarcascade_fullbody.xml

Smile detector: haarcascade_smile.xml

Operation steps:

  1. CV2. Cascade classifier() loads Haar feature detection classifier
  2. cv2.imread() loads the image (the reading result is in BGR format, and the color is different from RGB format), and converts it into a gray image
  3. Recognize the faces in the image and return the rectangular box vector group of all faces
  4. Draw a rectangular box in the image to display the recognition results

Full code:

import cv2
import matplotlib.pyplot as plt

def detect(filename):
    face_cascade=cv2.CascadeClassifier('D:\Python\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml')    #Define cascading classifiers
    img=cv2.imread(filename)
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)    #Convert BGR format to grayscale image
    faces=face_cascade.detectMultiScale(gray, 1.3, 5)    #Recognize the faces in the image and return the rectangular box vector group of all faces
    num=0
    for (x, y, w, h) in faces:
        img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)    #Draw a rectangular box in the image
        num+=1
    print('Number of faces:',num)
    plt.imshow(img) 
    plt.axis('off')  
    plt.show()
    
detect('calorie.jpg')

Operation results:  

(it can be seen that there are 11 people in the picture, only 10 people are identified, with some deviation)

Parameter meaning:

Learning method - check the Python Module Docs document, where '[' indicates optional

 cv2.cvtColor(src, code[, dst[, dstCn]]) -> dst 

src          Pictures to convert

code          To what format

cv2.COLOR_BGR2GRAY          Convert BGR format to grayscale image

detectMultiScale(image[, scaleFactor[, minNeighbors[, flags[, minSize[, maxSize]]]]]) -> objects

image          The detected image needs to be converted into gray image

scaleFactor          In order to detect targets of different sizes, the length and width of the image are gradually reduced according to a certain proportion (1.1 by default) through this parameter (the larger the parameter is set, the faster the calculation speed is, but the face of a certain size may be missed)

minNeighbors           Only when the number of times the detection target is recognized is greater than or equal to this value (3 by default), it is considered to be the correct result

cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) -> img

pt1        Vertex of the rectangle

pt2        Vertex of the rectangle opposite to pt1 

color        Rectangle color 

thickness        Thickness of lines that make up the rectangle

  2. Face recognition face detection

  ① HOG algorithm

"HOG" is less accurate but faster on CPUs.The default is "HOG".

Operation steps:

  1. face_recognition.load_image_file()   Load image
  2. Call face_recognition.face_locations(image) to locate the face location information recognized in all images (the return value is in list form, and each face is a tuple storage, including [top, right, bottom, left])
  3. Loop all the faces found and print the position information of each face
  4. Draw a rectangular box in the image to display the recognition results

Full code:

import face_recognition
import cv2
import matplotlib.pyplot as plt

image = face_recognition.load_image_file("calorie.JPG")
face_locations=face_recognition.face_locations(image)
face_num=len(face_locations)
print('Number of faces:',face_num)       
org = cv2.imread("calorie.JPG")

for i in range(0,face_num):
    top = face_locations[i][0]
    right = face_locations[i][1]
    bottom = face_locations[i][2]
    left = face_locations[i][3]
    start = (left, top)
    end = (right, bottom)
    color = (255,0,0)
    thickness = 2
    img=cv2.rectangle(org, start, end, color, thickness)
    plt.imshow(img)
    plt.axis('off')  
plt.show()

Operation results:   

 

  ② CNN algorithm (convolutional neural network)

"CNN" is a more accurate deep-learning model which is GPU/CUDA accelerated (if available). 

Full code:

import face_recognition
import cv2
import matplotlib.pyplot as plt

image = face_recognition.load_image_file("calorie.JPG")
face_locations_useCNN = face_recognition.face_locations(image,model='cnn')    #model defaults to 'hog'
face_num1=len(face_locations_useCNN)
print('Number of faces:',face_num1)     
org = cv2.imread("calorie.JPG")

for i in range(0,face_num1):
    top = face_locations_useCNN[i][0]
    right = face_locations_useCNN[i][1]
    bottom = face_locations_useCNN[i][2]
    left = face_locations_useCNN[i][3]
    start = (left, top)
    end = (right, bottom)
    color = (0,255,255)
    thickness = 2
    img=cv2.rectangle(org, start, end, color, thickness)  
    plt.imshow(img)
    plt.axis('off')  
plt.show()

Operation results:    

③ Problem record and summary

dlib installation failed:

When installing Dlib library, PIP install Dlib and pip install dlib-19.17.99-cp37-cp37m-win_amd64.whl failed. Some error messages are as follows:

You must use Visual Studio to build a python extension on windows. If you are getting this error it means you have not installed Visual C++. Note that there are many flavors of Visual Studio, like Visual Studio for C#  development. You need to install Visual Studio for C++.

Finally, the problem is solved by installing cmake, boost and Visual Studio 2019

Slow download speed:

1. Using whl files: through web sites https://pypi.org/project , download the WHL third-party library installation file (consistent with the Python version) and place it in the Python installation directory; in the current directory, run the command pip install xxx.whl

2. Temporarily use the image source: run the command PIP install - I https://pypi.tuna.tsinghua.edu.cn/simple xxx

3. Permanently configure the image source: create a PIP directory in the Users directory, and then create a new file pip.ini; open the pip.ini file, copy and paste the following contents and save

[global]
 index-url = https://pypi.tuna.tsinghua.edu.cn/simple[global]
 index-url = https://pypi.tuna.tsinghua.edu.cn/simple

2, Face feature point calibration

Operation steps:

  1. Detector for obtaining face frame position and face key point detector
  2. Read the image and grayize the image
  3. The detector is used for face detection to obtain the position information of face frame
  4. Traverse the detected box to find 68 calibration points of the face; traverse all points, print out their coordinates and circle them

Full code:

import cv2
import dlib
import matplotlib.pyplot as plt

detector = dlib.get_frontal_face_detector()    #Get face classifier
predictor = dlib.shape_predictor(r"D:\Python\Lib\site-packages\face_recognition_models\models\shape_predictor_68_face_landmarks.dat")    #Get face detector

path = "Martina.JPEG"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

dets = detector(gray, 1)    #The detector is used for face detection, and detects is the returned result, (gray, 1) = (gray image, 1 channel)
for face in dets:
    shape = predictor(img, face)    #Find 68 calibration points of human face
    for pt in shape.parts():
        pt_pos = (pt.x, pt.y)
        img=cv2.circle(img, pt_pos, 2, (255, 0, 0), 1)        #Traverse all points, print out their coordinates and circle them

plt.imshow(img)
plt.axis('off')  
plt.show()

Operation results:

 

Tags: Python Machine Learning Deep Learning

Posted on Sat, 18 Sep 2021 20:38:46 -0400 by Fataqui