導航:首頁 > 源碼編譯 > dlib編譯太慢

dlib編譯太慢

發布時間:2023-03-14 21:47:19

㈠ 誰用過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 系統好配置啊。

閱讀全文

與dlib編譯太慢相關的資料

熱點內容
山東濟南生產伺服器雲主機 瀏覽:310
演算法員跳槽四年 瀏覽:730
秦九昭演算法v0怎麼求 瀏覽:384
斗魚java 瀏覽:896
程序員對老師的感謝 瀏覽:29
什麼app能查看銀行卡照片 瀏覽:24
win7pdf虛擬列印 瀏覽:332
程序員喜歡的女生條件 瀏覽:123
阿里雲伺服器ip搭建教程 瀏覽:85
解壓和拉伸這一動畫的原理是什麼 瀏覽:740
tbc戰士的命令怒吼 瀏覽:481
idea快捷鍵看源碼 瀏覽:976
手機碎屏解壓工具 瀏覽:245
jsonrpcphp使用 瀏覽:566
網上求職系統源碼 瀏覽:699
pdf數字不顯示 瀏覽:890
convertwordtopdf 瀏覽:253
程序編譯基本單位 瀏覽:23
python分析圖片角度 瀏覽:64
阿里雲伺服器能復制數據嗎 瀏覽:562