应用一个基于Python的开源人脸识别库,face_recognition

转载请注明出处:http://blog.csdn.net/hongbin_xuhttp://hongbin96.com/
文章链接:http://blog.csdn.net/hongbin_xu/article/details/74981819http://hongbin96.com/125

今天看微信时,看到一篇推送文章介绍了一个基于python的开源人脸识别库,且其离线识别率高达99.38%,于是上网搜了搜。
网上相关的中文文章基本都是一样的,且都是从github上的英文版本介绍翻译过来的,所以我就直接看github上的介绍了。(github链接

简介

该库可以通过python或者命令行即可实现人脸识别的功能。使用dlib深度学习人脸识别技术构建,在户外脸部检测数据库基准(Labeled Faces in the Wild)上的准确率为99.38%。
在github上有相关的链接和API文档。
这里写图片描述
在下方为提供的一些相关源码或是文档。当前库的版本是v0.2.0,点击docs可以查看API文档,我们可以查看一些函数相关的说明等。
这里写图片描述

安装配置

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

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

pip install face_recognition
  
  
  • 1

或者

pip3 install face_recognition
  
  
  • 1

正常来说,安装过程中会出错,会在安装dlib时出错,可能报错也可能会卡在那不动。因为pip在编译dlib时会出错,所以我们需要手动编译dlib再进行安装。
github上给的解决办法的链接需要翻墙才能访问,都懂得。(¬ -̮ ¬)
先进入网页代理:https://mm.ww.rrjs.pw/
在网页中输入:https://gist.github.com/ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf
按照它给出的解决办法:
1、先下载下来dlib的源码。

git clone https://github.com/davisking/dlib.git
  
  
  • 1

2、编译dlib。

cd dlib
mkdir build
cd build
cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1
cmake --build
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

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

cd ..
python3 setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA
  
  
  • 1
  • 2

注意:这个安装步骤是默认认为没有GPU的,所以不支持cuda。
在自己手动编译了dlib后,我们可以在python中import dlib了。
之后再重新安装,就可以配置成功了。
根据你的python版本输入指令:

pip install face_recognition
  
  
  • 1

或者

pip3 install face_recognition
  
  
  • 1

安装成功之后,我们可以在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()
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

注意:这里使用了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])
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

运行结果:
这里写图片描述
识别出未知的那张照片是乔布斯的。

摄像头实时识别

代码:

# -*- 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()
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

识别结果:
我直接在手机上百度了几张图试试,程序识别出了奥巴马。
这里写图片描述

这里写图片描述

这个库很cool啊!

————————————————2017.1019————————————————–

我自己后来还基于这个库编写了一个小软件,实现了前面的博文中提到的功能。
传送门:基于PYQT编写一个人脸识别软件

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: face_recognition一个基于Python人脸识别,它可以识别人脸并将其与已知的人脸进行匹配。它的优势在于它使用了深度学习技术,可以在大规模数据集上进行训练,从而提高了识别的准确性和速度。此外,它还支持多种图像格式和多种编程语言,使得它可以在不同的平台上使用。 ### 回答2: face_recognition一个用于人脸识别Python,具有以下优势: 1. 简单易用:face_recognition提供了简洁明了的API,使人脸识别变得简单易用。通过几行代码,可以实现人脸的定位、特征提取和对比等操作。 2. 高效精确:face_recognition采用了基于深度学习的算法,能够在大规模的人脸数据集上进行高效的人脸识别。它使用人脸关键点(landmarks)和特征描述向量(face embeddings)进行人脸比对,能够准确识别人脸,并具有很强的辨别能力。 3. 跨平台兼容:face_recognition可以在多个操作系统上运行,包括Windows、Linux和macOS等。无论是在本地开发还是在云端部署,都可以很方便地使用该进行人脸识别。 4. 开源自由:face_recognition开源的,可以免费使用和修改。这意味着用户可以根据自己的需求进行二次开发和定制,同时也能够从庞大的开发者社区中获得支持和帮助。 总之,face_recognition具有简单易用、高效精确、跨平台兼容和开源自由等优势。它是一个强大的人脸识别工具,可以广泛应用人脸识别、人脸验证、人脸检测等领域。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值