基于Python的开源人脸识别库:离线识别率高达99.38%
2017-07-28 16:36
585 查看
转自 : https://www.baidu.com/home/news/data/newspage?nid=3610841293596761341&n_type=0&p_from=1&dtype=-1
选自Github
机器之心编译
参与:路雪
仅用 Python 和命令行就可以实现人脸识别的库开源了。该库使用 dlib 顶尖的深度学习人脸识别技术构建,在户外脸部检测数据库基准(Labeled Faces in the Wild benchmark)上的准确率高达 99.38%。
该项目是要构建一款免费、开源、实时、离线的网络 app,支持组织者使用人脸识别技术或二维码识别所有受邀人员。
有了世界上最简单的人脸识别库,使用 Python 或命令行,即可识别和控制人脸。
该库使用 dlib 顶尖的深度学习人脸识别技术构建,在户外脸部检测数据库基准(Labeled Faces in the Wild benchmark)上的准确率高达 99.38%。
这也提供了一个简单的 face_recognition 命令行工具,你可以打开命令行中任意图像文件夹,进行人脸识别!
项目地址:https://github.com/ageitgey/face_recognition#face-recognition
特征
找出图片中的人脸
找出下面图片中所有的人脸:
import face_recognition
找到并且控制图像中的脸部特征
找到并勾勒出每个人的眼睛、鼻子、嘴和下巴。
找出脸部特征对很多重要的事情都非常有用。但是你也可以用它来做一些「蠢事」,比如数字化妆(美图):
识别图片中的人脸
识别每张图片中的人物。
你甚至可以使用该库和其他的 Python 库执行实时人脸识别:
此处可查看代码示例:https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py
安装
要求:
Python 3+ 或 Python 2.7
macOS 或 Linux (Windows 未测试)
还可在树莓派 2+上运行(按照具体指令来安装运行:https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65)
预配置的 VM 图像同样可用。
使用pin3从pypi安装这一模块:
pip3 install face_recognition
重要提示:pip 尝试编译 dlib 依赖时很可能会遇到一些问题。如果遇到问题,前往该地址(https://gist.github.com/ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf)从来源(而不是 pip)中安装 dlib,从而修复该错误。
手动安装 dlib 后,再次运行 pip3 install face_recognition,完成安装。
如果安装方面还有问题,你还可以试试预配置的 VM(https://medium.com/@ageitgey/try-deep-learning-in-python-now-with-a-fully-pre-configured-vm-1d97d4c3e9b)
用途
命令行界面
安装 face_recognition 时,你会得到一个名为 face_recognition 的简单命令行程序,该程序可用于识别照片或装满照片的文件夹中的人脸。
首先,你需要提供一个包含图片的文件夹,且每张图片中的每个人你都认识。每个人有一个图像文件,文件名就是图片中人物的名字:
然后,你需要再建一个文件夹,包含你想要识别的图像文件:
之后,你仅需要在已知人物文件夹和未知人物文件夹(或单个图像)中运行 face_recognition 命令,该程序会告诉你每个图像中的人物是谁:
$ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures//unknown_pictures/unknown.jpg,Barack Obama
每张人脸的输出结果只有一行,由文件名和找到的人物名组成,中间用逗号分隔。
unknown_person 是未与已知人物文件夹中任何照片相匹配的人脸。
如果你只想知道每张照片中的人物姓名,不在意文件名,那么你可以采用以下做法:
$ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/ | cut -d ',' -f2
如果你的电脑配有多核 CPU,你就可以同时执行多个人脸识别任务。例如,如果你的系统有 4 个 CPU 核,你可以同时使用这 4 个 CPU 核,那么同样时间内处理的图像数量是原来的四倍。
如果你使用 Python 3.4 或更新的版本,传入--cpus <number_of_cpu_cores_to_use>参数:
$ face_recognition -cpus 4 ./pictures_of_people_i_know/ ./unknown_pictures/
你还可以传入--cpus -1,来使用系统中所有的 CPU 核。
Python 模块
使用 face_recognition 模块,几行代码轻松控制人脸,so easy!
API 文件地址:https://face-recognition.readthedocs.io
自动定位图像中人物的脸部特征
图像人脸识别
注意事项
该人脸识别模型基于成年人照片训练,因此对儿童照片的识别效果不好。该模型默认比较阈值是 0.6,容易混淆儿童的面部。
将该模型配置到云主机(Heroku、AWS 等)
face_recognition 赖以存在的 dlib 是用 C++语言写的,因此将该内置该模型的 app 配置到 Heroku 或 AWS 等云主机提供商就很复杂。在该 repo 中有一个 Dockerfile 示例,展示如何在 Docker 容器中运行内置 face_recognition 模型的 app(详见该网址:https://www.docker.com/)。参考该示例,您能够将该模型配置到任何支持 Docker 图像的服务。
常见问题
问题:使用 face_recognition 或运行样本时,出现 Illegal instruction (core dumped)。
解决方案:dlib 需要在 SSE4 或 AVX 支持下编译,但是你的 CPU 太旧,无法支持编译。你需要根据此处(https://github.com/ageitgey/face_recognition/issues/11#issuecomment-287398611)所示修改代码,然后对 dilb 进行重新编译。
问题:运行摄像头样本时,出现 RuntimeError: Unsupported image type, must be 8bit gray or RGB image.
解决方案:你的摄像头可能并未在 OpenCV 上正确设置。点击此处(https://github.com/ageitgey/face_recognition/issues/21#issuecomment-287779524)了解更多。
问题:运行 pip2 install face_recognition 时出现 MemoryError。
解决方案:face_recognition_models 文件太大,不适合你可用的 pip 缓存内存。试一下 pip2 --no-cache-dir install face_recognition,解决该问题。
问题:AttributeError: 'module' object has no attribute 'face_recognition_model_v1'
解决方案:你安装的 dlib 版本过旧,需要 19.4 或者更新的版本。请升级 dlib 版本。
问题:TypeError: imread() got an unexpected keyword argument 'mode'
解决方案:你安装的 scipy 版本过旧,需要 0.17 或者更新的版本。请升级 scipy 版本。
源码:
import face_recognition
import cv2
# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
# other example, but it includes some basic performance tweaks to make things run a lot faster:
# 1. Process each video frame at 1/4 resolution (though still display it at full resolution)
# 2. Only detect faces in every other frame of video.
# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.
# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)
# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
while True:
# Grab a single frame of video
ret, frame = video_capture.read()
# Resize frame of video to 1/4 size for faster face recognition processing
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# Only process every other frame of video to save time
if process_this_frame:
# Find all the faces and face encodings in the current frame of video
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:
# See if the face is a match for the known face(s)
match = face_recognition.compare_faces([obama_face_encoding], face_encoding)
name = "Unknown"
if match[0]:
name = "Barack"
face_names.append(name)
process_this_frame = not process_this_frame
# Display the results
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
# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
# Draw a label with a name below the face
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 the resulting image
cv2.imshow('Video', frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()
选自Github
机器之心编译
参与:路雪
仅用 Python 和命令行就可以实现人脸识别的库开源了。该库使用 dlib 顶尖的深度学习人脸识别技术构建,在户外脸部检测数据库基准(Labeled Faces in the Wild benchmark)上的准确率高达 99.38%。
该项目是要构建一款免费、开源、实时、离线的网络 app,支持组织者使用人脸识别技术或二维码识别所有受邀人员。
有了世界上最简单的人脸识别库,使用 Python 或命令行,即可识别和控制人脸。
该库使用 dlib 顶尖的深度学习人脸识别技术构建,在户外脸部检测数据库基准(Labeled Faces in the Wild benchmark)上的准确率高达 99.38%。
这也提供了一个简单的 face_recognition 命令行工具,你可以打开命令行中任意图像文件夹,进行人脸识别!
项目地址:https://github.com/ageitgey/face_recognition#face-recognition
特征
找出图片中的人脸
找出下面图片中所有的人脸:
import face_recognition
找到并且控制图像中的脸部特征
找到并勾勒出每个人的眼睛、鼻子、嘴和下巴。
找出脸部特征对很多重要的事情都非常有用。但是你也可以用它来做一些「蠢事」,比如数字化妆(美图):
识别图片中的人脸
识别每张图片中的人物。
你甚至可以使用该库和其他的 Python 库执行实时人脸识别:
此处可查看代码示例:https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py
安装
要求:
Python 3+ 或 Python 2.7
macOS 或 Linux (Windows 未测试)
还可在树莓派 2+上运行(按照具体指令来安装运行:https://gist.github.com/ageitgey/1ac8dbe8572f3f533df6269dab35df65)
预配置的 VM 图像同样可用。
使用pin3从pypi安装这一模块:
pip3 install face_recognition
重要提示:pip 尝试编译 dlib 依赖时很可能会遇到一些问题。如果遇到问题,前往该地址(https://gist.github.com/ageitgey/629d75c1baac34dfa5ca2a1928a7aeaf)从来源(而不是 pip)中安装 dlib,从而修复该错误。
手动安装 dlib 后,再次运行 pip3 install face_recognition,完成安装。
如果安装方面还有问题,你还可以试试预配置的 VM(https://medium.com/@ageitgey/try-deep-learning-in-python-now-with-a-fully-pre-configured-vm-1d97d4c3e9b)
用途
命令行界面
安装 face_recognition 时,你会得到一个名为 face_recognition 的简单命令行程序,该程序可用于识别照片或装满照片的文件夹中的人脸。
首先,你需要提供一个包含图片的文件夹,且每张图片中的每个人你都认识。每个人有一个图像文件,文件名就是图片中人物的名字:
然后,你需要再建一个文件夹,包含你想要识别的图像文件:
之后,你仅需要在已知人物文件夹和未知人物文件夹(或单个图像)中运行 face_recognition 命令,该程序会告诉你每个图像中的人物是谁:
$ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures//unknown_pictures/unknown.jpg,Barack Obama
每张人脸的输出结果只有一行,由文件名和找到的人物名组成,中间用逗号分隔。
unknown_person 是未与已知人物文件夹中任何照片相匹配的人脸。
如果你只想知道每张照片中的人物姓名,不在意文件名,那么你可以采用以下做法:
$ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/ | cut -d ',' -f2
如果你的电脑配有多核 CPU,你就可以同时执行多个人脸识别任务。例如,如果你的系统有 4 个 CPU 核,你可以同时使用这 4 个 CPU 核,那么同样时间内处理的图像数量是原来的四倍。
如果你使用 Python 3.4 或更新的版本,传入--cpus <number_of_cpu_cores_to_use>参数:
$ face_recognition -cpus 4 ./pictures_of_people_i_know/ ./unknown_pictures/
你还可以传入--cpus -1,来使用系统中所有的 CPU 核。
Python 模块
使用 face_recognition 模块,几行代码轻松控制人脸,so easy!
API 文件地址:https://face-recognition.readthedocs.io
自动定位图像中人物的脸部特征
图像人脸识别
注意事项
该人脸识别模型基于成年人照片训练,因此对儿童照片的识别效果不好。该模型默认比较阈值是 0.6,容易混淆儿童的面部。
将该模型配置到云主机(Heroku、AWS 等)
face_recognition 赖以存在的 dlib 是用 C++语言写的,因此将该内置该模型的 app 配置到 Heroku 或 AWS 等云主机提供商就很复杂。在该 repo 中有一个 Dockerfile 示例,展示如何在 Docker 容器中运行内置 face_recognition 模型的 app(详见该网址:https://www.docker.com/)。参考该示例,您能够将该模型配置到任何支持 Docker 图像的服务。
常见问题
问题:使用 face_recognition 或运行样本时,出现 Illegal instruction (core dumped)。
解决方案:dlib 需要在 SSE4 或 AVX 支持下编译,但是你的 CPU 太旧,无法支持编译。你需要根据此处(https://github.com/ageitgey/face_recognition/issues/11#issuecomment-287398611)所示修改代码,然后对 dilb 进行重新编译。
问题:运行摄像头样本时,出现 RuntimeError: Unsupported image type, must be 8bit gray or RGB image.
解决方案:你的摄像头可能并未在 OpenCV 上正确设置。点击此处(https://github.com/ageitgey/face_recognition/issues/21#issuecomment-287779524)了解更多。
问题:运行 pip2 install face_recognition 时出现 MemoryError。
解决方案:face_recognition_models 文件太大,不适合你可用的 pip 缓存内存。试一下 pip2 --no-cache-dir install face_recognition,解决该问题。
问题:AttributeError: 'module' object has no attribute 'face_recognition_model_v1'
解决方案:你安装的 dlib 版本过旧,需要 19.4 或者更新的版本。请升级 dlib 版本。
问题:TypeError: imread() got an unexpected keyword argument 'mode'
解决方案:你安装的 scipy 版本过旧,需要 0.17 或者更新的版本。请升级 scipy 版本。
源码:
import face_recognition
import cv2
# This is a demo of running face recognition on live video from your webcam. It's a little more complicated than the
# other example, but it includes some basic performance tweaks to make things run a lot faster:
# 1. Process each video frame at 1/4 resolution (though still display it at full resolution)
# 2. Only detect faces in every other frame of video.
# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.
# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)
# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
while True:
# Grab a single frame of video
ret, frame = video_capture.read()
# Resize frame of video to 1/4 size for faster face recognition processing
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# Only process every other frame of video to save time
if process_this_frame:
# Find all the faces and face encodings in the current frame of video
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:
# See if the face is a match for the known face(s)
match = face_recognition.compare_faces([obama_face_encoding], face_encoding)
name = "Unknown"
if match[0]:
name = "Barack"
face_names.append(name)
process_this_frame = not process_this_frame
# Display the results
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
# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
# Draw a label with a name below the face
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 the resulting image
cv2.imshow('Video', frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()
相关文章推荐
- 基于Python的开源人脸识别库:离线识别率高达99.38%
- 基于Python的开源人脸识别库:离线识别率高达99.38%——新开源的用了一下感受一下
- 基于Python的开源人脸识别库:离线识别率高达99.38%
- 资源 | 基于Python的开源人脸识别库:离线识别率高达99.38%
- face_recognition开源人脸识别库:离线识别率高达99.38%
- face_recognition开源人脸识别库:离线识别率高达99.38%
- 开源 | 基于Python的人脸识别:识别准确率高达99.38%!
- 一种基于python的人脸识别开源系统
- 转:基于开源项目OpenCV的人脸识别Demo版整理(不仅可以识别人脸,还可以识别眼睛鼻子嘴等)【模式识别中的翘楚】
- 基于python3 OpenCV3实现静态图片人脸识别
- 基于Python的人脸检测及识别小程序(二)
- 【基于百度AI的人脸识别Python实现】
- 基于Python的人脸检测和识别小程序(一)
- python基于openCV3的人脸识别
- Python基于OpnenCV实现人脸识别
- 基于开源项目OpenCV的人脸识别Demo版整理(不仅可以识别人脸,还可以识别眼睛鼻子嘴等)【模式识别中的翘楚】
- 基于开源项目OpenCV的人脸识别Demo版整理(不仅可以识别人脸,还可以识别眼睛鼻子嘴等)
- 基于Python的人脸检测及识别小程序文档说明
- Python基于OpenCV库Adaboost实现人脸识别功能详解
- 基于Python的人脸检测及识别小程序(四)