I have properly included the eng.traineddata file to the project. It was working fine. All of a sudden it started giving me the following error and crashes.
Error opening data file /var/mobile/Applications/B36E2682-933F-4B12-9B32-4C3F640BE19E/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!
Code I used
- (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;
}
and
- (NSString*) OCRImage:(UIImage*)src{
// init the tesseract engine.
tesseract::TessBaseAPI *tesseract = new tesseract::TessBaseAPI();
tesseract->Init([[self pathToLangugeFIle] cStringUsingEncoding:NSUTF8StringEncoding], "eng");
tesseract->SetVariable("tessedit_char_whitelist", ":-0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
//Pass the UIIMage to cvmat and pass the sequence of pixel to tesseract
cv::Mat toOCR=[src CVGrayscaleMat];
NSLog(@"%d", toOCR.channels());
tesseract->SetImage((uchar*)toOCR.data, toOCR.size().width, toOCR.size().height
, toOCR.channels(), toOCR.step1());
tesseract->Recognize(NULL);
char* utf8Text = tesseract->GetUTF8Text();
return [NSString stringWithUTF8String:utf8Text];
}