2014-02-12 95 views
1

我使用下面的方法来添加的Tesseract在我的项目Tessdata没有在文件目录复制

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // 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); 

     // init the tesseract engine. 
     tesseract = new tesseract::TessBaseAPI(); 
     tesseract->Init([dataPath cStringUsingEncoding:NSUTF8StringEncoding], "eng"); 
    } 
    return self; 
} 

训练的数据得到以下错误

Error opening data file /Users/frf/Library/Application Support/ 
iPhone Simulator/6.1/Applications/686F83C8-526C-47FB-9F02- 
E44FBE69F60B/Documents/tessdata/eng.traineddata 

tessdata不被复制到文档目录中。我该怎么办 ?

请建议编辑我上面的代码......

+0

你为什么不检查复制成功也许在这里得到错误'[fileManager copyItemAtPath:tessdataPath toPath:dataPath error:NULL];'?错误对象不是为了好玩而传递的,所以请使用它们! – Volker

回答

2

我假设tessdata目录不存在。 在用户/文档目录中。

+0

是的,它不会。我必须手动创建它吗? – SwapnilPopat

+0

是的,使用'NSFileManager'你必须创建目录,如果它不存在。所以首先检查它是否存在,然后在必要时创建。 – Volker

1

它是因为你的文档文件夹不包含语言文件。您必须保存添加到文档文件夹中的语言文件。在启动tesseract之前保存该文件。Tesseract * tesseract = [[Tesseract alloc] initWithDataPath:@“tessdata”language:@“eng”];请参考答案here

2

这段代码工作对我来说...

if (tessdataPath) { 

       [[NSFileManager defaultManager] createDirectoryAtPath:[documentPath stringByAppendingPathComponent:@"/tessdata"] withIntermediateDirectories:YES attributes:nil error:nil]; 
       [fileManager copyItemAtPath:tessdataPath toPath:dataPath error:NULL]; 
      } 

谢谢大家...... @沃尔克@Vaisakh