|
21 | 21 | NSString *tessdataFolderName = @"tessdata"; |
22 | 22 | NSString *tessdataFolderPathFromTheBundle = [[resourcePath stringByAppendingPathComponent:tessdataFolderName] stringByAppendingString:@"/"]; |
23 | 23 | NSString *debugConfigsFileName = @"debugConfigs.txt"; |
24 | | - NSString *recognitionConfigsFileName = @"recognitionConfis.txt"; |
| 24 | + NSString *recognitionConfigsFileName = @"recognitionConfigs.txt"; |
25 | 25 | NSString *tessConfigsFolderName = @"tessconfigs"; |
26 | 26 |
|
27 | 27 | void (^checkVariablesAreSetForTesseract)(G8Tesseract *tesseract) = ^(G8Tesseract *tesseract){ |
| 28 | + // these variable could be set up during the initialization |
28 | 29 | [[[tesseract variableValueForKey:kG8ParamTessdataManagerDebugLevel] should] equal:@"1"]; |
29 | 30 | [[[tesseract variableValueForKey:kG8ParamLoadSystemDawg] should] equal:@"0"]; |
30 | 31 | [[[tesseract variableValueForKey:kG8ParamLoadFreqDawg] should] equal:@"0"]; |
31 | 32 | [[[tesseract variableValueForKey:kG8ParamUserWordsSuffix] should] equal:@"user-words"]; |
32 | 33 | [[[tesseract variableValueForKey:kG8ParamUserPatternsSuffix] should] equal:@"user-patterns"]; |
33 | 34 | }; |
34 | 35 |
|
| 36 | + void (^recognizeSimpleImageWithTesseract)(G8Tesseract *tesseract) = ^(G8Tesseract *tesseract) { |
| 37 | + tesseract.image = [UIImage imageNamed:@"image_sample.jpg"]; |
| 38 | + [tesseract recognize]; |
| 39 | + |
| 40 | + [[theValue(tesseract.progress) should] equal:theValue(100)]; |
| 41 | + |
| 42 | + NSString *recognizedText = tesseract.recognizedText; |
| 43 | + [[recognizedText should] equal:@"1234567890\n\n"]; |
| 44 | + }; |
| 45 | + |
35 | 46 | context(@"nil cachesRelatedDataPath", ^{ |
36 | 47 |
|
37 | 48 | it(@"Should initialize simple", ^{ |
|
134 | 145 |
|
135 | 146 | context(@"no tessdata folder in the Caches yet", ^{ |
136 | 147 |
|
137 | | - it(@"Should simple init", ^{ |
| 148 | + it(@"Should simple init, download rus language files and reinitialize tess with them", ^{ |
138 | 149 | // proof Caches folder is empty |
139 | 150 | BOOL folderExists = [fileManager fileExistsAtPath:cachesTessDataPath]; |
140 | 151 | [[theValue(folderExists) should] beNo]; |
|
149 | 160 | [[tesseract.absoluteDataPath should] equal:cachesTessDataPath]; |
150 | 161 |
|
151 | 162 | [[theValue(doFoldersContainTheSameElements()) should] beYes]; |
| 163 | + |
| 164 | + recognizeSimpleImageWithTesseract(tesseract); |
| 165 | + |
| 166 | + // move rus langiage files to the folder created on previous steps |
| 167 | + NSString *rusTessdataSourcePath = [[resourcePath stringByAppendingPathComponent:tessdataFolderName] stringByAppendingString:@"-rus"]; |
| 168 | + NSString *destinationPath = [[cachesPath stringByAppendingPathComponent:tessdataPath] stringByAppendingPathComponent:tessdataFolderName]; |
| 169 | + |
| 170 | + NSFileManager *fileManager = [NSFileManager defaultManager]; |
| 171 | + NSError *error = nil; |
| 172 | + NSArray *contents = [fileManager contentsOfDirectoryAtPath:rusTessdataSourcePath error:&error]; |
| 173 | + for (NSString *filaName in contents) { |
| 174 | + NSString *rusFileName = [rusTessdataSourcePath stringByAppendingPathComponent:filaName]; |
| 175 | + NSString *destinationFileName = [destinationPath stringByAppendingPathComponent:filaName]; |
| 176 | + if ([fileManager fileExistsAtPath:destinationFileName isDirectory:nil]) { |
| 177 | + [fileManager removeItemAtPath:destinationFileName error:nil]; |
| 178 | + } |
| 179 | + |
| 180 | + BOOL res = [fileManager copyItemAtPath:rusFileName toPath:destinationFileName error:&error]; |
| 181 | + if (error) { |
| 182 | + NSLog(@"Error copying rus traineddata: %@", error); |
| 183 | + } |
| 184 | + assert(res == YES); |
| 185 | + } |
| 186 | + |
| 187 | + // initialize with rus now |
| 188 | + G8Tesseract *rusResseract = [[G8Tesseract alloc] initWithLanguage:@"rus" |
| 189 | + configDictionary:nil |
| 190 | + configFileNames:nil |
| 191 | + cachesRelatedDataPath:tessdataPath |
| 192 | + engineMode:G8OCREngineModeTesseractOnly]; |
| 193 | + [[rusResseract shouldNot] beNil]; |
| 194 | + |
| 195 | + [[rusResseract.absoluteDataPath should] equal:cachesTessDataPath]; |
152 | 196 | }); |
153 | 197 |
|
154 | 198 | it(@"Should initialize with config dictionary", ^{ |
|
168 | 212 | [[tesseract.absoluteDataPath should] equal:cachesTessDataPath]; |
169 | 213 |
|
170 | 214 | checkVariablesAreSetForTesseract(tesseract); |
| 215 | + |
| 216 | + recognizeSimpleImageWithTesseract(tesseract); |
171 | 217 | }); |
172 | 218 |
|
173 | 219 | it(@"Should initialize config dictionary and a file", ^{ |
|
183 | 229 | [[tesseract.absoluteDataPath should] equal:cachesTessDataPath]; |
184 | 230 |
|
185 | 231 | checkVariablesAreSetForTesseract(tesseract); |
| 232 | + |
| 233 | + recognizeSimpleImageWithTesseract(tesseract); |
186 | 234 | }); |
187 | | - }); |
188 | | - |
189 | | - it(@"Should initialize with config file path, while there is no tessdata folder yet", ^{ |
190 | 235 |
|
191 | | - NSLog(@"debugConfigsFilePathFromTheCaches: %@", debugConfigsFilePathFromTheCaches); |
192 | | - NSLog(@"recognitionConfigsFilePathFromTheCaches: %@", recognitionConfigsFilePathFromTheCaches); |
193 | | - [[[fileManager attributesOfItemAtPath:debugConfigsFilePathFromTheCaches error:nil] should] beNil]; |
194 | | - [[[fileManager attributesOfItemAtPath:recognitionConfigsFilePathFromTheCaches error:nil] should] beNil]; |
195 | | - |
196 | | - G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages |
197 | | - configDictionary:nil |
198 | | - configFileNames:@[debugConfigsFilePathFromTheCaches, recognitionConfigsFilePathFromTheCaches] |
199 | | - cachesRelatedDataPath:tessdataPath |
200 | | - engineMode:G8OCREngineModeTesseractOnly]; |
201 | | - [[tesseract shouldNot] beNil]; |
202 | | - [[tesseract.absoluteDataPath should] equal:cachesTessDataPath]; |
203 | | - |
204 | | - checkVariablesAreSetForTesseract(tesseract); |
| 236 | + it(@"Should initialize with 2 config files", ^{ |
| 237 | + |
| 238 | + [[[fileManager attributesOfItemAtPath:debugConfigsFilePathFromTheCaches error:nil] should] beNil]; |
| 239 | + [[[fileManager attributesOfItemAtPath:recognitionConfigsFilePathFromTheCaches error:nil] should] beNil]; |
| 240 | + |
| 241 | + G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages |
| 242 | + configDictionary:nil |
| 243 | + configFileNames:@[debugConfigsFilePathFromTheCaches, recognitionConfigsFilePathFromTheCaches] |
| 244 | + cachesRelatedDataPath:tessdataPath |
| 245 | + engineMode:G8OCREngineModeTesseractOnly]; |
| 246 | + [[tesseract shouldNot] beNil]; |
| 247 | + [[tesseract.absoluteDataPath should] equal:cachesTessDataPath]; |
| 248 | + |
| 249 | + checkVariablesAreSetForTesseract(tesseract); |
| 250 | + }); |
205 | 251 | }); |
206 | 252 |
|
207 | 253 | context(@"tessdata are already in the Caches", ^{ |
|
0 commit comments