如何使用dlib提取面部特征

时间:2016-05-14 11:18:07

标签: python c++ dlib

我需要提取特征点并将它们转换为YAML文件。现在我的代码是:

#pragma once
#include "facial_feature_points.h"


using namespace dlib;
using namespace std;

const string model_path = "../src/shape_predictor_68_face_landmarks.dat";

int facial_feature_detector (const string& img_path) {

    try {

        frontal_face_detector detector = get_frontal_face_detector ();
        shape_predictor s_predict;
        deserialize (model_path) >> s_predict;

        image_window win_a, win_b;

        array2d<rgb_pixel> img;
        load_image (img, img_path);
        pyramid_up (img);

        std::vector<rectangle> dets = detector (img);
        cout << "Number of faces detected: " << dets.size () << endl;
        std::vector<full_object_detection> shapes;

        std::vector <double> landmarks;

        for (unsigned long j = 0; j < dets.size (); ++j) {
            full_object_detection shape = s_predict (img, dets[j]);
            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(shape);
            double 
        }


        win_a.clear_overlay ();
        win_a.set_image (img);
        win_a.add_overlay (render_face_detections (shapes));

        dlib::array <array2d <rgb_pixel> > face_chips;
        extract_image_chips (img, get_face_chip_details(shapes), face_chips);
        win_b.set_image (tile_images (face_chips));



        // disp_landmarks (img_path, dets, shapes);



    } catch (exception& e) {
        cout << "\n[!] Exception thrown:  " << e.what () << endl;
    }

    return 0;
}

int shape_to_np (const full_object_detection& shape) {

    for (int i=0; i<68; ++i )

    return 0;
}


// TODO: resolve the problem in compilation
/*
void disp_landmarks (const string& img, const std::vector <rectangle> dets, const std::vector <full_object_detection> shapes) {
    image_window win;
    win.clear_overlay ();
    win.set_image (img);

    for (int i=0; i<shapes.size(); ++i)
        win.add_overlay (shapes[i]);

    win.add_overlay (dets);
    // hit_enter_to_continue ();
    win.wait_until_closed ();
}
*/

我需要将地标转换为单独的68分。我找到了转换的这个python代码:

def _shape_to_np(shape):
    xy = []
    for i in range(68):
        xy.append((shape.part(i).x, shape.part(i).y,))
    xy = np.asarray(xy, dtype='float32')
    return xy

但我不知道如何在c ++中重新创建它。请帮忙 。如果您可以解释这个python代码正在做什么,它也会工作。我不熟悉python,代码对我来说有点奇怪。

0 个答案:

没有答案