我正在尝试从在 VisualStudio 项目中运行良好的教程中读取“classifications.xml”和“images.xml”。现在如何将我的 Swift XCode 项目中的 C++ 库指向这些文件?将它们拖放到项目中不起作用。
这是原始代码。如何将 cv::Filestorage 类指向我项目中的文件?现在它返回一个错误
cv::Mat matClassificationInts; // we will read the classification numbers into this variable as though it is a vector
cv::FileStorage fsClassifications("classifications.xml", cv::FileStorage::READ); // open the classifications file
if (fsClassifications.isOpened() == false) { // if the file was not opened successfully
std::cout << "error, unable to open training classifications file, exiting program\n\n"; // show error message
return(0); // and exit program
}
fsClassifications["classifications"] >> matClassificationInts; // read classifications section into Mat classifications variable
fsClassifications.release(); // close the classifications file
最佳答案
当您将它们拖放到您的项目中时,它们最终会出现在应用程序包中。
可以使用以下 Swift 代码找到这些文件的文件名:
Bundle.main.path(forResource: "classifications", ofType: "xml")
我不知道你的代码加载了什么,但如果你只是使用“classifications.xml”,它会默认到当前文件夹,这不是 iOS 上允许的资源路径。
如果您尝试与 C++ 进行互操作,那么使用 Objective-C 可能会更容易。
类似的东西;
NSString *path = [[NSBundle main] pathForResource:@"classification" ofType:@"xml"];
char *pathCString = [path cStringUsingEncoding:NSUTF8StringEncoding];
关于ios - OpenCV 在 Swift IOS 项目中读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43504172/