Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tesseract running error on Xcode

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.

like image 392
AppleLover Avatar asked Jan 23 '14 04:01

AppleLover


1 Answers

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;
    }
like image 107
Vaisakh Avatar answered Nov 06 '22 02:11

Vaisakh