一、face_recognition库

1. load_image_file函数

face_recognition.load_image_file(file, mode=’RGB’)

  1. def load_image_file(file, mode='RGB'):
  2. """
  3. Loads an image file (.jpg, .png, etc) into a numpy array
  4. :param file: image file name or file object to load
  5. :param mode: format to convert the image to. Only 'RGB' (8-bit RGB, 3 channels) and 'L' (black and white) are supported.
  6. :return: image contents as numpy array
  7. """
  8. im = PIL.Image.open(file)
  9. if mode:
  10. im = im.convert(mode)
  11. return np.array(im)

功能描述:
将图像文件(.jpg,.png等)加载到numpy数组中。

参数:

  • file :图像文件名或要加载的文件对象
  • mode :将图像转换成的格式。仅支持“ RGB”(8位RGB,3通道)和“ L”(黑白)。

返回值:
图像内容为numpy数组

2. face_encodings函数

face_recognition.face_encodings(face_image, known_face_locations=None, num_jitters=1, model=’small’)

  1. def face_encodings(face_image, known_face_locations=None, num_jitters=1, model="small"):
  2. """
  3. Given an image, return the 128-dimension face encoding for each face in the image.
  4. :param face_image: The image that contains one or more faces
  5. :param known_face_locations: Optional - the bounding boxes of each face if you already know them.
  6. :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)
  7. :param model: Optional - which model to use. "large" (default) or "small" which only returns 5 points but is faster.
  8. :return: A list of 128-dimensional face encodings (one for each face in the image)
  9. """
  10. raw_landmarks = _raw_face_landmarks(face_image, known_face_locations, model)
  11. return [np.array(face_encoder.compute_face_descriptor(face_image, raw_landmark_set, num_jitters)) for raw_landmark_set in raw_landmarks]

功能描述:
给定图像,返回图像中每个面部的128维面部编码。即,返回每个人脸的128维特征值。

参数:

  • face_image :包含一个或多个面部的图像
  • known_face_locations :可选参数;每张脸的边界框(如果您已经知道的话)。
  • num_jitters :计算编码时对人脸重新采样的次数。越高越准确,但越慢(即,100x:慢100倍)
  • model :可选参数;使用哪种模型。“large”或“small”(默认)。small:仅返回5点,但速度更快。

返回值:
返回一个列表,列表中的元素是ndarray数组对象,对象里存储的是128维的人脸编码(图像中的每个人脸一个元素)

  1. load_image = face_recognition.load_image_file(path + image_name) # 加载图片
  2. info = face_recognition.face_encodings(load_image) # 获得128维特征值
  3. image_face_encoding = info[0]
  4. print(type(info)) # <class 'list'>
  5. print(len(info)) # 3
  6. print(type(image_face_encoding)) # <class 'numpy.ndarray'>
  7. print(image_face_encoding.shape) # (128,)
  8. output
  9. <class 'list'>
  10. 3
  11. <class 'numpy.ndarray'>
  12. (128,)

3. face_locations

face_recognition.face_locations(img, number_of_times_to_upsample=1, model=’hog’)

  1. def face_locations(img, number_of_times_to_upsample=1, model="hog"):
  2. """
  3. Returns an array of bounding boxes of human faces in a image
  4. :param img: An image (as a numpy array)
  5. :param number_of_times_to_upsample: How many times to upsample the image looking for faces. Higher numbers find smaller faces.
  6. :param model: Which face detection model to use. "hog" is less accurate but faster on CPUs. "cnn" is a more accurate
  7. deep-learning model which is GPU/CUDA accelerated (if available). The default is "hog".
  8. :return: A list of tuples of found face locations in css (top, right, bottom, left) order
  9. """
  10. if model == "cnn":
  11. return [_trim_css_to_bounds(_rect_to_css(face.rect), img.shape) for face in _raw_face_locations(img, number_of_times_to_upsample, "cnn")]
  12. else:
  13. return [_trim_css_to_bounds(_rect_to_css(face), img.shape) for face in _raw_face_locations(img, number_of_times_to_upsample, model)]

功能描述:
返回图像中人脸边界框的数组

参数:

  • img :一个图片(作为numpy数组)
  • number_of_times_to_upsample :对图像进行上采样的倍数,倍数越大,脸部越小。要寻找越小的人脸就要设置更高的数值。
  • model :采用哪种人脸检测的模式。“hog”(方向梯度直方图)准确度小一些但是在CPU上的运行速度更快;“cnn”是一个拥有更高准确度的深度学习模式,在GPU/CUDA上速度更快些。默认是“hog”模式。

返回值:
以css(上,右,下,左)顺序找到的脸部位置的元组的列表(是列表类型,列表的元素是元组类型)

  1. rgb_frame = np.array(Image.open('./images/image.png'))
  2. face_locations = face_recognition.face_locations(rgb_frame) # 获得所有人脸位置
  3. print(type(face_locations))
  4. print(len(face_locations))
  5. print(face_locations)
  6. output
  7. <class 'list'>
  8. 3
  9. [(235, 139, 343, 32), (150, 373, 305, 218), (98, 562, 253, 408)]

4. batch_face_locations函数

face_recognition.batch_face_locations(images, number_of_times_to_upsample=1, batch_size=128)

  1. def batch_face_locations(images, number_of_times_to_upsample=1, batch_size=128):
  2. """
  3. Returns an 2d array of bounding boxes of human faces in a image using the cnn face detector
  4. If you are using a GPU, this can give you much faster results since the GPU
  5. can process batches of images at once. If you aren't using a GPU, you don't need this function.
  6. :param images: A list of images (each as a numpy array)
  7. :param number_of_times_to_upsample: How many times to upsample the image looking for faces. Higher numbers find smaller faces.
  8. :param batch_size: How many images to include in each GPU processing batch.
  9. :return: A list of tuples of found face locations in css (top, right, bottom, left) order
  10. """
  11. def convert_cnn_detections_to_css(detections):
  12. return [_trim_css_to_bounds(_rect_to_css(face.rect), images[0].shape) for face in detections]
  13. raw_detections_batched = _raw_face_locations_batched(images, number_of_times_to_upsample, batch_size)
  14. return list(map(convert_cnn_detections_to_css, raw_detections_batched))

功能描述:
(使用GPU时使用此函数)这个函数使用cnn人脸检测,返回一个描述图片中人脸的边界线的2维数组。

参数:

  • img :图片的list(每个都是一个numpy数组)。
  • number_of_times_to_upsample :对图像进行上采样的倍数,倍数越大,脸部越小。
  • batch_size :每个GPU处理批次中包含多少个图像。

返回值:
css顺序(上、右、下、左)的一个人脸位置tuples list。

5. compare_faces函数

face_recognition.compare_faces(known_face_encodings, face_encoding_to_check, tolerance=0.6)

  1. def compare_faces(known_face_encodings, face_encoding_to_check, tolerance=0.6):
  2. """
  3. Compare a list of face encodings against a candidate encoding to see if they match.
  4. :param known_face_encodings: A list of known face encodings
  5. :param face_encoding_to_check: A single face encoding to compare against the list
  6. :param tolerance: How much distance between faces to consider it a match. Lower is more strict. 0.6 is typical best performance.
  7. :return: A list of True/False values indicating which known_face_encodings match the face encoding to check
  8. """
  9. return list(face_distance(known_face_encodings, face_encoding_to_check) <= tolerance)

功能描述:
将已知的一些人脸编码与候选的人脸编码进行比对,看他们是否匹配。

参数:

  • known_face_encodings :已知的人脸编码列表
  • face_encoding_to_check :一个待比对的人脸编码
  • tolerance :认为相匹配的两张脸之间的距离。越低越严格。0.6是典型的最佳性能。
  • 面孔之间的距离有多大才能将其视为匹配项。越低越严格。 0.6是典型的最佳性能。

返回值:
一个标志已知的人脸编码与待匹配人脸编码是否匹配的True/False值的list。

6. face_distance

face_recognition.face_distance(face_encodings, face_to_compare)

  1. def face_distance(face_encodings, face_to_compare):
  2. """
  3. Given a list of face encodings, compare them to a known face encoding and get a euclidean distance
  4. for each comparison face. The distance tells you how similar the faces are.
  5. :param faces: List of face encodings to compare
  6. :param face_to_compare: A face encoding to compare against
  7. :return: A numpy ndarray with the distance for each face in the same order as the 'faces' array
  8. """
  9. if len(face_encodings) == 0:
  10. return np.empty((0))
  11. return np.linalg.norm(face_encodings - face_to_compare, axis=1)

功能描述:
给定一组人脸编码,将其与已知的人脸编码进行比较,并得到每个比较人脸的欧几里得距离,以此距离来表示两个人脸的相似度。

参数:

  • face_encodings :用来比较的人脸编码list。
  • face_to_compare :待比较的一个人脸的编码。

返回值:
一个关于距离的numpy ndarray(其顺序与face_encodings的顺序相同)。

7. face_landmarks

face_recognition.face_landmarks(face_image, face_locations=None, model=’large’)

  1. def face_landmarks(face_image, face_locations=None, model="large"):
  2. """
  3. Given an image, returns a dict of face feature locations (eyes, nose, etc) for each face in the image
  4. :param face_image: image to search
  5. :param face_locations: Optionally provide a list of face locations to check.
  6. :param model: Optional - which model to use. "large" (default) or "small" which only returns 5 points but is faster.
  7. :return: A list of dicts of face feature locations (eyes, nose, etc)
  8. """
  9. landmarks = _raw_face_landmarks(face_image, face_locations, model)
  10. landmarks_as_tuples = [[(p.x, p.y) for p in landmark.parts()] for landmark in landmarks]
  11. # For a definition of each point index, see https://cdn-images-1.medium.com/max/1600/1*AbEg31EgkbXSQehuNJBlWg.png
  12. if model == 'large':
  13. return [{
  14. "chin": points[0:17],
  15. "left_eyebrow": points[17:22],
  16. "right_eyebrow": points[22:27],
  17. "nose_bridge": points[27:31],
  18. "nose_tip": points[31:36],
  19. "left_eye": points[36:42],
  20. "right_eye": points[42:48],
  21. "top_lip": points[48:55] + [points[64]] + [points[63]] + [points[62]] + [points[61]] + [points[60]],
  22. "bottom_lip": points[54:60] + [points[48]] + [points[60]] + [points[67]] + [points[66]] + [points[65]] + [points[64]]
  23. } for points in landmarks_as_tuples]
  24. elif model == 'small':
  25. return [{
  26. "nose_tip": [points[4]],
  27. "left_eye": points[2:4],
  28. "right_eye": points[0:2],
  29. } for points in landmarks_as_tuples]
  30. else:
  31. raise ValueError("Invalid landmarks model type. Supported models are ['small', 'large'].")

功能描述:给定一张图,对图像中的每张人脸返回人脸特征(眼睛、鼻子等)的位置dict。

参数:

  • face_image :待搜索的图像。
  • face_locations :可选参数;提供一个人脸位置的list供核查
  • model :可选参数;使用哪种模式。“large”是默认的,返回68个特征点,“small”返回5个特征点但是速度更快。

返回值:
人脸特征(眼睛、鼻子等)的位置dicts list。