我需要提取特征点并将它们转换为 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 不是很流利,而且代码对我来说有点奇怪。