将检测到的面部标记保存到文件中

时间:2016-12-08 19:02:32

标签: c++ opencv dlib

我尝试从网络摄像头或视频导入面部地标点

将dlib用于文件。我可以在终端上显示所有检测到的地标

但它只将第一个和第二个标志性的ponits(x,y)保存到

输出文件,而不是将所有检测到的地标保存到

输出文件

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

    frontal_face_detector detector = get_frontal_face_detector();
    shape_predictor pose_model;
    deserialize("shape_predictor_68_face_landmarks.dat") >> pose_model;


    while(!win.is_closed())
    {
        // Grab a frame
        cv::Mat temp;
        cap >> temp;


        cv_image<bgr_pixel> cimg(temp);


        std::vector<rectangle> faces = detector(cimg);

        std::vector<full_object_detection> shapes;
        for (unsigned long i = 0; i < faces.size(); ++i)
      {  

               full_object_detection shape = pose_model(cimg, faces[i]);

        cout << "number of parts: "<< shape.num_parts() << endl;
            cout << "pixel position of first part:  " << shape.part(0) << endl;
   cout << "pixel position of second part: " << shape.part(1) << endl;
  shapes.push_back(pose_model(cimg, faces[i]));


   const full_object_detection& d = shapes[0];
           ofstream outputfile;
           outputfile.open("data1.txt");


                outputfile<< shape.part(0).x() << " " << shape.part(0).y() << endl;
                outputfile<< shape.part(1).x() << " " << shape.part(1).y() << endl;


              }

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

}

1 个答案:

答案 0 :(得分:0)

我错了,或者你只想保存所有地标:
ofstream outputfile; outputfile.open("data1.txt"); outputfile<< shape.part(0).x() << " " << shape.part(0).y() << endl; outputfile<< shape.part(1).x() << " " << shape.part(1).y() << endl;
甚至没有正确关闭文件。试试for声明。