简体   繁体   English

如何在使用Dlib的opencv C ++中的地标提取中使用“ shape_predictor_68_face_landmarks.dat”

[英]how to use “shape_predictor_68_face_landmarks.dat” in landmark extraction in opencv C++ using Dlib

I'm trying to run the following code and detect the facial landmarks of the frames which are taken from webcam. 我正在尝试运行以下代码,并检测从网络摄像头获取的帧的面部标志。

#include <dlib/opencv.h>
#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>

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(!win.is_closed())
        {
            // Grab a frame
            cv::Mat temp;
            cap >> temp;
            // 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);
            // 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]));

            // Display it all on the screen
            win.clear_overlay();
            win.set_image(cimg);
            win.add_overlay(render_face_detections(shapes));
        }
    }
    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;
    }
}

But when executing this .cpp file it gives the console output like this. 但是,当执行此.cpp文件时,它会提供如下控制台输出。

enter image description here 在此处输入图片说明

As in this I have download shape_predictor_68_face_landmarks.dat. 这样,我就下载了shape_predictor_68_face_landmarks.dat。 But I don't know where to add this .dat and whish directory to include. 但是我不知道在何处添加此.dat和whish目录。 Could anyone tell me how to use this shape_predictor_68_face_landmarks.dat. 谁能告诉我如何使用shape_predictor_68_face_landmarks.dat。

The error seems to be from: 错误似乎来自:

deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;

And Fix for the above would be to provide a full qualified path as: 而针对上述问题的解决方案将是提供一个完整的合格路径,如下所示:

deserialize("/full/path/to/shape_predictor_68_face_landmarks.dat") >> pose_model;

This is probably due to the reason that the C++ executable file can be ran from some build location to which this file may not be in local scope and hence it is unable to find it, The best way would be to use full qualified paths. 这可能是由于C ++可执行文件可以从某个构建位置运行的原因,而该文件可能不在本地范围内,因此无法找到它。最好的方法是使用完全限定的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM