defload_image_file(file, mode='RGB'):"""
Loads an image file (.jpg, .png, etc) into a numpy array
:param file: image file name or file object to load
:param mode: format to convert the image to. Only 'RGB' (8-bit RGB, 3 channels) and 'L' (black and white) are supported.
:return: image contents as numpy array
"""
im = PIL.Image.open(file)if mode:
im = im.convert(mode)return np.array(im)
关键信息有:Loads an image file (.jpg, .png, etc) into a numpy array,PIL,np,
def face_landmarks(face_image, face_locations=None, model="large"):"""
Given an image, returns a dict of face feature locations (eyes, nose, etc)for each face in the image
:param face_image: image to search
:param face_locations: Optionally provide a list of face locations to check.:param model: Optional - which model to use."large"(default) or "small" which only returns 5 points but is faster.:return: A list of dicts of face feature locations (eyes, nose, etc)"""
landmarks =_raw_face_landmarks(face_image, face_locations, model)
landmarks_as_tuples =[[(p.x, p.y)for p in landmark.parts()]for landmark in landmarks]
# For a definition of each point index, see https://cdn-images-1.medium.com/max/1600/1*AbEg31EgkbXSQehuNJBlWg.png
if model =='large':return[{"chin": points[0:17],"left_eyebrow": points[17:22],"right_eyebrow": points[22:27],"nose_bridge": points[27:31],"nose_tip": points[31:36],"left_eye": points[36:42],"right_eye": points[42:48],"top_lip": points[48:55]+[points[64]]+[points[63]]+[points[62]]+[points[61]]+[points[60]],"bottom_lip": points[54:60]+[points[48]]+[points[60]]+[points[67]]+[points[66]]+[points[65]]+[points[64]]}for points in landmarks_as_tuples]
elif model =='small':return[{"nose_tip":[points[4]],"left_eye": points[2:4],"right_eye": points[0:2],}for points in landmarks_as_tuples]else:
raise ValueError("Invalid landmarks model type. Supported models are ['small', 'large'].")
defface_encodings(face_image, known_face_locations=None, num_jitters=1, model="small"):"""
Given an image, return the 128-dimension face encoding for each face in the image.
:param face_image: The image that contains one or more faces
:param known_face_locations: Optional - the bounding boxes of each face if you already know them.
:param num_jitters: How many times to re-sample the face when calculating encoding. Higher is more accurate, but slower (i.e. 100 is 100x slower)
:param model: Optional - which model to use. "large" (default) or "small" which only returns 5 points but is faster.
:return: A list of 128-dimensional face encodings (one for each face in the image)
"""
raw_landmarks = _raw_face_landmarks(face_image, known_face_locations, model)return[np.array(face_encoder.compute_face_descriptor(face_image, raw_landmark_set, num_jitters))for raw_landmark_set in raw_landmarks]
try:import face_recognition_models
except Exception:print("Please install `face_recognition_models` with this command before using `face_recognition`:\n")print("pip install git+https://github.com/ageitgey/face_recognition_models")quit()
提取完了就转成np数组返回;
2.2.face_recognition.compare_faces
defcompare_faces(known_face_encodings, face_encoding_to_check, tolerance=0.6):"""
Compare a list of face encodings against a candidate encoding to see if they match.
:param known_face_encodings: A list of known face encodings
:param face_encoding_to_check: A single face encoding to compare against the list
:param tolerance: How much distance between faces to consider it a match. Lower is more strict. 0.6 is typical best performance.
:return: A list of True/False values indicating which known_face_encodings match the face encoding to check
"""returnlist(face_distance(known_face_encodings, face_encoding_to_check)<= tolerance)
compare_faces调用的是face_distance
2.3.face_distance
defface_distance(face_encodings, face_to_compare):"""
Given a list of face encodings, compare them to a known face encoding and get a euclidean distance
for each comparison face. The distance tells you how similar the faces are.
:param faces: List of face encodings to compare
:param face_to_compare: A face encoding to compare against
:return: A numpy ndarray with the distance for each face in the same order as the 'faces' array
"""iflen(face_encodings)==0:return np.empty((0))return np.linalg.norm(face_encodings - face_to_compare, axis=1)