【问题标题】:Tessdata is not being copied in document directoryTessdata 没有被复制到文档目录中
【发布时间】:2014-03-10 10:33:58
【问题描述】:

我正在使用以下方法在我的项目中添加 tesseract 训练的数据

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // 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);

        // init the tesseract engine.
        tesseract = new tesseract::TessBaseAPI();
        tesseract->Init([dataPath cStringUsingEncoding:NSUTF8StringEncoding], "eng");
    }
    return self;
}

收到以下错误

Error opening data file /Users/frf/Library/Application Support/
iPhone Simulator/6.1/Applications/686F83C8-526C-47FB-9F02-
E44FBE69F60B/Documents/tessdata/eng.traineddata

tessdata 没有被复制到文档目录中。我该怎么办?

请建议编辑我上面的代码...

【问题讨论】:

  • 你为什么不检查复制是否成功,也许会在这里得到错误[fileManager copyItemAtPath:tessdataPath toPath:dataPath error:NULL];?传递错误对象不是为了好玩,所以使用它们!

标签: ios tesseract nsfilemanager nsdocumentdirectory


【解决方案1】:

我假设 tessdata 目录不存在。 在用户/文档目录中。

【讨论】:

  • 是的,使用NSFileManager如果目录不存在,您必须创建该目录。所以首先检查它是否存在,然后根据需要创建。
【解决方案2】:

这是因为您的文件夹不包含语言文件。您必须将捆绑中添加的语言文件保存到文档文件夹。在启动 tesseract 之前保存 Tesseract* tesseract = [[Tesseract alloc] initWithDataPath:@"tessdata" language:@"eng"];请参考答案here

【讨论】:

    【解决方案3】:

    这段代码对我有用...

    if (tessdataPath) {
    
                    [[NSFileManager defaultManager] createDirectoryAtPath:[documentPath stringByAppendingPathComponent:@"/tessdata"] withIntermediateDirectories:YES attributes:nil error:nil];
                    [fileManager copyItemAtPath:tessdataPath toPath:dataPath error:NULL];
                }
    

    谢谢大家......@Volker @Vaisakh

    【讨论】:

      猜你喜欢
      • 2013-10-26
      • 2010-12-03
      • 1970-01-01
      • 1970-01-01
      • 2011-01-30
      • 2016-02-27
      • 1970-01-01
      • 2015-03-28
      • 1970-01-01
      相关资源
      最近更新 更多