模块 face_recognition 人脸识别
face_recognition 人脸识别
| api | 说明 |
|---|---|
| 1 load_image_file | 将img文件加载到numpy 数组中 |
| 2 face_locations | 查找图像中所有面部和所有面部特征的位置 |
| 3 batch_face_locations | 批次人脸定位函数(GPU) |
| 4 face_landmarks | 人脸特征提取函数 |
| 5 face_encodings | 图像编码转为特征向量 |
| 6 compare_faces | 特征向量比对 |
| 7 face_distance | 计算特征向量差值 |
图像载入函数——load_image_file
load_image_file(file, mode='RGB')
加载一个图像文件到一个numpy array类型的对象上。
参数:
file:待加载的图像文件名字
mode:转换图像的格式。只支持“RGB”(8位RGB, 3通道)和“L”(黑白)
返回值:
一个包含图像数据的numpy array类型的对象
人脸编码函数——face_encodings
face_encodings(face_image, known_face_locations=None, num_jitters=1)
给定一个图像,返回图像中每个人脸的128脸部编码(特征向量)。
参数:
face_image:输入的人脸图像
known_face_locations:可选参数,如果你知道每个人脸所在的边界框
num_jitters=1:在计算编码时要重新采样的次数。越高越准确,但速度越慢(100就会慢100倍)
返回值:
一个128维的脸部编码列表
人脸匹配函数——compare_faces
compare_faces(known_face_encodings, face_encoding_to_check, tolerance=0.6)
比较脸部编码列表和候选编码,看看它们是否匹配,设置一个阈值,若两张人脸特征向量的距离,在阈值范围之内,则认为其 是同一个人
参数:
known_face_encodings:已知的人脸编码列表
face_encoding_to_check:待进行对比的单张人脸编码数据
tolerance=0.6:两张脸之间有多少距离才算匹配。该值越小对比越严格,0.6是典型的最佳值
返回值:
一个 True或者False值的列表,该表指示了known_face_encodings列表的每个成员的匹配结果
人脸定位函数——face_locations
face_locations(face_image,number_of_times_to_upsample=1,model="hog")
利用CNN深度学习模型或方向梯度直方图(Histogram of Oriented Gradient, HOG)进行人脸提取。返回值是一个数组(top, right, bottom, left)表示人脸所在边框的四条边的位置。
参数:
face_image:输入的人脸图像
number_of_times_to_upsample=1:从图片的样本中查找多少次人脸,该参数的值越高的话越能发现更小的人脸
model="hog":使用哪种人脸检测模型。“hog” 准确率不高,但是在CPU上运行更快,“cnn” 更准确更深度(且GPU/CUDA加速,如果有GPU支持的话),默认是“hog”
返回值:
一个元组列表,列表中的每个元组包含人脸的四边位置(top, right, bottom, left)
批次人脸定位函数(GPU)——batch_face_locations
batch_face_locations(face_images,number_of_times_to_upsample=1,batch_size=128)
使用CNN人脸检测器返回一个包含人脸特征的二维数组,如果使用了GPU,这个函数能够更快速的返回结果;如果不使用GPU的话,该函数就没必要使用
参数:
face_images:输入多张人脸图像组成的list
number_of_times_to_upsample=1:从图片的样本中查找多少次人脸,该参数的值越高的话越能发现更小的人脸
batch_size=128:每个GPU一次批处理多少个image
返回值:
一个元组列表,列表中的每个元组包含人脸的四边位置(top, right, bottom, left)
人脸特征提取函数——face_landmarks
face_landmarks(face_image,face_locations=None,model="large")
给定一个图像,提取图像中每个人脸的脸部特征位置
参数:
face_image:输入的人脸图片
face_locations=None:可选参数,默认值为None,代表默认解码图片中的每一个人脸。若输入face_locations()[i]可指定人脸进行解码
model="large":输出的特征模型,默认为“large”,可选“small”。当选择为"small"时,只提取左眼、右眼、鼻尖这三种脸部特征。
返回值:
返回值类型为:List[Dict[str,List[Tuple[Any,Any]]]],是由各个脸部特征关键点位置组成的字典记录列表,一个Dict对象对应图片中的一个人脸,其key为某个脸部特征(如输出中的nose_bridge、left_eye等),value是由该脸部特征各个关键点位置组成的List,关键点位置是一个Tuple(如上输出中,nose_bridge对应的关键点位置组成的列表为[(881L, 128L), (880L, 141L), (880L, 154L), (879L, 167L)] )
计算特征相识度差值——face_distance
例子1-人脸识别和特征匹配
import face_recognition
# 加载2张已知面孔的图片
known_obama_image = face_recognition.load_image_file("obama.jpg")
known_biden_image = face_recognition.load_image_file("biden.jpg")
# 计算图片对应的编码
obama_face_encoding = face_recognition.face_encodings(known_obama_image)[0]
biden_face_encoding = face_recognition.face_encodings(known_biden_image)[0]
known_encodings = [
obama_face_encoding,
biden_face_encoding
]
# 加载一张未知面孔的图片
image_to_test = face_recognition.load_image_file("obama2.jpg")
# 计算图片对应的编码
image_to_test_encoding = face_recognition.face_encodings(image_to_test)[0]
# 计算未知图片与已知的2个面孔的距离
face_distances = face_recognition.face_distance(known_encodings, image_to_test_encoding)
for i, face_distance in enumerate(face_distances):
print("The test image has a distance of {:.2} from known image #{}".format(face_distance, i))
print("- With a normal cutoff of 0.6, would the test image match the known image? {}".format(face_distance < 0.6))
print("- With a very strict cutoff of 0.5, would the test image match the known image? {}".format(face_distance < 0.5))
print()
结果
The test image has a distance of 0.35 from known image #0 (与#0面孔差异为0.35,在相似度阈值分别为 0.6和0.5的情形下,都可以认为与#0是同一人)
- With a normal cutoff of 0.6, would the test image match the known image? True
- With a very strict cutoff of 0.5, would the test image match the known image? True
The test image has a distance of 0.82 from known image #1(与#1面孔差异为0.82,在相似度阈值分别为 0.6和0.5的情形下,都不认为与#1是同一人)
- With a normal cutoff of 0.6, would the test image match the known image? False
- With a very strict cutoff of 0.5, would the test image match the known image? False
例子2-特征点检测和美颜
import face_recognition
from PIL import Image, ImageDraw
# Load the jpg file into a numpy array
image = face_recognition.load_image_file("tt3.jpg")
# Find all facial features in all the faces in the image
face_landmarks_list = face_recognition.face_landmarks(image)
for face_landmarks in face_landmarks_list:
# Create a PIL imageDraw object so we can draw on the picture
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image, 'RGBA')
# 画个浓眉
d.polygon(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128))
d.polygon(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 128))
d.line(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 150), width=5)
d.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=5)
# 涂个性感的嘴唇
d.polygon(face_landmarks['top_lip'], fill=(150, 0, 0, 128))
d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128))
d.line(face_landmarks['top_lip'], fill=(150, 0, 0, 64), width=8)
d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=8)
# 闪亮的大眼睛
d.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30))
d.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30))
# 画眼线
d.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=6)
d.line(face_landmarks['right_eye'] + [face_landmarks['right_eye'][0]], fill=(0, 0, 0, 110), width=6)
pil_image.show()
模块 face_recognition 人脸识别的更多相关文章
- python face_recognition模块实现人脸识别
import face_recognition #人脸识别库 pip cmake dlib import cv2 #读取图像 face_image1 = face_recognition.load_i ...
- Python 使用 face_recognition 人脸识别
Python 使用 face_recognition 人脸识别 官方说明:https://face-recognition.readthedocs.io/en/latest/readme.html 人 ...
- face_recognition人脸识别框架
一.环境搭建 1.系统环境 Ubuntu 17.04 Python 2.7.14 pycharm 开发工具 2.开发环境,安装各种系统包 人脸检测基于dlib,dlib依赖Boost和cmake $ ...
- face_recognition人脸识别
对亚洲人识别准确度有点差,具体安装移步:https://www.cnblogs.com/ckAng/p/10981025.html 更多操作移步:https://github.com/ageitgey ...
- 人脸识别课件需要安装的python模块
Python3.6安装face_recognition人脸识别库 https://www.jianshu.com/p/8296f2aac1aa
- Python 人工智能之人脸识别 face_recognition 模块安装
Python人工智能之人脸识别face_recognition安装 face_recognition 模块使用系统环境搭建 系统环境 Ubuntu / deepin操作系统 Python 3.6 py ...
- 手把手教你用1行代码实现人脸识别 --Python Face_recognition
环境要求: Ubuntu17.10 Python 2.7.14 环境搭建: 1. 安装 Ubuntu17.10 > 安装步骤在这里 2. 安装 Python2.7.14 (Ubuntu17.10 ...
- Github开源人脸识别项目face_recognition
Github开源人脸识别项目face_recognition 原文:https://www.jianshu.com/p/0b37452be63e 译者注: 本项目face_recognition是一个 ...
- 微信小程序 人脸识别登陆模块
微信小程序---人脸识别登陆的实现 关键词:微信小程序 人脸识别 百度云接口 前言 这是一篇关于一个原创微信小程序开发过程的原创文章.涉及到的核心技术是微信小程序开发方法和百度云人脸识别接口.小程序的 ...
随机推荐
- 【WPF学习】第五十六章 基于帧的动画
除基于属性的动画系统外,WPF提供了一种创建基于帧的动画的方法,这种方法只使用代码.需要做的全部工作是响应静态的CompositionTarge.Rendering事件,触发该事件是为了给每帧获取内容 ...
- [译]HTML&CSS Lesson7: 设置背景和渐变色
背景对网站的设计有重大的影响.它有利于建立网站的整体感觉,设置分组,分配优先级,对网站的可用性也有相当大的影响. 在CSS中,元素的背景可以是一个纯色,一张图,一个渐变色或者它们的组合.在我们决定如何 ...
- Ubuntu 系统下如何安装pip3工具
一.[导读]Ubuntu 系统内置了 Python2 和 Python3 两个版本的开发环境,却没有内置相应的 pip3 管理工具,本文将介绍如何在Ubuntu下如何快速安装 pip3 工具,并升级到 ...
- 浅析SIEM、态势感知平台、安全运营中心
近年来SIEM.态势感知平台.安全运营中心等概念炒的火热,有的人认为这都是安全管理产品,这些产品就是一回事,有人认为还是有所区分.那么到底什么是SIEM.什么是态势感知平台.什么是安全运营中心,他们之 ...
- Markdown For EditPlus插件发布(基于EditPlus快速编辑Markdonw文件,写作爱好的福音来啦)
详细介绍: Markdown For EditPlus插件使用说明 开发缘由 特点好处: 中文版使用说明 相关命令(输入字符敲空格自动输出): EditPlus常用快捷键: 相关教程: English ...
- plantUML最佳实践
plantUML 使用plantUML中的活动图用来画流程图很好用; 但类图等就不很好用; 个人体会如下: • 时序图 推荐 • 用例图 一般 • 类 图 不推荐, 用Visual Paradigm或 ...
- 环境变量之classpath配置和临时配置
前言 本篇文章介绍classpath环境变量的配置和一个环境变量配置的技巧:临时配置path环境变量. 正文 classpath环境变量配置 在上完了"Hello World"这堂 ...
- 常用计算机CMD 命令
cd\ '返回到根目录 cd.. '返回到上一级目录 1 cd 显示当前目录名或改变当前目录. 2 dir 显示目录中的文件和子目录列表. 3 md 创建一个目录. 4 mkdir 创 ...
- rem - 移动前端自适应适配布局解决方案和比较(转载)
原文链接:http://caibaojian.com/mobile-responsive-example.html 互联网上的自适应方案到底有几种呢?就我个人实践所知,有这么几种方案:· 固定一个某些 ...
- 有关KMP算法
KMP算法: 此算法的本质是首先对于模板字符串进行计算,生成一个数组(next数组),该数组反映了模板字符串的情况. 例: S: ABADACABABCD P: ABAB 当我们查询到P3与S3(B和 ...