Toshan
Toshan

Reputation: 23

Face Landmark Detection algorithm, output image disappear C++, Dlib

I'm trying to extract all the facial landmarks of a face image and save that image in my directory. In my case I have to follow these steps.

  1. Input the image
  2. Extract the face ROI
  3. Then using the Dlib extract the face landmarks
  4. Add a overlay to the image
  5. Save that image in my directory

I have tried this c++ code it gives the output which I needed. But after displaying overlayed image it disappears very quickly. Could any one tell me whats the wrong with this code.

#include <dlib\opencv\cv_image.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;
using namespace cv;

int main()
{
    try
    {

        cv::Mat src1;
        src1 = imread("obama.jpg", CV_LOAD_IMAGE_COLOR);

        //image_window win, win_faces;
        image_window win;
        Mat atom_image = Mat::zeros(500, 500, CV_8UC3);


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

            //cv::Mat temp;
            //temp = src1;

            cv_image<bgr_pixel> cimg(src1);
            cv_image<bgr_pixel> black(atom_image);
            // Detect faces 
            std::vector<rectangle> faces = detector(cimg);

            cout << "Number of faces detected: " << faces.size() << endl;

            // Find the pose of each face.
            std::vector<full_object_detection> shapes;
            //for (int i = 0; i < faces.size(); i++) {
                //shapes.push_back(pose_model(cimg, faces[i]));


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


                shapes.push_back(pose_model(cimg, faces[0]));


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


                cout << "pixel position of first part:  " << shape.part(2) << endl;

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

Upvotes: 0

Views: 760

Answers (1)

Ankur Jain
Ankur Jain

Reputation: 507

You are not holding the image window. A simple way to do would be

win.set_image(cimg);
win.add_overlay(render_face_detections(shapes));
waitKey(0);

Also, I don't see any code where you are saving the output to current directory as you mentioned in the question. You may look at imwrite.

Upvotes: 1

Related Questions