人脸识别库 face_recognition
阅读原文时间:2023年07月10日阅读:1

face_recognition

默认环境:anaconda的python3.7版本,win10环境

第一步:安装dlib

  1. 从网络上下载:

    http://dlib.net/ 或者 https://github.com/davisking/dlib

  2. 下载完成后解压

  3. 编译:打开终端,是使用anaconda的python环境定位到dlib文件夹,使用 pyhton setup.py install 安装文件。

  4. 在安装过程遇到的问题:

  5. 从新使用 pyhton setup.py install 安装文件。

第二步:安装face_recognition

使用 pip install face_recognition 安装即可。

  1. 导入face_recognition

    import face_recognition

  2. 加载图像

图像加载到一个 numpy 数组中

image = face_recognition.load_image_file("your_file.jpg")
  1. 对图片进行操作

batch_face_locations

使用 cnn 人脸检测器返回图像中人脸边界框的二维数组 ,使用GPU批量处理图像

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

参数:

  • images:图像列表
  • number_of_times_to_upsample: 在图像上寻找人脸的次数 , 数字越大,查找精细
  • batch_size: 每个 GPU 处理批次中包含多少图像

返回:

​ 以 css(上、右、下、左)顺序找到的人脸位置的元组列表

compare_faces

面部编码列表与候选编码进行比较,看看它们是否匹配。

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

参数:

  • known_face_encodings: 已知人脸编码列表
  • face_encoding_to_check: 要与列表进行比较的单个人脸编码
  • tolerance: 人脸之间的距离多少才算是匹配。越低越严格。0.6 是典型的最佳性能

返回:

​ True/False 值列表

face_distance

给定一个人脸编码列表,将它们与已知的人脸编码进行比较,并获得每个比较人脸的欧几里德距离。距离告诉您面孔的相似程度。

face_recognition.api.face_distance( face_encodings , face_to_compare )

参数:

​ face_encodings: 要比较的人脸编码列表

​ face_to_compare: 要比较的人脸编码

返回:

​ 一个 numpy ndarray,每个面的距离与 'faces' 数组的顺序相同

face_encodings

给定一个图像,返回图像中每个人脸的 128 维人脸编码。

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

参数:

​ face_image: 包含一张或多张人脸的图像

​ known_face_locations: 可选 - 已经知道每个人脸的边界框。

​ num_jitters: 计算编码时对人脸重新采样的次数。更高更准确,但更慢(即 100 慢 100 倍)

​ model: 可选 - 要使用的模型。“ large ”或“ small ”(默认)仅返回 5 个点,但速度更快。

返回:

​ 128 维人脸编码列表(图像中的每个人脸编码一个)

face_landmarks

给定一个图像,返回图像中每个人脸特征位置(眼睛、鼻子等)的字典

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

参数:

​ face_image: 要搜索的图像

​ face_locations: 可选择提供要检查的人脸位置列表。

​ model: 可选 - 要使用的模型。“large ”(默认)或“small”仅返回 5 个点但速度更快。

返回:

​ 面部特征位置(眼睛、鼻子等)的字典列表

face_locations

返回图像中人脸的边界框数组

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

参数:

​ img: 图像(作为 numpy 数组)

​ number_of_times_to_upsample:在图像上寻找人脸的次数。较高的数字会发现较小的面孔。

​ model: 要使用的人脸检测模型。“hog”在 CPU 上不太准确但速度更快。“cnn”是一种更准确的深度学习模型,它经过 GPU/CUDA 加速(如果可用)。 默认为“hog”

返回:

​ 以 css(上、右、下、左)顺序找到的人脸位置的元组列表

load_image_file

将图像文件(.jpg、.png 等)加载到 numpy 数组中

face_recognition.api.load_image_file(file,model='RGB')

参数:

​ file: 要加载的图像文件名或文件对象

​ model: 将图像转换为的格式。仅支持“RGB”(8 位 RGB,3 通道)和“L”(黑白)。

返回:

​ 图像内容作为 numpy 数组

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器