安裝dlib C++


環境

  • Ubuntu18.04
  • dlib19

下載安裝

下載

從github下載 dlib

git clone https://github.com/davisking/dlib.git

安裝

在dlib目錄下, 依次執行。

mkdir build; cd build; cmake .. ; cmake --build .

Python使用

python setup.py install

安裝成功

python_examples 目錄下有很多例子, 可以試試

python opencv_webcam_face_detection.py # 打開電腦攝像頭檢測人臉

C++使用

創建測試文件 3d_point_cloud_ex.cpp

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

using namespace dlib;
using namespace std;

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();
}

編寫CMakelists.txt

project(test_dlib)
cmake_minimum_required(VERSION 2.8)

add_subdirectory(../dlib dlib_build) # 找到dlib下的源碼文件
add_executable(hc 3d_point_cloud_ex.cpp) # 生成執行文件名字為hc 

target_link_libraries(hc dlib::dlib) # hc這個執行文件需要鏈接到 dlib

說明:../dlib 可以使相對路徑也可以是絕對路徑

編譯

mkdir build 
cd build 
cmake ..
make

執行

.hc 

注:中間遇到一些warning沒有關系

GPU版本使用

使用GPU則需要添加如下幾個參數

git clone https://github.com/davisking/dlib.git
cd dlib
mkdir build
cd build
cmake .. -DDLIB_USE_CUDA=1 -DUSE_AVX_INSTRUCTIONS=1
cmake --build .

cmake 成功找到cuda和cudnn結果是這樣的

-- Found CUDA: /usr/local/cuda-9.2 (found suitable version "9.2", minimum required is "7.5")
-- Looking for cuDNN install...
-- Found cuDNN: /usr/local/cuda-9.2/lib64/libcudnn.so
-- Building a CUDA test project to see if your compiler is compatible with CUDA...
-- Checking if you have the right version of cuDNN installed.
-- Enabling CUDA support for dlib.  DLIB WILL USE CUDA
-- C++11 activated.
-- Configuring done
-- Generating done
-- Build files have been written to: /home/xxx/dlib/build

給python使用。

cd ..
python setup.py install --set USE_AVX_INSTRUCTIONS=yes --set DLIB_USE_CUDA=yes
多數博客會寫成這樣 
#python setup.py install --yes USE_AVX_INSTRUCTIONS --yes DLIB_USE_CUDA  # dlib 已經取消yes參數了。

安裝成功

import dlib
dlib.DLIB_USE_CUDA # 結果為True則可以使用了

參考


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM