简体   繁体   English

IOS上的OpenCV来自C ++的cascade xml文件路径问题

[英]OpenCV on IOS haar cascade xml file path issues from C++

So I have my code in C++ and I am able to run it fine on linux/OSX/Android. 所以我的代码用C ++编写,我可以在linux / OSX / Android上运行得很好。 The problem now is ios. 现在的问题是ios。 I am simply trying to load the haar cascade xml files via full path "/foo/blah/myapp.app/haar.xml" and having one heck of a time. 我只是试图通过完整路径“/foo/blah/myapp.app/haar.xml”加载haar cascade xml文件并且只有一点时间。

C++ can't confirm the file exists nor can opencv load it. C ++无法确认文件是否存在,也无法opencv加载它。 I am new to ios and am aware that the myapp.app could be a compressed dir like the android .apk dir. 我是ios的新手,我知道myapp.app可能是一个像android .apk目录这样的压缩目录。

I have confirmed that I'm looking in the right place....or at least I hope I have! 我已经确认我正在寻找正确的地方....或者至少我希望我有! to check i use getcwd in c++ to get running directory and I confirm that with the file path to the xml file. 检查我在c ++中使用getcwd获取正在运行的目录,并确认使用xml文件的文件路径。 It looks legit! 它看起来合法!

What can I do? 我能做什么? The nice part about using C++ for the lib is that I can use one code base for desktop/android/ios. 关于为lib使用C ++的好处是我可以为desktop / android / ios使用一个代码库。 I really don't want to have to mix in Objective-C into my lib just for file paths. 我真的不想将Objective-C混合到我的lib中,仅用于文件路径。

Thanks in advance! 提前致谢!

The code I'm using is the following: 我正在使用的代码如下:

    std::ifstream fexists(face_haar_path.c_str());
    if(!fexists.good()) {
        std::cout << "Could not find any facehaar.xml" << std::endl;
        return NO_FACE_HAAR_FILE;
    }

    face_haar.load(face_haar_path); //cv::CascadeClassifier face_haar

Adding reference to file location in iOS apps is quite different. 在iOS应用程序中添加对文件位置的引用是完全不同的。 First add the .xml files to your Xcode project and use the following lines of code for loading the haar-cascade. 首先将.xml文件添加到Xcode项目中,然后使用以下代码行加载haar-cascade。 Please note that should not contain the extension .xml (Example: For face_frontal.xml file, your cascade name should be face_frontal ) 请注意,不应包含扩展名.xml (示例:对于face_frontal.xml文件,您的级联名称应为face_frontal

NSString *faceCascadePath = [[NSBundle mainBundle]
                             pathForResource:@"<your_cascade_name>"
                             ofType:@"xml"];
const CFIndex CASCADE_NAME_LEN = 2048;
char *CASCADE_NAME = (char *) malloc(CASCADE_NAME_LEN);
CFStringGetFileSystemRepresentation( (CFStringRef)faceCascadePath,
                                    CASCADE_NAME,
                                    CASCADE_NAME_LEN);
if(!face_cascade.load(CASCADE_NAME)) {
    cout << "Unable to load the face detector!!!!" << endl;
    exit(-1);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM