【发布时间】:2014-07-20 11:01:03
【问题描述】:
我已将 eng.traineddata 文件正确包含到项目中。它工作正常。突然之间,它开始给我以下错误和崩溃。
打开数据文件时出错 /var/mobile/Applications/B36E2682-933F-4B12-9B32-4C3F640BE19E/Documents/tessdata/eng.traineddata 请确保 TESSDATA_PREFIX 环境变量设置为 “tessdata”目录的父目录。加载失败 语言“eng”Tesseract 无法加载任何语言!
我使用的代码
- (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;
}
和
- (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];
}
【问题讨论】:
标签: ios objective-c tesseract objective-c++