2016-04-30 27 views
3

我正在尝试为iOS应用程序实现dlib的人脸标志检测。 在DLIB的例子,他们初始化shape_predictor这样:如何在Objective-C中使用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

dlib库是一个C++库,我不相信你可以直接在iOS中使用,因为iOS使用Objective-C。我认为你需要转换为Objective-C++。我从来没有尝试过,但这些链接可能会有所帮助。 [objective-c-C++-and-objective-C++](http://philjordan.eu/article/mixing-objective-c-c++-and-objective-c++)[Interoperating Between C++ and Objective-C](http: //www.drdobbs.com/cpp/interoperating-between-c-and-objective-c/240165502) –

+0

[面向目标c和dlib的Facelandmarking](https://github.com/shaileshh/Mirror)小型演示项目使用目标C n dlib进行人脸检测。 – Shailesh

回答

3

假设你已经设置了DLIB库正确的项目(并包括了源程序e.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; 

注:在项目中,我加入shape_predictor_68_face_landmarks.dat在项目 - 捆绑资源>靶>构建Phrases->复制包资源,并检查复制按钮。

+0

嘿阿门斯基!谢谢你这个伟大的答案!你可以请给我发电子邮件:[email protected],我想做这个真棒开源git项目(关于dlib和iOS),但我陷入了一对夫妇的障碍。谢谢你,先生! –