使用CMake製作lib檔案以及Dlib機器學習庫的安裝和使用-親測可行
第一步驟:使用CMake製作lib檔案
CMake是一個跨平臺的安裝(編譯)工具,可以用簡單的語句來描述所有平臺的安裝(編譯過程)。他能夠輸出各種各樣的makefile或者project檔案,能測試編譯器所支援的C++特性,類似UNIX下的automake。
這裡使用Dlib機器學習庫做實驗,進行打包實驗
工具/原料
- CMake 3.2.1
- dlib-18.14
- VS2013
方法/步驟
我們下載CMake 3.2.1 ,
將dlib-18.14 解壓到D盤
建立打包後的資料夾dlib_building,
原始檔在dlib-18.14/dlib中,它是是Dlib軟體包中的資料夾
將路徑放入CMake中
點選Generate 生成專案。(Dlib中有make檔案)
成功之後目錄為
用VS2013開啟
dlib.vcxproj 專案檔案
右擊重新生成,完成之後我們會發現在原來資料夾多了一個debug資料夾,裡面就是dlib.lib檔案
只要將生成的lib檔案匯入工程中即可
匯入步驟為如下:
注意事項
什麼架構的工程生成什麼工程的lib檔案否則不好用
第二步驟:Dlib機器學習庫的安裝和使用
Dlib是一個機器學習的C++庫,包含了許多機器學習常用的演算法。而且文件和例子都非常詳細。這裡介紹該庫的安裝和使用工具/原料
VS2013
dlib-18.14
CMake 3.2.1
方法/步驟
我將dlib-18.14 解壓到D盤中了
開始新建一個專案,右擊解決方案管理器中的該專案開啟屬性。在專案目錄中新增 dlib- 18.14的路徑
下面我們就需要把dlib.lib匯入到原來的工程中去,右擊工程選擇屬性修改兩處地方
在工程 屬性 前處理器 前處理器定義 中加入
DLIB_PNG_SUPPORT
DLIB_JPEG_SUPPORT
兩項
程式碼測試,本人在debug
win32下執行如下程式碼
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <iostream>
using namespace dlib;
using namespace std;
// ----------------------------------------------------------------------------------------
int main(int argc, char** argv)
{
try
{
if (argc == 1)
{
cout << "Give some image files as arguments to this program." << endl;
return 0;
}
frontal_face_detector detector = get_frontal_face_detector();//定義一個frontal_face_detctor類的例項detector,用get_frontal_face_detector函式初始化該例項
image_window win;//一個顯示視窗
// Loop over all the images provided on the command line.
// 迴圈所有的圖片
for (int i = 1; i < argc; ++i)
{
cout << "processing image " << argv[i] << endl;
array2d<unsigned char> img;
load_image(img, argv[i]);// 載入一張圖片,從argv[i](圖片路勁)載入到變數img
// Make the image bigger by a factor of two. This is useful since
// the face detector looks for faces that are about 80 by 80 pixels
// or larger. Therefore, if you want to find faces that are smaller
// than that then you need to upsample the image as we do here by
// calling pyramid_up(). So this will allow it to detect faces that
// are at least 40 by 40 pixels in size. We could call pyramid_up()
// again to find even smaller faces, but note that every time we
// upsample the image we make the detector run slower since it must
// process a larger image.
/*確保檢測圖片是檢測器的兩倍。這第一點是十分有用的,因為臉部檢測器搜尋的人臉大小是80*80或者更大。
因此,如果你想找到比80*80小的人臉,需要將檢測圖片進行上取樣,我們可以呼叫pyramid_up()函式。
執行一次pyramid_up()我們能檢測40*40大小的了,如果我們想檢測更小的人臉,那還需要再次執行pyramid_up()函式。
注意,上取樣後,速度會減慢!*/
pyramid_up(img);//對影象進行上採用,檢測更小的人臉
// Now tell the face detector to give us a list of bounding boxes
// around all the faces it can find in the image.
//開始檢測,返回一系列的邊界框
std::vector<rectangle> dets = detector(img);//detector()函式檢測人臉,返回一系列邊界盒子
cout << "Number of faces detected: " << dets.size() << endl;//dets.size 人臉數量
// Now we show the image on the screen and the face detections as
// red overlay boxes.
// 在原圖片上顯示結果
win.clear_overlay();
win.set_image(img);
win.add_overlay(dets, rgb_pixel(255, 0, 0));
cout << "Hit enter to process the next image..." << endl;
cin.get();
}
}
catch (exception& e)
{
cout << "\nexception thrown!" << endl;
cout << e.what() << endl;
}
}
會出現一系列的錯誤
錯誤 28 error LNK1120: 26 個無法解析的外部命令 F:\VS2012\Test1\ConsoleApplication33\Debug\ConsoleApplication33.exe 1 1 ConsoleApplication33
錯誤 25 error LNK2019: 無法解析的外部符號 "private: unsigned char const * __thiscall dlib::png_loader::get_row(unsigned int)const " ([email protected][email protected]@@[email protected]),該符號在函式 "public: void __thiscall dlib::png_loader::get_image<class dlib::array2d<unsigned char,class dlib::memory_manager_stateless_kernel_1<char> > >(class dlib::array2d<unsigned char,class dlib::memory_manager_stateless_kernel_1<char> > &)const " ([email protected][email protected][email protected]@[email protected]@@[email protected]@@[email protected]@@[email protected][email protected]@[email protected]@@[email protected]@Z) 中被引用 F:\VS2012\Test1\ConsoleApplication33\ConsoleApplication33\源.obj ConsoleApplication33
錯誤 13 error LNK2019: 無法解析的外部符號 "protected: void __thiscall dlib::scrollable_region::set_total_rect_size(unsigned long,unsigned long)" ([email protected][email protected]@@[email protected]),該符號在函式 "public: void __thiscall dlib::image_display::set_image<class dlib::array2d<unsigned char,class dlib::memory_manager_stateless_kernel_1<char> > >(class dlib::array2d<unsigned char,class dlib::memory_manager_stateless_kernel_1<char> > const &)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@@[email protected]@@[email protected][email protected]@[email protected]@@[email protected]@Z) 中被引用 F:\VS2012\Test1\ConsoleApplication33\ConsoleApplication33\源.obj ConsoleApplication33
等等,一共20多個錯誤,後來在網上發現是CMAKE生成dlib.lib的時候預設的是debug x64,而我卻在debug win32下配置,所以說框架不匹配了,就會出現錯誤,然後我又在debug x64上面進行dlib配置,配置過程如下:
最後選擇確定,這就是選擇debug x64的過程
然後開啟專案屬性,如下圖:
代開屬性,找到VC++目錄下,進行包含目錄、引用目錄、庫目錄的dlib配置
根據自己dlib-18.14所放的路徑進行配置,最好不要手動輸入,用滑鼠直接點到所需要的路徑,防止出錯
根據最上面的步驟二Dlib機器學習庫的安裝和使用進行dlib最後三項配置
最後因為測試的程式碼中需要進行引數設定,還需要進行引數設定,即所用到的檢測的人來那圖片的圖片名,本文用1.jpg
測試圖片如下:
注意,測試圖片要放在源.CPP同一資料夾下
一切準備就緒,重新除錯程式碼,最終沒有出錯
執行的時候,人臉檢測比較慢,可以稍微等一下,最終的執行結果效果圖如下:
彩色變成灰色了,程式碼可能需要修改,總之dlib配置算是成功了,CMAKE生成lib是一定要與VS的編譯器一致,要不然錯誤很多,反正我CMAKE編譯時,選擇的是預設的,沒想到預設的編譯器不是debug win32,而是debug x64真是出乎我的意料
還有什麼不清楚的請小夥伴留言詢問,可能有的地方描述的還不到位!!!
參考:http://jingyan.baidu.com/article/25648fc18083af9191fd00b2.html
參考:http://jingyan.baidu.com/article/48b37f8d0461831a6464889c.html