Dlib初入門:實現人臉特徵點檢測(附CMake安裝教程)

我的系統:Win10 64位(其實所有系統都差不多的流程)

軟件工具:Visual Studio 2017

目錄

一、安裝CMake

        二、使用CMake編譯生成Dlib庫文件(Release版)

        三、在Visual Studio中配置Dlib

         四、配置openCv

         五、實現人臉特徵點檢測


一、安裝CMake

其實好簡單,就兩步:

1、cmake官網下載cmake(選擇後綴爲msi的文件,這個文件裏面纔有cmake.exe)

下載完後我的目錄:

2、將cmake.exe所在的bin目錄添加到環境變量中

在第一步雙擊msi文件,安裝cmake的時候,就有選項詢問你是否配置環境變量的,如果那時候沒有選擇配,就只能手動配置啦。

例如我的就是:D:\2018three\class\js\HW\tool\cmakeexe\bin(選擇有cmake.exe在的那個路徑)

3、檢查是否成功安裝

打開“cmake.exe”閃退不要緊,cmake-gui.exe能打開就行。或者直接在cmd輸入"cmake",看看安裝是否成功。如下:

 

二、使用CMake編譯生成Dlib庫文件(Release版)

1、官網上下載文件(可以看看官網的編譯教程)

      例如解壓後我的dlib文件夾名是 “ dlib-19.16 ”。

2、使用CMake編譯dlib(參考鏈接:使用cmake命令行模式編譯dlib

      在dlib文件夾下打開cmd (主要是爲了讓cmd的路徑在dlib文件夾下)

      然後執行如下代碼,mkdir build的意思是在dlib文件夾下再新建一個“build”文件夾。cd build 是進入build文件夾路徑。我是已          經在dilb文件夾下新建好了build文件夾,並且路徑已經定位到build文件夾下。所以我沒有執行這兩條語句。

mkdir build
cd build
cmake -G "Visual Studio 14 2015 Win64" ..
cmake --build . --config Release

一般而言,執行完"cmake --build . --config Release"就編譯完成了,但是我報錯“Error:could not load cache”了。

這個回答的提醒下,我再執行了一次"cmake ..",才成功生成庫文件。

這時候我們會看到dlib的build目錄下生成了一堆文件。

 

三、在Visual Studio中配置Dlib

1、首先在VS中創建一個空項目(例如我的項目名是DlibTest2)

2、打開項目屬性頁,注意切換到“Release”版本下,而不是“Debug”。

3、打開VC++目錄->包含目錄->添加上dlib文件夾的路徑

4、打開鏈接器->輸入->附加依賴項->添加上剛剛編譯生成好的dlib.lib的路徑

ok,配置完成了。

 

四、配置openCv

dlib的example實例代碼有不少是需要結合openCv庫的。所以最好配置opencv。

大部分弄dlib的都早已配置好了opencv了吧?我就不再詳述了。opencv安裝配置教程

 

五、實現人臉特徵點檢測

1、將dlib-19.16\dlib\all目錄下的source.cpp加入到源文件中(必須要!)

2、下載人臉庫,解壓後將dat文件放置在 "項目名/項目名"目錄下,例如我的:

3、新建cpp文件:facepoint.cpp(代碼出自:Dlib提取人臉特徵點算法

#include <dlib/opencv.h>
#include <opencv2/opencv.hpp>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/gui_widgets.h>
 
using namespace dlib;
using namespace std;
 
int main()
{
	try
	{
		cv::VideoCapture cap(0);
		if (!cap.isOpened())
		{
			cerr << "Unable to connect to camera" << endl;
			return 1;
		}
 
		//image_window win;
 
		// Load face detection and pose estimation models.
		frontal_face_detector detector = get_frontal_face_detector();
		shape_predictor pose_model;
		deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;
 
		// Grab and process frames until the main window is closed by the user.
		while (cv::waitKey(30) != 27)
		{
			// Grab a frame
			cv::Mat temp;
			cap >> temp;
 
			cv_image<bgr_pixel> cimg(temp);
			// Detect faces 
			std::vector<rectangle> faces = detector(cimg);
			// Find the pose of each face.
			std::vector<full_object_detection> shapes;
			for (unsigned long i = 0; i < faces.size(); ++i)
				shapes.push_back(pose_model(cimg, faces[i]));
	
			if (!shapes.empty()) {
				for (int i = 0; i < 68; i++) {
					circle(temp, cvPoint(shapes[0].part(i).x(), shapes[0].part(i).y()), 3, cv::Scalar(0, 0, 255), -1);
					//	shapes[0].part(i).x();//68個
				}
			}
			//Display it all on the screen
			imshow("Dlib特徵點", temp);
 
		}
	}
	catch (serialization_error& e)
	{
		cout << "You need dlib's default face landmarking model file to run this example." << endl;
		cout << "You can get it from the following URL: " << endl;
		cout << "   http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2" << endl;
		cout << endl << e.what() << endl;
	}
	catch (exception& e)
	{
		cout << e.what() << endl;
	}
}

最後的運行結果:

該項目的結構列表爲:

其中"webcam_face_pose_ex.cpp"來自 “dlib / examples” 目錄,這是dlib自帶的示例代碼,可以運行嘗試下功能。注意一個項目中只能有一個主函數main,不然會報錯。

對了,記得也要把vs調到realease,不然可能出現錯誤。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章