Tesseract在Xcode上运行错误

时间:2014-01-23 04:44:27

标签: ios objective-c xcode tesseract

我一直在Xcode上测试Tesseract。我按照访问http://lois.di-qual.net/blog/install-and-use-tesseract-on-ios-with-tesseract-ios/的说明进行测试。但问题是,当我运行程序时,我在控制台上遇到以下错误:

Error opening data file /Users/mdriduanulislam/Library/Application Support/iPhone 
Simulator/7.0/Applications/0ABCEAB3-3793-44C9-8914-
A99BB6B4EF9F/Documents/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!`

我在StackOveflow上遇到过1个问题,但答案并不令人满意。有人请告诉我为什么会出现问题以及问题的可能解决方案。请等待正确的答案。< / p>

3 个答案:

答案 0 :(得分:5)

因为您的文档文件夹不包含语言文件。使用下面的代码保存添加到文档文件夹中的语言文件。在启动tesseract之前调用此方法 Tesseract * tesseract = [[Tesseract alloc] initWithDataPath:@“tessdata”语言:@“eng”];

- (void)storeLanguageFile {

        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *path = [docsDirectory stringByAppendingPathComponent:@"/tessdata/eng.traineddata"];
        if(![fileManager fileExistsAtPath:path])
        {
            NSData *data = [NSData dataWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/tessdata/eng.traineddata"]];
            NSError *error;
            [[NSFileManager defaultManager] createDirectoryAtPath:[docsDirectory stringByAppendingPathComponent:@"/tessdata"] withIntermediateDirectories:YES attributes:nil error:&error];
            [data writeToFile:path atomically:YES];
        }
}

- (NSString *)scanImage:(UIImage *)image {

        Tesseract *tesseract = [[Tesseract alloc] initWithDataPath:@"/tessdata" language:@"eng"];

        [tesseract setVariableValue:@"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" forKey:@"tessedit_char_whitelist"];
        [tesseract setVariableValue:@".,:;'" forKey:@"tessedit_char_blacklist"];

        if (image) {
            [tesseract setImage:image];
            [tesseract setRect:CGRectMake(0, point.y- 25, image.size.width, 50)];
            [tesseract recognize];
            return [tesseract recognizedText];
        }
        return nil;
    }

答案 1 :(得分:0)

经过几天寻找解决方案后,所提出的解决方案都没有对我有用,因为我在xcode中使用了客观的C ++。但是经过大量的实验,对于仍然需要解决的人来说,解决方案是1-liner(如果使用TessBaseAPI),在api.init(...)添加之前 G8Tesseract * tesseract = [[G8Tesseract alloc] initWithLanguage:@“eng”]; 这神奇地摆脱了TESSDATA_PREFIX错误

答案 2 :(得分:-1)

将TESSDATA_PREFIX添加到系统变量后,尝试重新启动PC。我正在运行Windows 10以及为我修复此错误的原因。