當前位置:首頁 » 編程軟體 » 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圖像效果

熱點內容
手機版如何下載gmc伺服器 發布:2024-04-25 05:56:30 瀏覽:170
哈夫曼c語言編譯 發布:2024-04-25 05:43:20 瀏覽:663
贖回清演算法 發布:2024-04-25 05:33:00 瀏覽:677
mysql安裝圖解linux 發布:2024-04-25 05:18:59 瀏覽:605
有哪些配置中心的框架 發布:2024-04-25 05:11:37 瀏覽:829
進程的調度演算法代碼 發布:2024-04-25 04:25:20 瀏覽:588
maven編譯scala 發布:2024-04-25 04:25:11 瀏覽:110
手機存儲空間里的其他 發布:2024-04-25 04:10:42 瀏覽:27
文件改文件夾 發布:2024-04-25 04:03:00 瀏覽:563
50次方編程 發布:2024-04-25 04:02:59 瀏覽:58