计算机系统应用教程网站

网站首页 > 技术文章 正文

Python人脸识别技术及人脸识别过程解析

btikc 2025-01-09 10:47:40 技术文章 16 ℃ 0 评论

一、环境搭建

1.系统环境

Ubuntu 17.04
Python 2.7.14
pycharm 开发工具

2.开发环境,安装各种系统包

人脸检测基于dlib,dlib依赖Boost和cmake

在windows中如果要使用dlib还是比较麻烦的,如果想省时间可以在anaconda中安装

conda install -c conda-forge dlib=19.4

$ sudo apt-get install build-essential cmake
$ sudo apt-get install libgtk-3-dev
$ sudo apt-get install libboost-all-dev

其他重要的包

$ pip install numpy
$ pip install scipy
$ pip install opencv-python
$ pip install dlib

安装 face_recognition

# 安装 face_recognition
$ pip install face_recognition
# 安装face_recognition过程中会自动安装 numpy、scipy 等 


二、使用教程

1、facial_features文件夹

此demo主要展示了识别指定图片中人脸的特征数据,下面就是人脸的八个特征,我们就是要获取特征数据

'chin',
 'left_eyebrow',
 'right_eyebrow',
 'nose_bridge',
 'nose_tip',
 'left_eye',
 'right_eye',
 'top_lip',
 'bottom_lip'

运行结果:

自动识别图片中的人脸,并且识别它的特征

原图:

特征数据,数据就是运行出来的矩阵,也就是一个二维数组

代码:

# -*- coding: utf-8 -*-
# 自动识别人脸特征
# filename : find_facial_features_in_picture.py
# 导入pil模块 ,可用命令安装 apt-get install python-Imaging
from PIL import Image, ImageDraw
# 导入face_recogntion模块,可用命令安装 pip install face_recognition
import face_recognition
# 将jpg文件加载到numpy 数组中
image = face_recognition.load_image_file("chenduling.jpg")
#查找图像中所有面部的所有面部特征
face_landmarks_list = face_recognition.face_landmarks(image)
print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))
for face_landmarks in face_landmarks_list:
 #打印此图像中每个面部特征的位置
 facial_features = [
 'chin',
 'left_eyebrow',
 'right_eyebrow',
 'nose_bridge',
 'nose_tip',
 'left_eye',
 'right_eye',
 'top_lip',
 'bottom_lip'
 ]
 for facial_feature in facial_features:
 print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))
 #让我们在图像中描绘出每个人脸特征!
 pil_image = Image.fromarray(image)
 d = ImageDraw.Draw(pil_image)
 for facial_feature in facial_features:
 d.line(face_landmarks[facial_feature], width=5)
 pil_image.show()

2、find_face文件夹

不仅能识别出来所有的人脸,而且可以将其截图挨个显示出来,打印在前台窗口

原始的图片

识别的图片

代码:

# -*- coding: utf-8 -*-
# 识别图片中的所有人脸并显示出来
# filename : find_faces_in_picture.py
# 导入pil模块 ,可用命令安装 apt-get install python-Imaging
from PIL import Image
# 导入face_recogntion模块,可用命令安装 pip install face_recognition
import face_recognition
# 将jpg文件加载到numpy 数组中
image = face_recognition.load_image_file("yiqi.jpg")
# 使用默认的给予HOG模型查找图像中所有人脸
# 这个方法已经相当准确了,但还是不如CNN模型那么准确,因为没有使用GPU加速
# 另请参见: find_faces_in_picture_cnn.py
face_locations = face_recognition.face_locations(image)
# 使用CNN模型
# face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn")
# 打印:我从图片中找到了 多少 张人脸
print("I found {} face(s) in this photograph.".format(len(face_locations)))
# 循环找到的所有人脸
for face_location in face_locations:
 # 打印每张脸的位置信息
 top, right, bottom, left = face_location
 print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right)) 
# 指定人脸的位置信息,然后显示人脸图片
 face_image = image[top:bottom, left:right]
 pil_image = Image.fromarray(face_image)
 pil_image.show()

3、know_face文件夹

通过设定的人脸图片识别未知图片中的人脸

# -*- coding: utf-8 -*-
# 识别人脸鉴定是哪个人
# 导入face_recogntion模块,可用命令安装 pip install face_recognition
import face_recognition
#将jpg文件加载到numpy数组中
chen_image = face_recognition.load_image_file("chenduling.jpg")
#要识别的图片
unknown_image = face_recognition.load_image_file("sunyizheng.jpg")
#获取每个图像文件中每个面部的面部编码
#由于每个图像中可能有多个面,所以返回一个编码列表。
#但是由于我知道每个图像只有一个脸,我只关心每个图像中的第一个编码,所以我取索引0。
chen_face_encoding = face_recognition.face_encodings(chen_image)[0]
print("chen_face_encoding:{}".format(chen_face_encoding))
unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]
print("unknown_face_encoding :{}".format(unknown_face_encoding))
known_faces = [
 chen_face_encoding
]
#结果是True/false的数组,未知面孔known_faces阵列中的任何人相匹配的结果
results = face_recognition.compare_faces(known_faces, unknown_face_encoding)
print("result :{}".format(results))
print("这个未知面孔是 陈都灵 吗? {}".format(results[0]))
print("这个未知面孔是 我们从未见过的新面孔吗? {}".format(not True in results))

4、video文件夹

通过调用电脑摄像头动态获取视频内的人脸,将其和我们指定的图片集进行匹配,可以告知我们视频内的人脸是否是我们设定好的

实现:

代码:

# -*- coding: utf-8 -*-
# 摄像头头像识别
import face_recognition
import cv2
video_capture = cv2.VideoCapture(0)
# 本地图像
chenduling_image = face_recognition.load_image_file("chenduling.jpg")
chenduling_face_encoding = face_recognition.face_encodings(chenduling_image)[0]
# 本地图像二
sunyizheng_image = face_recognition.load_image_file("sunyizheng.jpg")
sunyizheng_face_encoding = face_recognition.face_encodings(sunyizheng_image)[0]
# 本地图片三
zhangzetian_image = face_recognition.load_image_file("zhangzetian.jpg")
zhangzetian_face_encoding = face_recognition.face_encodings(zhangzetian_image)[0]
# Create arrays of known face encodings and their names
# 脸部特征数据的集合
known_face_encodings = [
 chenduling_face_encoding,
 sunyizheng_face_encoding,
 zhangzetian_face_encoding
]
# 人物名称的集合
known_face_names = [
 "michong",
 "sunyizheng",
 "chenduling"
]
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
while True:
 # 读取摄像头画面
 ret, frame = video_capture.read()
 # 改变摄像头图像的大小,图像小,所做的计算就少
 small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
 # opencv的图像是BGR格式的,而我们需要是的RGB格式的,因此需要进行一个转换。
 rgb_small_frame = small_frame[:, :, ::-1]
 # Only process every other frame of video to save time
 if process_this_frame:
 # 根据encoding来判断是不是同一个人,是就输出true,不是为flase
 face_locations = face_recognition.face_locations(rgb_small_frame)
 face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
 face_names = []
 for face_encoding in face_encodings:
 # 默认为unknown
 matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
 name = "Unknown"
 # if match[0]:
 # name = "michong"
 # If a match was found in known_face_encodings, just use the first one.
 if True in matches:
 first_match_index = matches.index(True)
 name = known_face_names[first_match_index]
 face_names.append(name)
 process_this_frame = not process_this_frame
 # 将捕捉到的人脸显示出来
 for (top, right, bottom, left), name in zip(face_locations, face_names):
 # Scale back up face locations since the frame we detected in was scaled to 1/4 size
 top *= 4
 right *= 4
 bottom *= 4
 left *= 4
 # 矩形框
 cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
 #加上标签
 cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
 font = cv2.FONT_HERSHEY_DUPLEX
 cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
 # Display
 cv2.imshow('monitor', frame)
 # 按Q退出
 if cv2.waitKey(1) & 0xFF == ord('q'):
 break
video_capture.release()
cv2.destroyAllWindows()

5、boss文件夹

本开源项目,主要是结合摄像头程序+极光推送,实现识别摄像头中的人脸。并且通过极光推送平台给移动端发送消息!

人脸识别全过程解析

1、人脸检测

“人脸检测(Face Detection)”是检测出图像中人脸所在位置的一项技术。

人脸检测算法的输入是一张图片,输出是人脸框坐标序列(0个人脸框或1个人脸框或多个人脸框)。一般情况下,输出的人脸坐标框为一个正朝上的正方形,但也有一些人脸检测技术输出的是正朝上的矩形,或者是带旋转方向的矩形。

常见的人脸检测算法基本是一个“扫描”加“判别”的过程,即算法在图像范围内扫描,再逐个判定候选区域是否是人脸的过程。因此人脸检测算法的计算速度会跟图像尺寸、图像内容相关。开发过程中,我们可以通过设置“输入图像尺寸”、或“最小脸尺寸限制”、或“人脸数量上限”的方式来加速算法。

2、人脸配准

“人脸配准(Face Alignment)”是定位出人脸上五官关键点坐标的一项技术。

人脸配准算法的输入是“一张人脸图片”加“人脸坐标框”,输出五官关键点的坐标序列。五官关键点的数量是预先设定好的一个固定数值,可以根据不同的语义来定义(常见的有5点、68点、90点等等)。

当前效果的较好的一些人脸配准技术,基本通过深度学习框架实现,这些方法都是基于人脸检测的坐标框,按某种事先设定规则将人脸区域扣取出来,缩放的固定尺寸,然后进行关键点位置的计算。因此,若不计入图像缩放过程的耗时,人脸配准算法是可以计算量固定的过程。另外,相对于人脸检测,或者是后面将提到的人脸提特征过程,人脸配准算法的计算耗时都要少很多。

3、人脸属性识别

“人脸属性识别(Face Attribute)”是识别出人脸的性别、年龄、姿态、表情等属性值的一项技术。

一般的人脸属性识别算法的输入是“一张人脸图”和“人脸五官关键点坐标”,输出是人脸相应的属性值。人脸属性识别算法一般会根据人脸五官关键点坐标将人脸对齐(旋转、缩放、扣取等操作后,将人脸调整到预定的大小和形态),然后进行属性分析。

常规的人脸属性识别算法识别每一个人脸属性时都是一个独立的过程,即人脸属性识别只是对一类算法的统称,性别识别、年龄估计、姿态估计、表情识别都是相互独立的算法。但的一些基于深度学习的人脸属性识别也具有一个算法同时输入性别、年龄、姿态等属性值的能力。

4、以在基本保证算法效果的前提下,将模型大小和运算速度优化到移动端可用的状态。

5、人脸比对(人脸验证、人脸识别、人脸检索、人脸聚类)

“人脸比对(Face Compare)”是衡量两个人脸之间相似度的算法

人脸比对算法的输入是两个人脸特征(注:人脸特征由前面的人脸提特征算法获得),输出是两个特征之间的相似度。人脸验证、人脸识别、人脸检索都是在人脸比对的基础上加一些策略来实现。相对人脸提特征过程,单次的人脸比对耗时极短,几乎可以忽略。

基于人脸比对可衍生出人脸验证(Face Verification)、人脸识别(Face Recognition)、人脸检索(Face Retrieval)、人脸聚类(Face Cluster)等算法。

6、人脸验证

“人脸验证(Face Verification)”是判定两个人脸图是否为同一人的算法。

它的输入是两个人脸特征,通过人脸比对获得两个人脸特征的相似度,通过与预设的阈值比较来验证这两个人脸特征是否属于同一人(即相似度大于阈值,为同一人;小于阈值为不同)。

7、 人脸识别

“人脸识别(Face Recognition)”是识别出输入人脸图对应身份的算法。

它的输入一个人脸特征,通过和注册在库中N个身份对应的特征进行逐个比对,找出“一个”与输入特征相似度较高的特征。将这个较高相似度值和预设的阈值相比较,如果大于阈值,则返回该特征对应的身份,否则返回“不在库中”。

8、人脸检索

“人脸检索”是查找和输入人脸相似的人脸序列的算法。

人脸检索通过将输入的人脸和一个集合中的说有人脸进行比对,根据比对后的相似度对集合中的人脸进行排序。根据相似度从高到低排序的人脸序列即使人脸检索的结果。

9、人脸聚类

“人脸聚类(Face Cluster)”是将一个集合内的人脸根据身份进行分组的算法。

人脸聚类也通过将集合内所有的人脸两两之间做人脸比对,再根据这些相似度值进行分析,将属于同一个身份的人划分到一个组里。

在没有进行人工身份标注前,只知道分到一个组的人脸是属于同一个身份,但不知道确切身份。另外假设集合中有N个人脸,那么人脸聚类的算法复杂度为O(N2)

10、人脸活体

“人脸活体(FaceLiveness)”是判断人脸图像是来自真人还是来自攻击假体(照片、视频等)的方法。

和前面所提到的人脸技术相比,人脸活体不是一个单纯算法,而是一个问题的解法。这个解法将用户交互和算法紧密结合,不同的交互方式对应于完全不同的算法。鉴于方法的种类过于繁多,这里只介绍“人脸活体”的概念,不再展开。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表