㈠ 谁用过python中的第三方库face recognition
简介
该库可以通过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()
注意:这里使用了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 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()5455
识别结果:
我直接在手机上网络了几张图试试,程序识别出了奥巴马。
这个库很cool啊!
㈡ [python]机器学习库dlib中,编译器无法识别dlib.image_window()怎么办
将Cocos2d-x的VS工程导入XCode新建Cocos2d-x工程vs2010cocos2d-x3.0创建VS工程----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!
㈢ c++使用dlib/dnn/core.h编译出错 函数模板已经定义
1 LIB文件直接加入到工程文件列表中
在VC中打开File View一页,选中工程名,单击鼠标右键,然后选中"Add Files to Project"菜单,在弹出的文件对话框中选中要加入DLL的LIB文件。然后在首先要使用该函数的地方加上该LIB的头文件,如#include "..\lib.h"即可(没有头文件当然就不用了)。
2 设置工程的 Project Settings来加载DLL的LIB文件
打开工程的 Project Settings菜单,选中Link,然后在Object/library moles下的文本框中输入DLL的LIB文件,如you.lib(或者lib文件的路径,包括文件名)。然后在首先要使用该函数的地方加上该LIB的头文件,如#include "..\lib.h"即可(没有头文件当然就不用了)。
3 通过程序代码的方式
加入预编译指令#pragma comment (lib,"*.lib"),这种方法优点是可以利用条件预编译指令链接不同版本的LIB文件。因为,在Debug方式下,产生的LIB文件是Debug版本,如Regd.lib;在Release方式下,产生的LIB文件是Release版本,如Regr.lib。然后在首先要使用该函数的地方加上该LIB的头文件,如#include "..\lib.h"即可(没有头文件当然就不用了)。
当应用程序对DLL的LIB文件加载后,还需要把DLL对应的头文件(*.h)包含到其中,在这个头文件中给出了DLL中定义的函数原型,然后声明
㈣ dlib库,怎么在python中安装
这几天刚好用到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,花了我两天时间去琢磨调试,上面的经验需要的朋友请拿去进一步整理,以免浪费不必好的时间。有问题的童鞋请在下面留言。
㈤ 如何线上部署用python基于dlib写的人脸识别算法
python使用dlib进行人脸检测与人脸关键点标记
Dlib简介:
首先给大家介绍一下Dlib
我使用的版本是dlib-18.17,大家也可以在我这里下载:
之后进入python_examples下使用bat文件进行编译,编译需要先安装libboost-python-dev和cmake
cd to dlib-18.17/python_examples
./compile_dlib_python_mole.bat 123
之后会得到一个dlib.so,复制到dist-packages目录下即可使用
这里大家也可以直接用我编译好的.so库,但是也必须安装libboost才可以,不然python是不能调用so库的,下载地址:
将.so复制到dist-packages目录下
sudo cp dlib.so /usr/local/lib/python2.7/dist-packages/1
最新的dlib18.18好像就没有这个bat文件了,取而代之的是一个setup文件,那么安装起来应该就没有这么麻烦了,大家可以去直接安装18.18,也可以直接下载复制我的.so库,这两种方法应该都不麻烦~
有时候还会需要下面这两个库,建议大家一并安装一下
9.安装skimage
sudo apt-get install python-skimage1
10.安装imtools
sudo easy_install imtools1
Dlib face landmarks Demo
环境配置结束之后,我们首先看一下dlib提供的示例程序
1.人脸检测
dlib-18.17/python_examples/face_detector.py 源程序:
#!/usr/bin/python# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt## This example program shows how to find frontal human faces in an image. In# particular, it shows how you can take a list of images from the command# line and display each on the screen with red boxes overlaid on each human# face.## The examples/faces folder contains some jpg images of people. You can run# this program on them and see the detections by executing the# following command:# ./face_detector.py ../examples/faces/*.jpg## This face detector is made using the now classic Histogram of Oriented# Gradients (HOG) feature combined with a linear classifier, an image# pyramid, and sliding window detection scheme. This type of object detector# is fairly general and capable of detecting many types of semi-rigid objects# in addition to human faces. Therefore, if you are interested in making# your own object detectors then read the train_object_detector.py example# program. ### COMPILING THE DLIB PYTHON INTERFACE# Dlib comes with a compiled python interface for python 2.7 on MS Windows. If# you are using another python version or operating system then you need to# compile the dlib python interface before you can use this file. To do this,# run compile_dlib_python_mole.bat. This should work on any operating# system so long as you have CMake and boost-python installed.# On Ubuntu, this can be done easily by running the command:# sudo apt-get install libboost-python-dev cmake## Also note that this example requires scikit-image which can be installed# via the command:# pip install -U scikit-image# Or downloaded from . import sys
import dlib
from skimage import io
detector = dlib.get_frontal_face_detector()
win = dlib.image_window()
print("a");for f in sys.argv[1:]:
print("a");
print("Processing file: {}".format(f))
img = io.imread(f)
# The 1 in the second argument indicates that we should upsample the image
# 1 time. This will make everything bigger and allow us to detect more
# faces.
dets = detector(img, 1)
print("Number of faces detected: {}".format(len(dets))) for i, d in enumerate(dets):
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
i, d.left(), d.top(), d.right(), d.bottom()))
win.clear_overlay()
win.set_image(img)
win.add_overlay(dets)
dlib.hit_enter_to_continue()# Finally, if you really want to you can ask the detector to tell you the score# for each detection. The score is bigger for more confident detections.# Also, the idx tells you which of the face sub-detectors matched. This can be# used to broadly identify faces in different orientations.if (len(sys.argv[1:]) > 0):
img = io.imread(sys.argv[1])
dets, scores, idx = detector.run(img, 1) for i, d in enumerate(dets):
print("Detection {}, score: {}, face_type:{}".format(
d, scores[i], idx[i]))5767778798081
我把源代码精简了一下,加了一下注释: face_detector0.1.py
# -*- coding: utf-8 -*-import sys
import dlib
from skimage import io#使用dlib自带的frontal_face_detector作为我们的特征提取器detector = dlib.get_frontal_face_detector()#使用dlib提供的图片窗口win = dlib.image_window()#sys.argv[]是用来获取命令行参数的,sys.argv[0]表示代码本身文件路径,所以参数从1开始向后依次获取图片路径for f in sys.argv[1:]: #输出目前处理的图片地址
print("Processing file: {}".format(f)) #使用skimage的io读取图片
img = io.imread(f) #使用detector进行人脸检测 dets为返回的结果
dets = detector(img, 1) #dets的元素个数即为脸的个数
print("Number of faces detected: {}".format(len(dets))) #使用enumerate 函数遍历序列中的元素以及它们的下标
#下标i即为人脸序号
#left:人脸左边距离图片左边界的距离 ;right:人脸右边距离图片左边界的距离
#top:人脸上边距离图片上边界的距离 ;bottom:人脸下边距离图片上边界的距离
for i, d in enumerate(dets):
print("dets{}".format(d))
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}"
.format( i, d.left(), d.top(), d.right(), d.bottom())) #也可以获取比较全面的信息,如获取人脸与detector的匹配程度
dets, scores, idx = detector.run(img, 1)
for i, d in enumerate(dets):
print("Detection {}, dets{},score: {}, face_type:{}".format( i, d, scores[i], idx[i]))
#绘制图片(dlib的ui库可以直接绘制dets)
win.set_image(img)
win.add_overlay(dets) #等待点击
dlib.hit_enter_to_continue()041424344454647484950
分别测试了一个人脸的和多个人脸的,以下是运行结果:
运行的时候把图片文件路径加到后面就好了
python face_detector0.1.py ./data/3.jpg12
一张脸的:
两张脸的:
这里可以看出侧脸与detector的匹配度要比正脸小的很多
2.人脸关键点提取
人脸检测我们使用了dlib自带的人脸检测器(detector),关键点提取需要一个特征提取器(predictor),为了构建特征提取器,预训练模型必不可少。
除了自行进行训练外,还可以使用官方提供的一个模型。该模型可从dlib sourceforge库下载:
arks.dat.bz2
也可以从我的连接下载:
这个库支持68个关键点的提取,一般来说也够用了,如果需要更多的特征点就要自己去训练了。
dlib-18.17/python_examples/face_landmark_detection.py 源程序:
#!/usr/bin/python# The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt## This example program shows how to find frontal human faces in an image and# estimate their pose. The pose takes the form of 68 landmarks. These are# points on the face such as the corners of the mouth, along the eyebrows, on# the eyes, and so forth.## This face detector is made using the classic Histogram of Oriented# Gradients (HOG) feature combined with a linear
㈥ 如何提高python下的dlib人脸检测速度
Dlib is capable of detecting faces in very small areas (80x80 pixels). You are probably sending raw WebCam frames at approximately 1280x720 resolution, which is not necessary. I recommend from my experience to rece the frames about a quarter of the original resolution. Yes, 320x180 is fine for Dlib. In consequence you will get 4x speed.
· try turning on the compilation optimizations while building Dlib, you will get significantly improvement in speed.
· Dlib works faster with grayscale images. You do not need the color on the webcam frame. You can use OpenCV to convert into grayscale the previously reced in size frame.
· Dlib takes its time finding faces but is extremely fast finding landmarks on faces. Only if your Webcam provides a high framerate (24-30fps), you could skip some frames because faces normally doesn't move so much.
㈦ ubuntu里面怎么安装dlib
下面是在ubuntu下如何为Python安装dlib:
1.在官网dlib官网下载最新版本的dlib
由于dlib最初是一个C++库,要安装为python第三方库,
2.要下载boost将C++ 编译为python,同时还要下载cmake
命令:sudo apt-get install libboost-python-dev cmake11
3.然后切换到 dlib-18.17/python_examples目录
然后运行
./compile_dlib_python_mole.bat 11
会生成dlib.so
4.该脚本还要加上可执行权限
这样在dlib-18.17/python_examples目录下就可以导入dlib库,但在其它地方 还不能导入
将dlib.so复制到python第三库的文件夹下
sudo cp dlib.so /usr/local/lib/python2.7/dist-packages/11
这样dlib库就安装好了
㈧ Python的Dlib安装时一直出现找不到boost怎么解决
刚刚在 macOS 遇到了类似问题并有了一个解决方案:解决 macOS 下 Python 安装 Dlib 的问题:Cmake 找不到 boost-python
Linux 以及 其他 类 Unix 系统可能都可以参考上面在 macOS High Sierra 下的思路来通过设定 ~/.bashprofile 里面的 CMAKE_PREFIX_PATH 指向 boost 安装路径来解决这个问题。
现在我正在Windows虚拟机里面测试,发现似乎也是 cmake 没有设定 boost 位置导致的。
我尝试一下用类似方法来解决,然后把细节过程截图发上来。
到Python Extension Packages for Windows
下载对应系统版本的 boost python 的 whl:
上面这些内容部分参考了 BOOST 官方文档的内容:Getting Started on Windows
上述步骤完成之后,使用 pip install dlib 来安装吧.
我自己在 Windows 7 32bit 系统下测到一半提示编译错误,不过能确定的是上面这些步骤都没问题了,算了,我懒得折腾了,以上内容供参考了。
希望大家都安装顺利,另外开发机还是 类 Unix 系统好配置啊。