3

我正在尝试为 iOS 应用程序实现 dlib 的面部标志检测。在 dlib 的示例中,他们这样初始化 shape_predictor:

// And we also need a shape_predictor.  This is the tool that will predict face
// landmark positions given an image and face bounding box.  Here we are just
// loading the model from the shape_predictor_68_face_landmarks.dat file you gave
// as a command line argument.
    shape_predictor sp;
    deserialize(argv[1]) >> sp;

我正在尝试在 Objective-C 中做同样的事情并且已经做到了这一点:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"shape_predictor_68_face_landmarks" ofType:@"dat"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];

执行以下操作会给我一个错误“接收器类型'dlib :: shape_predictor'不是Objective-C类”

sp = [dlib::shape_predictor deserialize:myData];
4

1 回答 1

4

假设您已在项目中正确设置 dlib 库(并包含 source.cpp 文件),您需要一个 .mm 文件(Objective-C++),您可以在其中执行以下操作:

#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
#include <dlib/opencv.h>
#include <dlib/image_io.h>

然后,您可以加载所需的 dlib 类并使用常规 C++ 语法使用它们。这是一个例子:

dlib::frontal_face_detector detector = dlib::get_frontal_face_detector();
dlib::shape_predictor shapePredictor;

NSBundle *mainBundle = [NSBundle mainBundle];
NSString *landmarkdat = [[NSString alloc] initWithFormat:@"%@/%s", mainBundle.bundlePath,"shape_predictor_68_face_landmarks.dat"];
dlib::deserialize(landmarkdat.UTF8String) >> shapePredictor;

注意:在我的项目中,我在 Project->Target->Build Phrases->Copy bundle resources 中添加了 shape_predictor_68_face_landmarks.dat 作为捆绑资源并选中了复制按钮。

于 2016-05-20T14:39:39.300 回答