Hand in hand to teach you face recognition, hand on the line

application area

  • Financial sector. At present, face recognition is the most widely used in the financial field. At present, the domestic financial field has strict regulatory requirements. Financial related products need real name authentication and have high security requirements. Living body recognition, bank card ocr recognition, ID card ocr recognition, ID card comparison and so on have become an indispensable link in major mobile banking, financial app and insurance app
  • Security. At present, a large number of enterprises, houses, communities, schools and other security management are becoming more and more popular. Face access control system has become a very popular way of security.
  • Traffic area. Railway stations in many cities have installed face recognition access equipment to compare and check people's cards. Subway stations in some cities can also access subway stations through face recognition.
  • Pan entertainment. Now the popular beauty cameras, webcast and short videos in the market are all based on face recognition to deal with the beauty and special effects of faces.
  • Public security, judicial field. When chasing fugitives, the public security system will also use the face recognition system to locate fugitives. At present, the prison system will also alarm and protect prisoners through the face recognition system
  • Self service equipment. Such as bank ATM, unmanned supermarket, etc.
  • Attendance and meeting affairs. Such as work attendance, meeting attendance, face wall, etc. At present, the giants in the face recognition market mainly include shopping malls and small companies invested by giant companies in many fields.

Step 1: import module

dlib module installation is actually complicated. Be careful and patient. Please refer to: dlib installation , if not, look at other tutorials.

import sys
import cv2
import face_recognition #dlib face recognition database

Step 2: load the picture and digitize it

The test picture is my idol:

face_img=face_recognition.load_image_file('1.png')
print(face_img)

Print results:

Output as a three-dimensional image matrix and convert the image into a matrix.

Step 3: get the face data in the picture

Extract face feature code and obtain the position of facial features:

face_encodings=face_recognition.face_encodings(face_img)#Feature extraction and vectorization are carried out to obtain the face coding
face_locations=face_recognition.face_locations(face_img)#Corresponding position of facial features
print(face_encodings)

There are several arrays of faces in the picture:

Step 4: number calculation

Here we only judge whether two people are one person. If there are more than two, we will quit

n=len(face_encodings)
print(n)
#Here we only judge whether two people are one person. If there are more than two, we will quit
if n>2:
    print('More than two people')
    sys.exit()

Printing can be divided into two people:

Step 5: face comparison

#Get the data of two people
face1=face_encodings[0]
face2=face_encodings[1]

result=face_recognition.compare_faces([face1],face2,tolerance=0.6)#Face comparison, if the error is no more than 0.6, the default value is 0.6
print(result)

return:

Judge that it is not the same person.
Modify it slightly to make the expression clearer:

if result==[True]:
    name='same'
    print('Two people are the same person')
else:
    print('The two are not the same person')
    name='different'

return:

Step 6: frame the face and write words

Obtain two face coordinates:

for i in range(len(face_encodings)):
    face_encoding=face_encodings[(i-1)] #Reverse order acquisition
    face_location = face_locations[(i - 1)]
    print(face_location)#Get face location

return:

The four values of Yuanzu represent the coordinates of the four points of the face rectangle.

After obtaining the coordinates, start to draw the frame and write the text:

top,right,bottom,left=face_location#Determine the coordinates
    #Picture frame
    cv2.rectangle(face_img,(left,top),(right,bottom),(255,0,0))#The transmission parameters are: picture, coordinate, RGB color and frame thickness
    #Write it
    cv2.putText(face_img,name,(left-10,top-10),cv2.FONT_HERSHEY_DUPLEX,0.8,(255,255,0),2)#The transmission parameters are: picture, text, coordinates, font, font size, color and thickness

Step 7: display the processed image

face_img_rgb=cv2.cvtColor(face_img,cv2.COLOR_BGR2RGB)#Make sure the colors are not confused
#Display image
cv2.imshow('compare',face_img_rgb)
#Set wait to close
cv2.waitKey(0)

effect:

You only need to type the code step by step to complete the code. Of course, for the convenience of direct cv, the code is shown as follows:

# coding=gbk
"""
Author: Chuan Chuan
 Official account: big data
@time  : 2022/2/5 14:36
 Group: 428335755
"""
import sys
import cv2
import face_recognition #dlib face recognition database


face_img=face_recognition.load_image_file('1.png')
# print(face_img)

face_encodings=face_recognition.face_encodings(face_img)#Feature extraction and vectorization are carried out to obtain the face coding
face_locations=face_recognition.face_locations(face_img)#Corresponding position of facial features
# print(face_encodings)

n=len(face_encodings)
print(n)
#Here we only judge whether two people are one person. If there are more than two, we will quit
if n>2:
    print('More than two people')
    sys.exit()

#Get the data of two people
face1=face_encodings[0]
face2=face_encodings[1]

result=face_recognition.compare_faces([face1],face2,tolerance=0.6)#Face comparison, if the error is no more than 0.6, the default value is 0.6
# print(result)
if result==[True]:
    name='same'
    print('Two people are the same person')
else:
    print('The two are not the same person')
    name='different'


for i in range(len(face_encodings)):
    face_encoding=face_encodings[(i-1)] #Reverse order acquisition
    face_location = face_locations[(i - 1)]
    # print(face_location)#Get face location

    top,right,bottom,left=face_location#Determine the coordinates
    #Picture frame
    cv2.rectangle(face_img,(left,top),(right,bottom),(255,0,0))#The transmission parameters are: picture, coordinate, RGB color and frame thickness
    #Write it
    cv2.putText(face_img,name,(left-10,top-10),cv2.FONT_HERSHEY_DUPLEX,0.8,(255,255,0),2)#The transmission parameters are: picture, text, coordinates, font, font size, color and thickness

face_img_rgb=cv2.cvtColor(face_img,cv2.COLOR_BGR2RGB)#Make sure the colors are not confused
#Display image
cv2.imshow('compare',face_img_rgb)
#Set wait to close
cv2.waitKey(0)

Marking two faces and writing them as different means different. Of course, in order to give you a brief introduction to the implementation of face recognition, this article has not done too many complex implementations. Recently, I have also done some complex functional implementations of face recognition. If you are interested, you can talk together.

Higher order face recognition project

Graduation project: Face recognition

Keywords: AI Computer Vision Deep Learning

Added by lxndr on Sat, 05 Feb 2022 10:29:07 +0200