谁用过python中的第三方库face recognition

Python018

简介

该库可以通过python或者命令行即可实现人脸识别的功能。使用dlib深度学习人脸识别技术构建,在户外脸部检测数据库基准(Labeled Faces in the Wild)上的准确率为99.38%。

在github上有相关的链接和API文档。

在下方为提供的一些相关源码或是文档。当前库的版本是v0.2.0,点击docs可以查看API文档,我们可以查看一些函数相关的说明等。

安装配置

安装配置很简单,按照github上的说明一步一步来就可以了。

根据你的python版本输入指令:

pip install face_recognition11

或者

pip3 install face_recognition11

正常来说,安装过程中会出错,会在安装dlib时出错,可能报错也可能会卡在那不动。因为pip在编译dlib时会出错,所以我们需要手动编译dlib再进行安装。

按照它给出的解决办法:

1、先下载下来dlib的源码。

git clone

2、编译dlib。

cd dlib

mkdir build

cd build

cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1

cmake --build1234512345

3、编译并安装python的拓展包。

cd ..

python3 setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA1212

注意:这个安装步骤是默认认为没有GPU的,所以不支持cuda。

在自己手动编译了dlib后,我们可以在python中import dlib了。

之后再重新安装,就可以配置成功了。

根据你的python版本输入指令:

pip install face_recognition11

或者

pip3 install face_recognition11

安装成功之后,我们可以在python中正常import face_recognition了。

编写人脸识别程序

编写py文件:

# -*- coding: utf-8 -*-

#

# 检测人脸

import face_recognition

import cv2

# 读取图片并识别人脸

img = face_recognition.load_image_file("silicon_valley.jpg")

face_locations = face_recognition.face_locations(img)

print face_locations

# 调用opencv函数显示图片

img = cv2.imread("silicon_valley.jpg")

cv2.namedWindow("原图")

cv2.imshow("原图", img)

# 遍历每个人脸,并标注

faceNum = len(face_locations)

for i in range(0, faceNum):

top = face_locations[i][0]

right = face_locations[i][1]

bottom = face_locations[i][2]

left = face_locations[i][3]

start = (left, top)

end = (right, bottom)

color = (55,255,155)

thickness = 3

cv2.rectangle(img, start, end, color, thickness)

# 显示识别结果

cv2.namedWindow("识别")

cv2.imshow("识别", img)

cv2.waitKey(0)

cv2.destroyAllWindows()12345678910111213141516171819202122232425262728293031323334353637381234567891011121314151617181920212223242526272829303132333435363738

注意:这里使用了python-OpenCV,一定要配置好了opencv才能运行成功。

运行结果:

程序会读取当前目录下指定的图片,然后识别其中的人脸,并标注每个人脸。

(使用图片来自美剧硅谷)

编写人脸比对程序

首先,我在目录下放了几张图片:

这里用到的是一张乔布斯的照片和一张奥巴马的照片,和一张未知的照片。

编写程序:

# 识别图片中的人脸

import face_recognition

jobs_image = face_recognition.load_image_file("jobs.jpg")

obama_image = face_recognition.load_image_file("obama.jpg")

unknown_image = face_recognition.load_image_file("unknown.jpg")

jobs_encoding = face_recognition.face_encodings(jobs_image)[0]

obama_encoding = face_recognition.face_encodings(obama_image)[0]

unknown_encoding = face_recognition.face_encodings(unknown_image)[0]

results = face_recognition.compare_faces([jobs_encoding, obama_encoding], unknown_encoding )

labels = ['jobs', 'obama']

print('results:'+str(results))

for i in range(0, len(results)):

if results[i] == True:

print('The person is:'+labels[i])123456789101112131415161718123456789101112131415161718

运行结果:

识别出未知的那张照片是乔布斯的。

摄像头实时识别

代码:

# -*- coding: utf-8 -*-

import face_recognition

import cv2

video_capture = cv2.VideoCapture(1)

obama_img = face_recognition.load_image_file("obama.jpg")

obama_face_encoding = face_recognition.face_encodings(obama_img)[0]

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)

if process_this_frame:

face_locations = face_recognition.face_locations(small_frame)

face_encodings = face_recognition.face_encodings(small_frame, face_locations)

face_names = []

for face_encoding in face_encodings:

match = face_recognition.compare_faces([obama_face_encoding], face_encoding)

if match[0]:

name = "Barack"

else:

name = "unknown"

face_names.append(name)

process_this_frame = not process_this_frame

for (top, right, bottom, left), name in zip(face_locations, face_names):

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), 2)

font = cv2.FONT_HERSHEY_DUPLEX

cv2.putText(frame, name, (left+6, bottom-6), font, 1.0, (255, 255, 255), 1)

cv2.imshow('Video', frame)

if cv2.waitKey(1) &0xFF == ord('q'):

break

video_capture.release()

cv2.destroyAllWindows()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545512345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455

识别结果:

我直接在手机上百度了几张图试试,程序识别出了奥巴马。

这个库很cool啊!

人脸识别软件如下:

1、Ageitgey

Ageitgey是最流行的免费人脸识别软件,它在GitHub上也有37.6k的星星。该软件可以通过Python API或其二进制命令行工具使用。这个平台有关于安装的所有说明,这使得它更加有趣和流行。

2、CompreFace

CompreFace于2020年在GitHub上发布,拥有约900颗星星。它是少数几个只需一个docker compose命令即可使用的自托管REST API自由人脸识别软件之一。该软件可以识别多个视频流中的人脸。CompreFace还有一个用于管理用户角色和面部集合的UI。

3、DeepFace

DeepFace于2020年在Github上发布,拥有约1100颗星星。这个免费的人脸识别软件支持不同的人脸识别方法,如FaceNet和Insightface。

4、FaceNet

FaceNet是一个由谷歌研究人员创建的免费人脸识别程序,它是一个开源Python库,实现了该程序。FaceNet具有很高的准确性,但唯一的缺点是它没有RESTAPI。

5、InsightFaceInsightFace

是另一款免费人脸识别软件,拥有约800颗星星。该软件使用最新和准确的人脸识别方法。

python三步实现人脸识别

Face Recognition软件包

这是世界上最简单的人脸识别库了。你可以通过Python引用或者命令行的形式使用它,来管理和识别人脸。

该软件包使用dlib中最先进的人脸识别深度学习算法,使得识别准确率在《Labled Faces in the world》测试基准下达到了99.38%。

它同时提供了一个叫face_recognition的命令行工具,以便你可以用命令行对一个文件夹中的图片进行识别操作。

特性

在图片中识别人脸

找到图片中所有的人脸

找到并操作图片中的脸部特征

获得图片中人类眼睛、鼻子、嘴、下巴的位置和轮廓

找到脸部特征有很多超级有用的应用场景,当然你也可以把它用在最显而易见的功能上:美颜功能(就像美图秀秀那样)。

鉴定图片中的脸

识别图片中的人是谁。

你甚至可以用这个软件包做人脸的实时识别。

这里有一个实时识别的例子:

1https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py

安装

环境要求

Python3.3+或者Python2.7

MacOS或者Linux(Windows不做支持,但是你可以试试,也许也能运行)

安装步骤

在MacOS或者Linux上安装

首先,确保你安装了dlib,以及该软件的Python绑定接口。如果没有的话,看这篇安装说明:

1   https://gist.github.com/ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf   

然后,用pip安装这个软件包:

如果你安装遇到问题,可以试试这个安装好了的虚拟机:

1   https://medium.com/@ageitgey/try-deep-learning-in-python-now-with-a-fully-pre-configured-vm-1d97d4c3e9b   

在树莓派2+上安装

看这篇说明:

1   https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65   

在Windows上安装

虽然Windows不是官方支持的,但是有热心网友写出了一个Windows上的使用指南,请看这里:

1   https://github.com/ageitgey/face_recognition/issues/175#issue-257710508   

使用已经配置好的虚拟机(支持VMWare和VirtualBox)

看这篇说明:

1   https://medium.com/@ageitgey/try-deep-learning-in-python-now-with-a-fully-pre-configured-vm-1d97d4c3e9b   

使用方法

命令行接口

如果你已经安装了face_recognition,那么你的系统中已经有了一个名为face_recognition的命令,你可以使用它对图片进行识别,或者对一个文件夹中的所有图片进行识别。

首先你需要提供一个文件夹,里面是所有你希望系统认识的人的图片。其中每个人一张图片,图片以人的名字命名。

然后你需要准备另一个文件夹,里面是你要识别的图片。

然后你就可以运行face_recognition命令了,把刚刚准备的两个文件夹作为参数传入,命令就会返回需要识别的图片中都出现了谁。

输出中,识别到的每张脸都单独占一行,输出格式为

通过Python模块使用

你可以通过导入face_recognition模块来使用它,使用方式超级简单,文档在这里:https://face-recognition.readthedocs.io

自动找到图片中所有的脸

看看这个例子自己实践一下:

1   https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture.py   

你还可以自定义替换人类识别的深度学习模型。

注意:想获得比较好的性能的话,你可能需要GPU加速(使用英伟达的CUDA库)。所以编译的时候你也需要开启dlib的GPU加速选项。

你也可以通过这个例子实践一下:

1   https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_picture_cnn.py   

如果你有很多图片和GPU,你也可以并行快速识别,看这篇文章:

1   https://github.com/ageitgey/face_recognition/blob/master/examples/find_faces_in_batches.py   

自动识别人脸特征

试试这个例子:

1   https://github.com/ageitgey/face_recognition/blob/master/examples/find_facial_features_in_picture.py   

识别人脸鉴定是哪个人

这里是一个例子:

1   https://github.com/ageitgey/face_recognition/blob/master/examples/recognize_faces_in_picture