【问题标题】:Saving detected facial landmarks into a file将检测到的面部标志保存到文件中
【发布时间】:2017-04-24 03:16:47
【问题描述】:

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

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

但它只是将第一个和第二个地标点 (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;
}

}

【问题讨论】:

    标签: c++ opencv dlib


    【解决方案1】:

    我错了还是你想保存所有地标时只有:
    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 声明。

    【讨论】:

    • 我以 shape.part(0).x() 和 shape.part(0).y() 为例,当我列出地标(例如从 0 到 10 )时,只有第一个和第二个检测到的地标被保存。 ofstream 输出文件;已经在 for 循环中
    • 您是否尝试过其他类型的输出到文件,例如FILE
    • 我刚刚尝试了 .csv 输出,但只保存前两个值是一样的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 2018-04-17
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多