ubuntu16.04 qt5上利用dlib庫實現Face Landmark Detection

Face Landmark Detection在疲勞檢測和眼球跟蹤等有着巨大作用。 

安裝步驟:

git clone https://github.com/davisking/dlib   #下載dlib
cd dlib                                  
cd examples                                   #進入dlib下的examples文件夾
mkdir build                                   #新建build文件夾,存放cmake編譯後的執行文件
cd build                                      #進入新建好的build文件夾
#cmake編譯examples整個文件夾
cmake .. -DDLIB_USE_CUDA=1 -DUSE_AVX_INSTRUCTIONS=1    #選擇是否使用cuda avx
cmake --build . --config Release 

攝像頭測試:

進入example/build目錄,執行:

./webcam_face_pose_ex

 qt5上c++實現:

配置.pro文件:

TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

#QMAKE_CFLAGS = -DDLIB_USE_CUDA=1 -DUSE_AVX_INSTRUCTIONS=1   #這句加不加感覺沒啥用,速度一樣 有沒人知道?

#system
INCLUDEPATH += /usr/local/lib \
               /usr/lib/x86_64-linux-gnu

LIBS += -L/usr/local/lib

#opencv
INCLUDEPATH += /usr/include \
               /usr/include/opencv \
               /usr/include/opencv2/

LIBS += -L /usr/lib/libopencv_*.so

#dlib
SOURCES += /home/bjw/git/dlib/dlib/all/source.cpp    #源碼 
INCLUDEPATH += /home/bjw/git/dlib             #頭文件
LIBS += -L/usr/lib -lpthread -lX11

注意根據自己情況配置。

qt c++代碼:

#include <dlib/opencv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.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>
#include <time.h>
#include <iostream>
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("/home/bjw/QT_Project/dlib_landmark/dlib_landmark/shape_predictor_68_face_landmarks.dat") >> pose_model;

        // Grab and process frames until the main window is closed by the user.
        while(cv::waitKey(1) != 27)
        {

            // Grab a frame
            cv::Mat temp;
            if (!cap.read(temp))
            {
                break;
            }

            // Turn OpenCV's Mat into something dlib can deal with.  Note that this just
            // wraps the Mat object, it doesn't copy anything.  So cimg is only valid as
            // long as temp is valid.  Also don't do anything to temp that would cause it
            // to reallocate the memory which stores the image as that will make cimg
            // contain dangling pointers.  This basically means you shouldn't modify temp
            // while using cimg.
            cv_image<bgr_pixel> cimg(temp);

            // Detect faces

            std::vector<rectangle> faces = detector(cimg);
            time_t start = clock();
            // 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
            //            win.clear_overlay();
            //            win.set_image(cimg);
            //            win.add_overlay(render_face_detections(shapes));
            //Display it all on the screen
            imshow("Dlib特徵點", temp);
            time_t end = clock();
            double total_time = (double)(end-start)/CLOCKS_PER_SEC*1000;
            cout<<"total_time:"<<total_time<<"ms"<<endl;
        }
    }
    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;
    }
}

結論:

(1)我測試了一下耗時,主要還是人臉檢測上,而且不是很準確,人臉檢測40ms左右,而68個特徵點檢測只有幾毫秒。

(2)不直達爲啥開了cuda加速,速度根本沒變化,希望知道的朋友告知一聲。

 

 

 

 

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