ubuntu16.04 qt5上利用dlib库实现Face Landmark Detection

Face Landmark Detection在疲劳检测和眼球跟踪等有着巨大作用。 

ubuntu16.04 qt5上利用dlib库实现Face Landmark Detection_第1张图片

安装步骤:

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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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 cimg(temp);

            // Detect faces

            std::vector faces = detector(cimg);
            time_t start = clock();
            // Find the pose of each face.
            std::vector 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:"<

结论:

(1)我测试了一下耗时,主要还是人脸检测上,而且不是很准确,人脸检测40ms左右,而68个特征点检测只有几毫秒。

(2)不直达为啥开了cuda加速,速度根本没变化,希望知道的朋友告知一声。

 

 

 

 

你可能感兴趣的:(人工智能AI)