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

Python014

简介

该库可以通过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啊!

这几天刚好用到Python,其中用到了Dlib库的人脸对齐算法。python中需要用到import dlib.pyd文件,这个文件需要用python对dlib源码进行编译生成。

具体的生成步骤如下:

1. 安装boost库

本人用的是boost_1_61_0版本,在这里简单说下安装步骤,具体的方法可以参考网上其它人的博客。

也可参考本文博文《windows下使用bjam安装Boost》。安装完成之后,记得配置环境变量。

2. 用python的CMD窗口,进入到dlib库的目录下,输入命令:python setup.py install.

如果提前配置好了boost库,并且把生成的boost_python-vc120-mt-1_61.dll和boost_python-vc120-mt-gd-1_61.dll两个文件放到python目录下。

还需要配置cmake的环境变量,../cmake/bin添加在系统环境变量path里,否则出错:cannot find cmake in the path.

成功编译后,会在../dlib/dist/dlib/目录下找到生成的dlib.pyd文件,把该文件拷贝放到python目录下的Lib\site-packages\下面,这样就完成了python编译dlib库的工作。

注意:在用python进行dlib编译时,可能因为python版本的问题,在Lib\distutils\log.py文件中编译出错

UnicodeEncodeError: 'gbk' codec can't encode character u'\x9' in position...的问题。

stream.write('%s\n' % msg) ///源文件

修改方法:stream.write('%s\n' % msg.decode('gbk')),即可编译通过。这是python2.7版本中的gbk和unicode编解码的原因造成的。

注意:上面的方法本人成功编译过一次,但是后来又有问题。总是显示"Could Not Found Boost."(期间卸载了电脑上的vs2008和vs2010,仅保留vs2013).

后来,借鉴了其他网友的方法如下:

首先,添加系统变量 BOOST_ROOT = D:\boost_1_59_0 和 BOOST_LIBRARYDIR = D:\boost_1_59_0\stage\lib。然后打开cmd,进入到boost目录,输入以下指令编译python library(我的python是32位,因此address-model=32):

编译python库生成两个lib文件:libboost_python-vc120-mt-s-1_61和libboost_python-vc120-mt-sgd-1_61,复制到...\stage\lib目录下面。

再键入命令:python setup.py install,显示如下:

不过按下面这种方式编译dlib,对于32位的笔记本需要把stream.write('%s\n' % msg.decode('gbk'))恢复为原来的stream.write('%s\n' % msg). 而在64位的PC机上,保留下面的修改的方法:stream.write('%s\n' % msg.decode('gbk'))stream.flush()并且在python的Lib\site-packages文件夹下新建一个sitecustomize.py,内容为:import sys

reload(sys)

sys.setdefaultencoding('utf8') #set default encoding to utf-8

两台机器上都可以编译成功。

Ps:在win7系统下用python编译dlib,花了我两天时间去琢磨调试,上面的经验需要的朋友请拿去进一步整理,以免浪费不必好的时间。有问题的童鞋请在下面留言。