Skip to content

Commit

Permalink
simple-thresholding: Refactor tests(remove 'let's)
Browse files Browse the repository at this point in the history
  • Loading branch information
bamx23 committed Dec 24, 2014
1 parent 6826d19 commit 0afb02f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 84 deletions.
4 changes: 4 additions & 0 deletions TestsProject/TestsProject.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
41184B591A3EFD41007F5923 /* tessdata in Resources */ = {isa = PBXBuildFile; fileRef = 41184B551A3EFCAD007F5923 /* tessdata */; };
412E9EAB1A44316C007DDAA5 /* well_scaned_page.png in Resources */ = {isa = PBXBuildFile; fileRef = 412E9EAA1A44316C007DDAA5 /* well_scaned_page.png */; };
412E9EAD1A45872A007DDAA5 /* image_sample_bl.png in Resources */ = {isa = PBXBuildFile; fileRef = 412E9EAC1A45872A007DDAA5 /* image_sample_bl.png */; };
4141211E1A4B1E3900583ED4 /* image_blank.png in Resources */ = {isa = PBXBuildFile; fileRef = 4141211D1A4B1E3900583ED4 /* image_blank.png */; };
41C68DAF1A41825500848AE1 /* UIImage+G8Equal.m in Sources */ = {isa = PBXBuildFile; fileRef = 41C68DAE1A41825500848AE1 /* UIImage+G8Equal.m */; };
41C68DB31A41849100848AE1 /* image_sample.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 41C68DB21A41849100848AE1 /* image_sample.jpg */; };
41C68DB51A41854600848AE1 /* image_sample_tr.png in Resources */ = {isa = PBXBuildFile; fileRef = 41C68DB41A41854600848AE1 /* image_sample_tr.png */; };
Expand Down Expand Up @@ -60,6 +61,7 @@
41184B551A3EFCAD007F5923 /* tessdata */ = {isa = PBXFileReference; lastKnownFileType = folder; name = tessdata; path = TestsProjectTests/tessdata; sourceTree = SOURCE_ROOT; };
412E9EAA1A44316C007DDAA5 /* well_scaned_page.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = well_scaned_page.png; sourceTree = "<group>"; };
412E9EAC1A45872A007DDAA5 /* image_sample_bl.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = image_sample_bl.png; sourceTree = "<group>"; };
4141211D1A4B1E3900583ED4 /* image_blank.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = image_blank.png; sourceTree = "<group>"; };
41C68DAD1A41825500848AE1 /* UIImage+G8Equal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+G8Equal.h"; sourceTree = "<group>"; };
41C68DAE1A41825500848AE1 /* UIImage+G8Equal.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+G8Equal.m"; sourceTree = "<group>"; };
41C68DB21A41849100848AE1 /* image_sample.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = image_sample.jpg; sourceTree = "<group>"; };
Expand Down Expand Up @@ -175,6 +177,7 @@
41C68DB21A41849100848AE1 /* image_sample.jpg */,
41C68DB41A41854600848AE1 /* image_sample_tr.png */,
412E9EAC1A45872A007DDAA5 /* image_sample_bl.png */,
4141211D1A4B1E3900583ED4 /* image_blank.png */,
);
name = Images;
sourceTree = "<group>";
Expand Down Expand Up @@ -274,6 +277,7 @@
41C68DB31A41849100848AE1 /* image_sample.jpg in Resources */,
4115B96C1A3EF8E90004EC0A /* Images.xcassets in Resources */,
4115B9851A3EF9920004EC0A /* Launch Screen.xib in Resources */,
4141211E1A4B1E3900583ED4 /* image_blank.png in Resources */,
41C68DB51A41854600848AE1 /* image_sample_tr.png in Resources */,
41184B591A3EFD41007F5923 /* tessdata in Resources */,
412E9EAD1A45872A007DDAA5 /* image_sample_bl.png in Resources */,
Expand Down
Binary file added TestsProject/TestsProject/image_blank.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified TestsProject/TestsProject/image_sample_tr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
131 changes: 47 additions & 84 deletions TestsProject/TestsProjectTests/RecognitionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,20 @@

__block G8Tesseract *tesseract;

let(image, ^id{
return [UIImage imageNamed:@"image_sample.jpg"];
});

let(engineMode, ^id{
return theValue(G8OCREngineModeTesseractOnly);
});

let(pageSegmentationMode, ^id{
return theValue(G8PageSegmentationModeAutoOnly);
});

let(charWhitelist, ^id{
return @"0123456789";
});

let(waitDeadline, ^id{
return @(6.0);
});

let(maxExpectedRecognitionTime, ^id{
return @(5.0);
__block G8OCREngineMode engineMode = G8OCREngineModeTesseractOnly;
__block G8PageSegmentationMode pageSegmentationMode = G8PageSegmentationModeAuto;
__block NSString *charWhitelist = @"0123456789";
__block NSTimeInterval waitDeadline = 5.0;
__block NSTimeInterval maxExpectedRecognitionTime = 5.0;
__block UIImage *image = nil;

beforeEach(^{
engineMode = G8OCREngineModeTesseractOnly;
pageSegmentationMode = G8PageSegmentationModeAuto;
charWhitelist = @"0123456789";
waitDeadline = 5.0;
maxExpectedRecognitionTime = 5.0;
image = nil;
});

#pragma mark - Helpers
Expand All @@ -60,11 +52,11 @@

void (^setupTesseract)() = ^{
tesseract.language = kG8Languages;
tesseract.engineMode = [engineMode integerValue];
tesseract.pageSegmentationMode = [pageSegmentationMode integerValue];
tesseract.engineMode = engineMode;
tesseract.pageSegmentationMode = pageSegmentationMode;

tesseract.charWhitelist = charWhitelist;
tesseract.maximumRecognitionTime = [waitDeadline floatValue];
tesseract.maximumRecognitionTime = waitDeadline;

tesseract.image = [image g8_blackAndWhite];
};
Expand All @@ -79,7 +71,7 @@
isDone = YES;
});

wait([maxExpectedRecognitionTime floatValue], ^{
wait(maxExpectedRecognitionTime, ^{
return (BOOL)(isDone == NO);
});

Expand All @@ -102,7 +94,7 @@
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:operation];

wait([maxExpectedRecognitionTime floatValue], ^{
wait(maxExpectedRecognitionTime, ^{
return (BOOL)(isDone == NO);
});

Expand All @@ -113,7 +105,8 @@

UIImage *(^thresholdedImageForImage)(UIImage *) = ^(UIImage *sourceImage) {
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages];
tesseract.image = image;

tesseract.image = [image g8_blackAndWhite];

return tesseract.thresholdedImage;
};
Expand All @@ -122,16 +115,10 @@

describe(@"Simple image", ^{

let(image, ^id{
return [UIImage imageNamed:@"image_sample.jpg"];
});

let(waitDeadline, ^id{
return @(1.0);
});

let(maxExpectedRecognitionTime, ^id{
return @(1.2);
beforeEach(^{
image = [UIImage imageNamed:@"image_sample.jpg"];
waitDeadline = 1.0;
maxExpectedRecognitionTime = 1.2;
});

it(@"Should recognize sync", ^{
Expand Down Expand Up @@ -206,20 +193,11 @@

describe(@"Well scaned page", ^{

let(image, ^id{
return [UIImage imageNamed:@"well_scaned_page"];
});

let(charWhitelist, ^id{
return @"";
});

let(waitDeadline, ^id{
return @(10.0);
});

let(maxExpectedRecognitionTime, ^id{
return @(9.0);
beforeEach(^{
image = [UIImage imageNamed:@"well_scaned_page"];
charWhitelist = @"";
waitDeadline = 10.0;
maxExpectedRecognitionTime = 9.0;
});

it(@"Should recognize", ^{
Expand All @@ -239,45 +217,30 @@
[[theValue([onceThresholded g8_isEqualToImage:twiceThresholded]) should] beYes];
});

describe(@"OSD", ^{

let(pageSegmentationMode, ^id{
return theValue(G8PageSegmentationModeAutoOSD);
});

it(@"Should analyze layout", ^{
[[theBlock(recognizeImageUsingOperation) shouldNot] raise];
it(@"Should analyze layout", ^{
pageSegmentationMode = G8PageSegmentationModeAutoOSD;

CGFloat deskewAngle = tesseract.deskewAngle;
[[theValue(ABS(deskewAngle)) should] beGreaterThan:theValue(FLT_EPSILON)];
[[theBlock(recognizeImageUsingOperation) shouldNot] raise];

[[theValue(tesseract.orientation) should] equal:theValue(G8OrientationPageUp)];
[[theValue(tesseract.writingDirection) should] equal:theValue(G8WritingDirectionLeftToRight)];
[[theValue(tesseract.textlineOrder) should] equal:theValue(G8TextlineOrderTopToBottom)];
});
CGFloat deskewAngle = tesseract.deskewAngle;
[[theValue(ABS(deskewAngle)) should] beGreaterThan:theValue(FLT_EPSILON)];

[[theValue(tesseract.orientation) should] equal:theValue(G8OrientationPageUp)];
[[theValue(tesseract.writingDirection) should] equal:theValue(G8WritingDirectionLeftToRight)];
[[theValue(tesseract.textlineOrder) should] equal:theValue(G8TextlineOrderTopToBottom)];
});

describe(@"Deadline", ^{

let(waitDeadline, ^id{
return @(2.0);
});

let(maxExpectedRecognitionTime, ^id{
return @(3.0);
});

it(@"Should break", ^{
[[theBlock(recognizeImageUsingOperation) shouldNot] raise];
it(@"Should break by deadline", ^{
waitDeadline = 2.0;
maxExpectedRecognitionTime = 3.0;

[[tesseract shouldNot] beNil];
NSString *recognizedText = tesseract.recognizedText;
[[recognizedText should] containString:@"Foreword"];
[[recognizedText shouldNot] containString:@"Mathematcs"];
[[[[tesseract confidencesByIteratorLevel:G8PageIteratorLevelWord] should] haveAtLeast:10] items];
});
[[theBlock(recognizeImageUsingOperation) shouldNot] raise];

[[tesseract shouldNot] beNil];
NSString *recognizedText = tesseract.recognizedText;
[[recognizedText should] containString:@"Foreword"];
[[recognizedText shouldNot] containString:@"Mathematcs"];
[[[[tesseract confidencesByIteratorLevel:G8PageIteratorLevelWord] should] haveAtLeast:10] items];
});

});
Expand Down

0 comments on commit 0afb02f

Please sign in to comment.