Python 30 lines of code to achieve simple face recognition example code
- 2021-10-15 11:07:47
- OfStack
1. Library introduction
opencv, face_recognition, numpy, and dlib
Note:
The installation speed of opencv may be too slow, and the domestic image source needs to be replaced. Reference: https://www.ofstack.com/article/208359. htm
With Python 3.7, 64-bit version dlib whl Download path: dlib-19 _ jb51. rar
2. Library installation
pip install opencv-python
pip install face_recognition
pip install numpy
The dlib library needs to be installed under the whl file path
pip install dlib-19.17.99-cp37-cp37m-win_amd64.whl
3. A brief introduction to the face_recognition library
The load_image_file method of face_recognition loads the picture and returns 1 data of type ndarray
face_path = "C://Users//25103//Desktop//Python Face recognition //face// Mr. Tsui .jpg"
image = face_recognition.load_image_file(face_path)
The face_encoding method of face_recognition can extract face features from the returned ndarray type data, and can extract multiple features at the same time, and the return value is list type
face_encoding = face_recognition.face_encodings(image)[0]
The face_location method of face_recognition can obtain the positions of all faces in the picture, and its return value is 1 list
face_locations = face_recognition.face_locations(rgb_frame)
4. Code implementation and annotation explanation
# coding = utf-8
import dlib
import cv2
import face_recognition
import os
# Create a video object
video_capture = cv2.VideoCapture(0)
# Load the face image to be recognized (this image requires only 1 Face)
# face_recognition Adj. load_image_file Method loads the picture and returns the 1 A ndarray Data of type
# ndarray The type is NumPy The array type of, in which the element types can be 1 To or not 1 To
face_path = "C://Users//25103//Desktop//Python Face recognition //face// Mr. Tsui .jpg"
image = face_recognition.load_image_file(face_path)
# face_recognition Adj. face_encoding Method , Can be returned from the ndarray Face features can be extracted from type data, and multiple features can be extracted at the same time, and the return value is list type
# Because there is only one in the photo 1 Personal face, so we take the first of the list 1 Values
face_encoding = face_recognition.face_encodings(image)[0]
while True:
# Read from a video object 1 Frame photo
ret,frame = video_capture.read()
# Shrink the photo to speed up the processing , Here, it is reduced to the original 1/4
# frame = cv2.rectangle(frame,(0,0),fx=0.25,fy=0.25)
# Because cv2 Using the BGR Color, which our group will convert into RGB Process
rgb_frame = frame[:,:,::-1] # List transposition operation
# face_recognition Adj. face_location Method can get the position of all faces in the picture, and its return value is 1 List
face_locations = face_recognition.face_locations(rgb_frame)
print(" I found it from the video {} Zhang Face ".format(len(face_locations)))
# Get the features of all faces in the video
face_encodings = face_recognition.face_encodings(rgb_frame,face_locations)
for face in face_encodings:
# Compare two eigenvalues- encoding1 And encoding2 Match returns True Otherwise, return False . tolerance The lower, as the name implies, the lower the fault tolerance rate , The return value is of list type
match = face_recognition.compare_faces([face_encoding],face,tolerance=0.4)
name = " People I don't know "
if match[0]:
# face Is the name of the picture
name = os.path.basename(face_path[0:-4])
print(" Found it {}".format(name))