Question

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:

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.

Was it helpful?

Solution

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"];

- (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;
    }

OTHER TIPS

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top