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加速,速度根本没变化,希望知道的朋友告知一声。

 

 

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值