dlib加速设置

参考连接:https://www.learnopencv.com/speeding-up-dlib-facial-landmark-detector/

1.编译dlib的时候选择ss2,ss4,和asx的选项。
2.在release的模式下编译。
3.修改dlib/image_processing/frontal_face_detector.h中的一个数值,最小为2,数值越小,识别的到的脸也就越大。
4.通过小图像来进行人脸的检测。
5。每个几帧再进行图像的人脸检测。

#define FACE_DOWNSAMPLE_RATIO 2
#define SKIP_FRAMES 2
#include <dlib/opencv.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.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>

using namespace dlib;
using namespace std;
//using namespace cv;

int main()
{
    try
    {
        cv::VideoCapture cap(0);
        cap.set(CV_CAP_PROP_FRAME_WIDTH, 1920);
        cap.set(CV_CAP_PROP_FRAME_HEIGHT, 1080);
        if (!cap.isOpened())
        {
            cerr << "Unable to connect to camera" << endl;
            return 1;
        }

        image_window win;
int count = 0;
        // 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(!win.is_closed())
        {
            // Grab a frame

            std::vector<rectangle> faces;
            cv::Mat temp;
            cv::Mat temp_small, temp_display;
            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::resize(temp, temp_small, cv::Size(), 1.0 / FACE_DOWNSAMPLE_RATIO, 1.0 / FACE_DOWNSAMPLE_RATIO);

            // Change to dlib's image format. No memory is copied.
            cv_image<bgr_pixel> cimg_small(temp_small);
            cv_image<bgr_pixel> cimg(temp);

            // Detect faces 


            clock_t   start, finish;

            start = clock();

            //if ( count % SKIP_FRAMES == 0 )
//{
            faces = detector(cimg_small);
//}

            finish = clock();

            printf("%f seconds\n", (double)(finish - start) / CLOCKS_PER_SEC);
            // Find the pose of each face.
            std::vector<full_object_detection> shapes;
            for (unsigned long i = 0; i < faces.size(); ++i)
            {
                rectangle r(
                    (long)(faces[i].left() * FACE_DOWNSAMPLE_RATIO),
                    (long)(faces[i].top() * FACE_DOWNSAMPLE_RATIO),
                    (long)(faces[i].right() * FACE_DOWNSAMPLE_RATIO),
                    (long)(faces[i].bottom() * FACE_DOWNSAMPLE_RATIO)
                );
                ;
                shapes.push_back(pose_model(cimg, r));
            }


            // Display it all on the screen
            win.clear_overlay();
            win.set_image(cimg);
            win.add_overlay(render_face_detections(shapes));
            count++;
        }

    }
    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;
    }
}

猜你喜欢

转载自blog.csdn.net/bookwormsmallman/article/details/81192343