我正在尝试为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];
答案 0 :(得分: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_predictor_68_face_landmarks.dat添加为Project-&gt; Target-&gt; Build Phrases-> gt复制包资源并检查复制按钮。