OpenCV级联分类器加载错误iOS [英] OpenCV Cascade Classifier load error iOS
问题描述
目前我正在尝试使用 OpenCV 级联分类器来检测我的 iOS 应用程序中的人脸.问题是,当我去加载分类器时,它失败了,因为haarcascade_frontalface_alt.xml"的路径名不正确.
Currently I am trying to using an OpenCV Cascade Classifier to detect faces in my iOS app. The problem is that when I go to load the classifier, it fails because the pathname to the "haarcascade_frontalface_alt.xml" isn't correct.
这是我的代码:
cv::String face_cascade_name = "haarcascade_frontalface_alt.xml";
void detectFaces(cv::Mat frame){
void detectFaces(cv::Mat frame){
cv::CascadeClassifier face_cascade;
if (face_cascade.load(face_cascade_name)){
printf("Load complete");
}else{
printf("Load error");
}
std::vector<cv::Rect> faces;
std::vector<cv::Mat> rgbChannels(3);
cv::split(frame, rgbChannels); // Making the frame gray scale
cv::Mat frame_gray = rgbChannels[2];
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE|CV_HAAR_FIND_BIGGEST_OBJECT, cv::Size(150,150));
if(faces.size()> 0){
printf("TRUE");
}
}
由于这是一个iOS项目我不知道如何在框架中找到所需的xml文件的路径.将级联分类器加载到 iOS 项目的正确方法是什么?或者,包含正确文件名以使其正确查看的正确方法是什么?
Since this is an iOS project I don't know how to find the path of the required xml file in the framework. What is the proper way to load a Cascade Classifier into an iOS project? Or, what is the correct way to include the right filename so that it sees it properly?
任何帮助将不胜感激.
推荐答案
在 iOS 中,您需要在 app bunnle 中定位文件路径.像这样:
In iOS, you need to locate the file path within the app bunnle. Something like this:
// get main app bundle
NSBundle * appBundle = [NSBundle mainBundle];
// constant file name
NSString * cascadeName = @"haarcascade_frontalface_alt";
NSString * cascadeType = @"xml";
// get file path in bundle
NSString * cascadePathInBundle = [appBundle pathForResource: cascadeName ofType: cascadeType];
// convert NSString to std::string
std::string cascadePath([cascadePathInBundle UTF8String]);
// load cascade
if (face_cascade.load(cascadePath)){
printf("Load complete");
}else{
printf("Load error");
}
这篇关于OpenCV级联分类器加载错误iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!