Tesseract ios SDK 打开数据文件时出错 /tessdata/eng.traineddata

Tesseract ios SDK Error opening data file /tessdata/eng.traineddata

我正在使用 openCV 和 Tesseract 框架开发一个应用程序。 它在 "NO 64 bit support" 上运行良好,但苹果现在要求在每个版本中都支持 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!

我在 google 上完成了每个 link,但都没有成功。 我可以确保在我的项目中添加了 "tessdata" 文件夹作为文件夹引用。

无论如何,我找到了解决方案。 只需将 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;
}