简体   繁体   中英

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. The problem now is 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.

C++ can't confirm the file exists nor can opencv load it. I am new to ios and am aware that the myapp.app could be a compressed dir like the android .apk dir.

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. 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. I really don't want to have to mix in Objective-C into my lib just for file paths.

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. First add the .xml files to your Xcode project and use the following lines of code for loading the haar-cascade. Please note that should not contain the extension .xml (Example: For face_frontal.xml file, your cascade name should be 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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