Tesseract iOS SDK出现错误,无法打开数据文件/tessdata/eng.traineddata。

3
我正在使用openCV和Tesseract框架开发一个应用程序。之前它可以良好地运行,只是没有64位支持。但现在苹果要求每个构建都需要支持64位。所以我在我的项目中更新了Tesseract框架:

pod 'TesseractOCRiOS', '3.4.0'

现在项目在所有设备上都可以正常运行。但是当我扫描任何图像时,我总是收到以下错误信息:

Error opening data file /tessdata/eng.traineddata Please make sure the TESSDATA_PREFIX environment variable is set to the parent directory of your "tessdata" directory. Failed loading language 'eng' Tesseract couldn't load any languages!

我已经查阅了谷歌上的每个链接,但都没有成功。我可以确定将“tessdata”文件夹作为文件夹引用添加到了我的项目中。
1个回答

4
无论如何,我找到了解决方案。 只需将pod更新到最新版本即可,你的pod文件应该如下所示

pod 'TesseractOCRiOS', '4.0.0'

如果在文档目录中未找到“tessdata”,它会自动管理。您只需要确保它存在于应用程序捆绑包中。
然后您可以像这样进行初始化
_tesseract = new tesseract::TessBaseAPI();
_tesseract->Init([[self pathToLangugeFIle] cStringUsingEncoding:NSUTF8StringEncoding], "eng");

该函数应该如下所示:
- (NSString*) pathToLangugeFIle{

    // Set up the tessdata path. This is included in the application bundle
    // but is copied to the Documents directory on the first run.
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = ([documentPaths count] > 0) ? [documentPaths objectAtIndex:0] : nil;

    NSString *dataPath = [documentPath stringByAppendingPathComponent:@"tessdata"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    // If the expected store doesn't exist, copy the default store.
    if (![fileManager fileExistsAtPath:dataPath]) {
        // get the path to the app bundle (with the tessdata dir)
        NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
        NSString *tessdataPath = [bundlePath stringByAppendingPathComponent:@"tessdata"];
        if (tessdataPath) {
            [fileManager copyItemAtPath:tessdataPath toPath:dataPath error:NULL];
        }
    }

    setenv("TESSDATA_PREFIX", [[documentPath stringByAppendingString:@"/"] UTF8String], 1);

    return dataPath;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接