ios - Tesseract ios SDK 错误打开数据文件/tessdata/eng.traineddata

标签 ios ocr tesseract scanning

我正在使用 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”文件夹作为文件夹引用。

最佳答案

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

关于ios - Tesseract ios SDK 错误打开数据文件/tessdata/eng.traineddata,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29445937/

相关文章:

objective-c - Instagram直接打开UTI

java - TessBaseAPI 无法解析符号 tess-two OCR Android

java.lang.NoClassDefFoundError : net/sourceforge/tess4j/TesseractException

python-3.x - 将 image_to_osd 方法与 pytesseract 结合使用时出错

ocr - 具有表格或行的文档的 Tesseract OCR 文本顺序

ios - 如何在 iOS 中触发 uilocalnotification 时(而不是在单击时)?

ios - 从本地通知启动关闭的 iOS 应用程序

android - android中文手写识别库

machine-learning - 如何获取汉字的分解

objective-c - 如何阻止 UIWebView 播放 mp3?