Skip to content

Commit

Permalink
Merge pull request #192 from ws233/master
Browse files Browse the repository at this point in the history
Increased code coverage to 100%. Added tests for 8 bit images. Fixes #85.
  • Loading branch information
Kevin Conley committed Jul 9, 2015
2 parents d288c8c + 6b68096 commit a4899f3
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 22 deletions.
14 changes: 9 additions & 5 deletions TesseractOCR/G8Tesseract.mm
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ - (BOOL)moveTessdataToDirectoryIfNecessary:(NSString *)directoryPath
BOOL isDirectory = YES;
if (![fileManager fileExistsAtPath:tessdataPath isDirectory:&isDirectory] || !isDirectory) {
// No tessdata directory in application bundle, nothing to do.
return YES;
return NO;
}

if ([fileManager fileExistsAtPath:destinationPath] == NO) {
Expand Down Expand Up @@ -403,9 +403,11 @@ - (void)setImage:(UIImage *)image
@try {
_tesseract->SetImage(pix);
}
//LCOV_EXCL_START
@catch (NSException *exception) {
NSLog(@"ERROR: Can't set image: %@", exception);
}
//LCOV_EXCL_STOP
pixDestroy(&pix);

_image = image;
Expand Down Expand Up @@ -657,7 +659,7 @@ - (NSData *)recognizedPDFForImages:(NSArray*)images {
// Begin producing output
const char* kUnknownTitle = "Unknown Title";
if (renderer && !renderer->BeginDocument(kUnknownTitle)) {
return nil;
return nil; // LCOV_EXCL_LINE
}

bool result = YES;
Expand All @@ -676,12 +678,12 @@ - (NSData *)recognizedPDFForImages:(NSArray*)images {

// error
if (!result) {
return nil;
return nil; // LCOV_EXCL_LINE
}

// Finish producing output
if (renderer && !renderer->EndDocument()) {
return nil;
return nil; // LCOV_EXCL_LINE
}

const char *pdfData = NULL;
Expand Down Expand Up @@ -741,9 +743,11 @@ - (BOOL)recognize
returnCode = _tesseract->Recognize(_monitor);
self.recognized = YES;
}
//LCOV_EXCL_START
@catch (NSException *exception) {
NSLog(@"Exception was raised while recognizing: %@", exception);
}
//LCOV_EXCL_STOP
return returnCode == 0 && self.recognized;
}

Expand Down Expand Up @@ -867,7 +871,7 @@ - (Pix *)pixForImage:(UIImage *)image
break;

default:
NSLog(@"Cannot convert image to Pix with bpp = %d", bpp);
NSLog(@"Cannot convert image to Pix with bpp = %d", bpp); // LCOV_EXCL_LINE
}
pixSetYRes(pix, (l_int32)self.sourceResolution);

Expand Down
12 changes: 8 additions & 4 deletions TestsProject/TestsProject.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@
73BE4C351A5BD324002C15F1 /* image_sample.hOCR in Resources */ = {isa = PBXBuildFile; fileRef = 73BE4C341A5BD324002C15F1 /* image_sample.hOCR */; };
73BE4C441A5DADDD002C15F1 /* TesseractOCR.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 73BE4C431A5DADDD002C15F1 /* TesseractOCR.framework */; };
8FA2F9CE23919BEC8C64A5EA /* libPods-TestsProjectTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 5CD3C116A45C293ADAC81D1B /* libPods-TestsProjectTests.a */; };
F120B54A1B45D642003F38A7 /* empty.pdf in Resources */ = {isa = PBXBuildFile; fileRef = F120B5491B45D642003F38A7 /* empty.pdf */; };
F18F53451AE7A6C9005F3843 /* grayscaledImage.png in Resources */ = {isa = PBXBuildFile; fileRef = F18F53441AE7A6C9005F3843 /* grayscaledImage.png */; };
F19D834A1B34C97D00EB8D2E /* NSData+G8Equal.m in Sources */ = {isa = PBXBuildFile; fileRef = F19D83491B34C97D00EB8D2E /* NSData+G8Equal.m */; };
F1A7AB8C1B34996D00FE9650 /* well_scaned_page.jpg in Resources */ = {isa = PBXBuildFile; fileRef = F1A7AB8B1B34996D00FE9650 /* well_scaned_page.jpg */; };
F1A7AB8E1B34B91000FE9650 /* well_scaned_page.pdf in Resources */ = {isa = PBXBuildFile; fileRef = F1A7AB8D1B34B91000FE9650 /* well_scaned_page.pdf */; };
F1D1AFF21B4580C100AB59EE /* well_scaned_page.png in Resources */ = {isa = PBXBuildFile; fileRef = F1D1AFF11B4580C100AB59EE /* well_scaned_page.png */; };
F1DF11991AE5772400FA6F5D /* rotated_image_sample.jpg in Resources */ = {isa = PBXBuildFile; fileRef = F1DF11981AE5772400FA6F5D /* rotated_image_sample.jpg */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -90,11 +91,12 @@
73BE4C431A5DADDD002C15F1 /* TesseractOCR.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = TesseractOCR.framework; path = "../build/Debug-iphoneos/TesseractOCR.framework"; sourceTree = "<group>"; };
73C0A7BC1A59561F00D823D4 /* TesseractOCR.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = TesseractOCR.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8CA185A06F5E788CE0DC28EB /* Pods-TestsProjectTests.coverage.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-TestsProjectTests.coverage.xcconfig"; path = "../Pods/Target Support Files/Pods-TestsProjectTests/Pods-TestsProjectTests.coverage.xcconfig"; sourceTree = "<group>"; };
F120B5491B45D642003F38A7 /* empty.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = empty.pdf; sourceTree = "<group>"; };
F18F53441AE7A6C9005F3843 /* grayscaledImage.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = grayscaledImage.png; sourceTree = "<group>"; };
F19D83481B34C97D00EB8D2E /* NSData+G8Equal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+G8Equal.h"; sourceTree = "<group>"; };
F19D83491B34C97D00EB8D2E /* NSData+G8Equal.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSData+G8Equal.m"; sourceTree = "<group>"; };
F1A7AB8B1B34996D00FE9650 /* well_scaned_page.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = well_scaned_page.jpg; sourceTree = "<group>"; };
F1A7AB8D1B34B91000FE9650 /* well_scaned_page.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = well_scaned_page.pdf; sourceTree = "<group>"; };
F1D1AFF11B4580C100AB59EE /* well_scaned_page.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = well_scaned_page.png; sourceTree = "<group>"; };
F1DF11981AE5772400FA6F5D /* rotated_image_sample.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = rotated_image_sample.jpg; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -214,8 +216,9 @@
41C68DB61A41854A00848AE1 /* Images */ = {
isa = PBXGroup;
children = (
F120B5491B45D642003F38A7 /* empty.pdf */,
73BE4C301A5B404C002C15F1 /* well_scaned_page.hOCR */,
F1A7AB8B1B34996D00FE9650 /* well_scaned_page.jpg */,
F1D1AFF11B4580C100AB59EE /* well_scaned_page.png */,
F1A7AB8D1B34B91000FE9650 /* well_scaned_page.pdf */,
41C68DB21A41849100848AE1 /* image_sample.jpg */,
41C68DB41A41854600848AE1 /* image_sample_tr.png */,
Expand Down Expand Up @@ -321,6 +324,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
F1D1AFF21B4580C100AB59EE /* well_scaned_page.png in Resources */,
F1DF11991AE5772400FA6F5D /* rotated_image_sample.jpg in Resources */,
41C68DB31A41849100848AE1 /* image_sample.jpg in Resources */,
73BE4C351A5BD324002C15F1 /* image_sample.hOCR in Resources */,
Expand All @@ -329,12 +333,12 @@
F1A7AB8E1B34B91000FE9650 /* well_scaned_page.pdf in Resources */,
4141211E1A4B1E3900583ED4 /* image_blank.png in Resources */,
F18F53451AE7A6C9005F3843 /* grayscaledImage.png in Resources */,
F1A7AB8C1B34996D00FE9650 /* well_scaned_page.jpg in Resources */,
41C68DB51A41854600848AE1 /* image_sample_tr.png in Resources */,
41184B591A3EFD41007F5923 /* tessdata in Resources */,
736EFF271A5882730031B432 /* tessdata-rus in Resources */,
73BE4C311A5B404C002C15F1 /* well_scaned_page.hOCR in Resources */,
412E9EAD1A45872A007DDAA5 /* image_sample_bl.png in Resources */,
F120B54A1B45D642003F38A7 /* empty.pdf in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file added TestsProject/TestsProject/empty.pdf
Binary file not shown.
Binary file removed TestsProject/TestsProject/well_scaned_page.jpg
Binary file not shown.
Binary file added TestsProject/TestsProject/well_scaned_page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 24 additions & 2 deletions TestsProject/TestsProjectTests/InitializationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,19 @@ - (void*)pixForImage:(UIImage *)image;

[[text should] beNil];
});

it(@"Should set original image if pixForImage for preprocessed image retuens nil", ^{
G8RecognitionTestsHelper *helper = [[G8RecognitionTestsHelper alloc] init];
[helper setupTesseract];
helper.tesseract.delegate = helper;
helper.customPreprocessingType = G8CustomPreprocessingSimpleThreshold;
[helper.tesseract stub:@selector(pixForImage:) andReturn:nil];

UIImage *inputImage = [UIImage imageNamed:@"image_sample.jpg"];
helper.tesseract.image = inputImage;

[[helper.tesseract.image should] equal:inputImage];
});
});

NSString *tessdataPath = @"foo/bar";
Expand Down Expand Up @@ -236,9 +249,18 @@ - (void*)pixForImage:(UIImage *)image;
[[theValue(isDirectory) should] beYes];

cleanTessdataFolderAtPath(customDirectoryPath);

});


it(@"Should not initialize if no tessdata folder in app bundle", ^{

[[NSFileManager defaultManager] stub:@selector(fileExistsAtPath:isDirectory:) andReturn:NO];
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages
configDictionary:nil
configFileNames:nil
absoluteDataPath:customDirectoryPath
engineMode:G8OCREngineModeTesseractOnly];
[[tesseract should] beNil];
});
});

context(@"nil cachesRelatedDataPath", ^{
Expand Down
43 changes: 32 additions & 11 deletions TestsProject/TestsProjectTests/RecognitionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@
static NSString *const kG8WellScanedFinalLongString = @"recommendations sometimes get acted on";

beforeEach(^{
helper.image = [UIImage imageNamed:@"well_scaned_page.jpg"];
helper.image = [UIImage imageNamed:@"well_scaned_page"];
});

it(@"Should recognize", ^{
Expand Down Expand Up @@ -311,7 +311,7 @@

NSString *path = [[NSBundle mainBundle] pathForResource:@"well_scaned_page" ofType:@"hOCR"];

helper.image = [UIImage imageNamed:@"well_scaned_page.jpg"];
helper.image = [UIImage imageNamed:@"well_scaned_page"];

[helper recognizeImage];
NSString *hOCR = [helper.tesseract recognizedHOCRForPageNumber:0];
Expand All @@ -331,16 +331,37 @@

describe(@"PDF", ^{

it(@"Should recognize well scanced page", ^{
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:@"eng"];
UIImage *image = [UIImage imageNamed:@"well_scaned_page.jpg"];
NSData *pdfData = [tesseract recognizedPDFForImages:@[image]];
NSData *(^recognizedPDFForImages)(NSArray*images) = ^NSData*(NSArray*images) {
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:@"eng"];
return [tesseract recognizedPDFForImages:images];
};

NSString *wellScannedPDFPath = [[NSBundle mainBundle] pathForResource:@"well_scaned_page" ofType:@"pdf"];
NSData *wellScannedPDF = [NSData dataWithContentsOfFile:wellScannedPDFPath];
NSAssert(wellScannedPDF, @"There is not %@ file to compare to", wellScannedPDFPath);
[[theValue([pdfData g8_isEqualToData:wellScannedPDF]) should] beYes];
});
NSData * (^samplePDFDataFromFile)(NSString *fileName) = ^NSData*(NSString *fileName) {
NSString *PDFPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"pdf"];
NSData *PDF = [NSData dataWithContentsOfFile:PDFPath];
NSAssert(PDF, @"There is not %@ file to compare to", PDFPath);
return PDF;
};

it(@"Should generate well scaned page", ^{
NSData *pdfData = recognizedPDFForImages(@[[UIImage imageNamed:@"well_scaned_page"]]);
[[theValue([pdfData g8_isEqualToData:samplePDFDataFromFile(@"well_scaned_page")]) should] beYes];
});

context(@"Should generate empty page for", ^{

it(@"nil array", ^{
[[theValue([recognizedPDFForImages(nil) g8_isEqualToData:samplePDFDataFromFile(@"empty")]) should] beYes];
});

it(@"empty array", ^{
[[theValue([recognizedPDFForImages(@[]) g8_isEqualToData:samplePDFDataFromFile(@"empty")]) should] beYes];
});

it(@"array containing nonimage", ^{
[[theValue([recognizedPDFForImages(@[@"someStringAsImage.png"]) g8_isEqualToData:samplePDFDataFromFile(@"empty")]) should] beYes];
});
});
});

SPEC_END

0 comments on commit a4899f3

Please sign in to comment.