Tesseract 在 Xcode 上运行错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21299571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:34:46  来源:igfitidea点击:

Tesseract running error on Xcode

iosobjective-cxcodetesseract

提问by AppleLover

I have been testing Tesseract on Xcode.I followed instructions from Visit http://lois.di-qual.net/blog/install-and-use-tesseract-on-ios-with-tesseract-ios/.But the problem is that when i run the program i get the following errors on console:

我一直在 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!`

I have 1 problem which was been asked on StackOveflow but the answer was not satisfactory.Can someone please tell me why the problem is happening and the possible solution for the problem please.I'm eagerly waiting for right answer please.

我有 1 个在 StackOveflow 上被问到的问题,但答案并不令人满意。有人可以告诉我为什么会发生问题以及问题的可能解决方案。我急切地等待正确的答案。

采纳答案by Vaisakh

Its because your document folder does not contain language file. Use the code below to save language file which added in bundle to document folder. call this method before you initiate tesseract Tesseract* tesseract = [[Tesseract alloc] initWithDataPath:@"tessdata" language:@"eng"];

这是因为您的文档文件夹不包含语言文件。使用下面的代码将添加到文件文件夹中的语言文件保存到文档文件夹中。在启动tesseract之前调用这个方法Tesseract* tesseract = [[Tesseract alloc] initWithDataPath:@"tessdata" language:@"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;
    }

回答by LeechCoder

After days of searching for the solution, none of the proposed solutions worked for me because I am using objective C++ in xcode. But after tons of experimentations, for anyone that still need this solved, the solution is a 1-liner (if ure using TessBaseAPI), before api.init(...) add G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:@"eng"]; This magically gets rid of the TESSDATA_PREFIX error

经过几天的搜索解决方案,没有一个建议的解决方案对我有用,因为我在 xcode 中使用了客观的 C++。但是经过大量实验,对于仍然需要解决此问题的任何人,解决方案是 1-liner(如果使用 TessBaseAPI),在 api.init(...) 之前添加 G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:@ "eng"]; 这神奇地摆脱了 TESSDATA_PREFIX 错误

回答by Pikamander2

After adding TESSDATA_PREFIX to your system variables, try restarting your PC. I'm running Windows 10 and that's what fixed this error for me.

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