Bhumi Goklani
Bhumi Goklani

Reputation: 613

Tesseract ios SDK Error opening data file /tessdata/eng.traineddata

I am developing an app with openCV and Tesseract framework. It was working well with "NO 64 bit support" but apple requires 64-bit support in every build now onwards. So i have updated the tesseract framework to

pod 'TesseractOCRiOS', '3.4.0'

in my project. Now project runs well with all devices. But when i scan any image, i always get below errors:

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!

I have gone through each link on google with no success. I can make sure that i have "tessdata" folder added as folder reference in my project.

Upvotes: 3

Views: 1748

Answers (1)

Bhumi Goklani
Bhumi Goklani

Reputation: 613

Anyways, i got the solution. Just update the pod to latest version so your pod file should look like

pod 'TesseractOCRiOS', '4.0.0'

It automaticaly manages "tessdata" if not found in document directory. You just have to make sure it exists in your application bundle.

Then you can initialize it like

_tesseract = new tesseract::TessBaseAPI();
_tesseract->Init([[self pathToLangugeFIle] cStringUsingEncoding:NSUTF8StringEncoding], "eng");

The function should be like

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

Upvotes: 4

Related Questions