转载请注明出处:http://blog.csdn.net/hongbin_xu 或 http://hongbin96.com/
文章链接:http://blog.csdn.net/hongbin_xu/article/details/74981819 或http://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

或者

pip3 install face_recognition

正常来说,安装过程中会出错,会在安装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

2、编译dlib。

cd dlib
mkdir build
cd build
cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1
cmake --build

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

cd ..
python3 setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA

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

pip install face_recognition

或者

pip3 install face_recognition

安装成功之后,我们可以在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 = 3cv2.rectangle(img, start, end, color, thickness)# 显示识别结果
cv2.namedWindow("识别")
cv2.imshow("识别", img)cv2.waitKey(0)
cv2.destroyAllWindows()

注意:这里使用了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])

运行结果:

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

摄像头实时识别

代码:

# -*- coding: utf-8 -*-
import face_recognition
import cv2video_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 = Truewhile 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_framefor (top, right, bottom, left), name in zip(face_locations, face_names):top *= 4right *= 4bottom *= 4left *= 4cv2.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_DUPLEXcv2.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'):breakvideo_capture.release()
cv2.destroyAllWindows()

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

这个库很cool啊!

————————————————2017.10.19————————————————–

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

————————————————2017.11.3————————————————–
由于很多人问我有关安装dlib时碰到的问题。直接用pip安装会出错,需要我们自己编译安装。自己编译也费不了多少力。
所以这里分享一下Ubuntu下安装dlib的过程:

  • 安装相关依赖包
sudo apt-get install libboost-python-dev cmake  
  • 下载dlib源码。(目前最新版本是19.7)
    github下载链接:https://github.com/davisking/dlib
    官网也有下载:http://dlib.net/

  • 接下来开始编译。
    到dlib中找到setup.py, 运行进行安装。

python setup.py install

会先自动编译dlib的c++库。

随后安装到python。

可以正常导入dlib了。

————————————————2018.2.10————————————————–
face_recognition库的性能还是有较多限制,所以我后来又在空闲时间基于dlib重新写了一个软件,相对之前的那个效果会好一点。没有用face_recognition库,它的速度相对比较慢,而且打包也会遇到一些比较奇葩的问题。
基于PYQT编写一个人脸识别软件(2)

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

  1. 基于Python的开源人脸识别库:离线识别率高达99.38%

    基于Python的开源人脸识别库:离线识别率高达99.38% 2019年04月18日 18:13:18 AI终结者 阅读数 1233 项目地址:https://github.com/ageitgey/ ...

  2. Python人脸识别教程 - 基于Python的开源人脸识别库:离线识别率高达99.38%

    Python人脸识别教程 - 基于Python的开源人脸识别库:离线识别率高达99.38% 仅用 Python 和命令行就可以实现人脸识别的库开源了.该库使用 dlib 顶尖的深度学习人脸识别技术构建 ...

  3. 基于Python的开源人脸识别库,离线识别率高达99.38%

    使用 dlib 顶尖的深度学习人脸识别技术构建,在户外脸部检测数据库基准(Labeled Faces in the Wild benchmark)上的准确率高达 99.38%. 这也提供了一个简单的 ...

  4. Python的开源人脸识别库:离线识别率高达99.38%【源码】

    以往的人脸识别主要是包括人脸图像采集.人脸识别预处理.身份确认.身份查找等技术和系统.现在人脸识别已经慢慢延伸到了ADAS中的驾驶员检测.行人跟踪.甚至到了动态物体的跟踪.由此可以看出,人脸识别系统已 ...

  5. python模块cv2人脸识别_python face_recognition模块实现人脸识别

    import face_recognition #人脸识别库 pip cmake dlib import cv2 #读取图像 face_image1 = face_recognition.load_i ...

  6. 人脸识别库face_recognition安装简单教程

    face_recognition是一款免费.开源.实时.离线的Python人脸识别库.Github网址为https://github.com/ageitgey/face_recognition.如安装 ...

  7. python开源人脸识别库识别率达99_Python的开源人脸识别库:离线识别率高达99.38%...

    本文的模型使用了C++工具箱dlib基于深度学习的最新人脸识别方法,基于户外脸部数据测试库Labeled Faces in the Wild 的基准水平来说,达到了99.38%的准确率. 数据测试库L ...

  8. 基于Python+OpenCV的人脸识别实现带墨镜效果

    环境以及执行步骤 相关介绍 环境配置 相关库安装介绍 上代码 github地址 动图介绍 改进 相关介绍 你好! 项目起初来源于一本科生的毕业设计,由于我给了一版更加优秀,所以初始版本的例子给予分享. ...

  9. linux开源人脸识别库,人脸识别身份验证 HIDL

    概览 借助人脸识别身份验证功能,用户只需将自己的面孔对准设备即可将其解锁.Android 10 增加了对一种新的人脸识别身份验证堆栈的支持,这种堆栈可安全处理摄像头帧,从而在支持的硬件上进行人脸识别身 ...

  10. python读取视频流做人脸识别_基于 Python + OpenCV 进行人脸识别,视频追踪代码全注释...

    1 #-*- coding: utf-8 -*- 2 from __future__ importunicode_literals3 #操作文件 4 importos5 #科学计算 6 importn ...

最新文章

  1. error MIDL2025/2026
  2. 拦截导弹(最长递增子序列)
  3. 初探 Vue 生命周期和钩子函数
  4. clover configurator_枯木逢春,用Clover让老电脑从NVME SSD启动 再用叁年没问题
  5. boost::alignment_of相关的测试程序
  6. linux文件夹权限问题
  7. 解决$ is not define
  8. 《2021国庆出行报告》出炉:全国高速拥堵里程占比同比下降37%
  9. Java cms 孔浩老师 完整视频加源码 共131集 百度网盘永久链接 无需密码解压
  10. 【历史上的今天】12 月 1 日:新浪网成立;钉钉上线;古登堡计划发布
  11. uni-app - MUMU模拟器模拟 iPad 尺寸开发(分辨率及DPI调整)
  12. Setting语言与输入法列表客制化
  13. 【华为机试】鸡蛋放在篮子里
  14. 实时控制软件开发第二次作业总结
  15. JAVA//JAVA基本程序设计架构
  16. Anaconda使用感悟
  17. 算法学习之——矩形切割思想
  18. Pointpillars三维点云实时检测
  19. 聚焦爬虫与通用爬虫详解
  20. 详解vue生命周期(重点!!)

热门文章

  1. 【信息学奥赛一本通】网址链接
  2. 面试中遇到的java笔试题
  3. C#中的Builder模式
  4. 防火墙如何打开和关闭某个端口
  5. 开源成语答题小程序红包设置教程
  6. 接口测试面试题及参考答案,轻松拿捏面试官
  7. 【软件开发架构平台】CH2 Spring IoC和Bean管理
  8. Spring Boot 的 JSON RPC(客户端示例) - briandilley/jsonrpc4j Wiki
  9. 基于F407ZGT6的WS2812B彩灯驱动
  10. Mybatis源码分析(一) | 如何调试Mybatis源码