如何在Objective-C中使用dlib的shape_predictor?
How to use dlib's shape_predictor in Objective-C?
我正在尝试为 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];
执行以下操作会出现 "Receiver type 'dlib::shape_predictor' is not an Objective-C class"
错误
sp = [dlib::shape_predictor deserialize:myData];
假设您已在项目中正确设置 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 作为 bundle 资源,并选中了复制按钮。
我正在尝试为 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];
执行以下操作会出现 "Receiver type 'dlib::shape_predictor' is not an Objective-C class"
错误sp = [dlib::shape_predictor deserialize:myData];
假设您已在项目中正确设置 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 作为 bundle 资源,并选中了复制按钮。