当前位置:首页 » 编程软件 » dlib编译

dlib编译

发布时间: 2022-01-08 15:20:14

1. 如何线上部署用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

2. 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库就安装好了

3. 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 系统好配置啊。

4. 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,花了我两天时间去琢磨调试,上面的经验需要的朋友请拿去进一步整理,以免浪费不必好的时间。有问题的童鞋请在下面留言。

5. dlib文件中的.a文件是怎样生成的

两个obj文件怎么连接生成一个exe程序
要保证这两个obj中有且只有一个main函数,然后用link命令来进行连接。
link /OUT:输出文件.exe [参数] A.obj B.obj
比如需要用1.obj和2.obj生成a.exe,a.exe是一个控制台程序,那么就是
link /OUT:a.exe /SUBSYSTEM:CONSOLE 1.obj 2.obj
如果a.exe是一个Windows窗体程序,那么就是
link /OUT:a.exe /SUBSYSTEM:WINDOWS 1.obj 2.obj
注:Link是Visual Studio中VC的连接器,如果你安装过VC,那么link就在你的VC目录的Bin目录下。在VC环境的话,那么可以通过新建工程,然后将obj文件添加至工程中,然后连接运行。
具体步骤如下:
[如果是VC6.0]
1、点击文件菜单,新建。
2、点击工程,选择Win32 Console Application或者Win32 Application。
3、在右边输入工程的名字(这个随便),和工程的保存路径。
4、点击确定,并在新弹出的窗口中选择空项目。
5、在环境的左边,右键点击刚创建的项目名称,选择添加现存项目。
6、在打开文件对话框中的文件类型选择目标文件(*.obj),并选择obj文件。
7、点击编译即可。

6. 如何用dlib实现人脸检测

如何用dlib实现人脸检测
一般情况下,R.java是不会缺少的,它由系统自动生成,修改,如果发生问题,可如下操作 可以先clear项目,然后重建项目,rebuild项目, 检查你的.xml文件名是否出现错误,文件名大写了也是会错误的 检查其他会在R.java中生成id的资源文件,只要是影响R.java文件的文件有错误,它就有可能错误,使你编译的过程中丢乏订催寡诎干挫吮旦经失R.java文件

7. [python]机器学习库dlib中,编译器无法识别dlib.image_window()怎么办

将Cocos2d-x的VS工程导入XCode新建Cocos2d-x工程vs2010cocos2d-x3.0创建VS工程----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

8. 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中定义的函数原型,然后声明

9. 在ubuntu上安装dlib时,安装好了boost

你那种编译安装方法好像过时了。不知道直接pip install dlib 是不是能成功,这种方法直接安装whl文件,不用编译

10. 如何使用dlib库

1.运行D:\搜狗高速下载\cmake-3.4.3-win32-x86\bin路径下的cmake-gui.exe

并配置编译路径

where is the source code 选取 D:\搜狗高速下载\dlib-18.18\dlib-18.18\dlib (一般都是选CMakeLists.txt文件所在的路径)

where to build the binaries 选取 D:/dlib_building (新建文件夹)

然后点击Configure按钮,我们是vs2008,所以选visual studio 9 2008

完成后下面会出现这样的提示

然后点击Genrate按钮,等待进度条完成... ...!!!

2.编译dlib,使用vs2008

用vs2008打开D:\dlib_building\dlib.sln

直接运行,就会生成Debug/Release文件夹,里面有我们要的dlib库文件

3.测试

新建一个win32工程

添加dlib的include路径到vs2008

把dlib.lib放到工程路径下(或者添加lib路径到vs2008也可以)

我用D:\搜狗高速下载\dlib-18.18\dlib-18.18\examples\3d_point_cloud_ex.cpp来测试

-----------------------------------------下面是代码----------------------------------------

// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*

This is an example illustrating the use of the perspective_window tool
in the dlib C++ Library. It is a simple tool for displaying 3D point
clouds on the screen.

*/

#include "stdafx.h"
#include <dlib/gui_widgets.h>
#include <dlib/image_transforms.h>
#include <cmath>

using namespace dlib;
using namespace std;

#pragma comment(lib,"dlib.lib")

// ----------------------------------------------------------------------------------------

int main()
{
// Let's make a point cloud that looks like a 3D spiral.
std::vector<perspective_window::overlay_dot> points;
dlib::rand rnd;
for (double i = 0; i < 20; i+=0.001)
{
// Get a point on a spiral
dlib::vector<double> val(sin(i),cos(i),i/4);

// Now add some random noise to it
dlib::vector<double> temp(rnd.get_random_gaussian(),
rnd.get_random_gaussian(),
rnd.get_random_gaussian());
val += temp/20;

// Pick a color based on how far we are along the spiral
rgb_pixel color = colormap_jet(i,0,20);

// And add the point to the list of points we will display
points.push_back(perspective_window::overlay_dot(val, color));
}

// Now finally display the point cloud.
perspective_window win;
win.set_title("perspective_window 3D point cloud");
win.add_overlay(points);
win.wait_until_closed();
}

// ----------------------------------------------------------------------------

什么都没改,直接就可以出效果了,一个用鼠标控制的3D图像效果

热点内容
海量小文件存储ftp 发布:2024-05-04 19:13:21 浏览:273
真我手机如何解除手机密码 发布:2024-05-04 18:24:44 浏览:708
数据库嵌套 发布:2024-05-04 18:24:29 浏览:146
豌豆荚源码 发布:2024-05-04 18:10:54 浏览:117
苹果消息的声音安卓怎么弄 发布:2024-05-04 18:06:23 浏览:555
减配配置有哪些 发布:2024-05-04 18:04:58 浏览:963
查询密码单是什么 发布:2024-05-04 17:54:03 浏览:41
安卓系统不支持网络怎么办 发布:2024-05-04 17:49:31 浏览:128
oraclesqlserver 发布:2024-05-04 17:49:16 浏览:47
关爱脚本 发布:2024-05-04 17:43:47 浏览:422