Skip to content

Commit

Permalink
Merge pull request #169 from ws233/master
Browse files Browse the repository at this point in the history
Improved test coverage to 99%.
  • Loading branch information
Kevin Conley committed Apr 23, 2015
2 parents 2cea0da + ccbc68e commit 07332c5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
6 changes: 3 additions & 3 deletions TesseractOCR/G8RecognitionOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

#import "TesseractOCR.h"

@interface G8RecognitionOperation() <G8TesseractDelegate>

@property (nonatomic, strong, readwrite) G8Tesseract *tesseract;
@interface G8RecognitionOperation() <G8TesseractDelegate> {
G8Tesseract *_tesseract;
}
@property (nonatomic, assign, readwrite) CGFloat progress;

@end
Expand Down
4 changes: 4 additions & 0 deletions TestsProject/TestsProject.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
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 */; };
F18F53451AE7A6C9005F3843 /* grayscaledImage.png in Resources */ = {isa = PBXBuildFile; fileRef = F18F53441AE7A6C9005F3843 /* grayscaledImage.png */; };
F1DF11991AE5772400FA6F5D /* rotated_image_sample.jpg in Resources */ = {isa = PBXBuildFile; fileRef = F1DF11981AE5772400FA6F5D /* rotated_image_sample.jpg */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -88,6 +89,7 @@
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>"; };
F18F53441AE7A6C9005F3843 /* grayscaledImage.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = grayscaledImage.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 @@ -213,6 +215,7 @@
73BE4C341A5BD324002C15F1 /* image_sample.hOCR */,
4141211D1A4B1E3900583ED4 /* image_blank.png */,
F1DF11981AE5772400FA6F5D /* rotated_image_sample.jpg */,
F18F53441AE7A6C9005F3843 /* grayscaledImage.png */,
);
name = Images;
sourceTree = "<group>";
Expand Down Expand Up @@ -317,6 +320,7 @@
4115B96C1A3EF8E90004EC0A /* Images.xcassets in Resources */,
4115B9851A3EF9920004EC0A /* Launch Screen.xib in Resources */,
4141211E1A4B1E3900583ED4 /* image_blank.png in Resources */,
F18F53451AE7A6C9005F3843 /* grayscaledImage.png in Resources */,
41C68DB51A41854600848AE1 /* image_sample_tr.png in Resources */,
41184B591A3EFD41007F5923 /* tessdata in Resources */,
736EFF271A5882730031B432 /* tessdata-rus in Resources */,
Expand Down
Binary file added TestsProject/TestsProject/grayscaledImage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 31 additions & 8 deletions TestsProject/TestsProjectTests/G8RecognitionTestsHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ - (void)setupTesseract
self.tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages];
}

self.tesseract.delegate = self;

self.tesseract.language = kG8Languages;
self.tesseract.engineMode = self.engineMode;
self.tesseract.pageSegmentationMode = self.pageSegmentationMode;
Expand All @@ -81,6 +79,8 @@ - (void)recognizeImage
{
self.tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages];
[self setupTesseract];
self.tesseract.delegate = self;

[self setupImage];

[self.tesseract recognize];
Expand All @@ -89,18 +89,25 @@ - (void)recognizeImage
- (void)recognizeImageUsingOperation
{
G8RecognitionOperation *operation = [[G8RecognitionOperation alloc] initWithLanguage:kG8Languages];
operation.delegate = self;
self.tesseract = operation.tesseract;
[self setupTesseract];

[self setupImage];

__weak __typeof(self) weakSelf = self;

__block BOOL recognitionCompleteBlockInvoked = NO;
__block BOOL progressCallbackBlockInvoked = NO;
// Set up callbacks to test that it's being called
operation.progressCallbackBlock = ^(G8Tesseract *tesseract){
progressCallbackBlockInvoked = YES;
};

self.tesseract = nil;
__weak __typeof(self) weakSelf = self;
operation.recognitionCompleteBlock = ^(G8Tesseract *recognizedTesseract) {
__strong __typeof(weakSelf) strongSelf = weakSelf;

strongSelf.tesseract = recognizedTesseract;
recognitionCompleteBlockInvoked = YES;
};

NSOperationQueue *queue = [[NSOperationQueue alloc] init];
Expand All @@ -111,22 +118,38 @@ - (void)recognizeImageUsingOperation

return (BOOL)(strongSelf.tesseract == nil);
}];

if (self.tesseract == nil) {
[NSException raise:@"Tesseract stopped" format:@"Tesseract worked too long"];
}

NSAssert(recognitionCompleteBlockInvoked == YES, @"Error! recognitionCompleteBlock has not been invoked");
NSAssert(progressCallbackBlockInvoked == YES, @"Error! progressCallbackBlock has not been invoked");
};

- (UIImage *)thresholdedImageForImage:(UIImage *)sourceImage
{
self.tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages];
[self setupTesseract];

self.tesseract.delegate = self;

self.tesseract.image = [sourceImage g8_blackAndWhite];

return self.tesseract.thresholdedImage;
};

#pragma mark - G8TesseractDelegate methods

- (void)progressImageRecognitionForTesseract:(G8Tesseract *)tesseract
{

}

- (BOOL)shouldCancelImageRecognitionForTesseract:(G8Tesseract *)tesseract
{
return NO;
}

- (UIImage *)preprocessedImageForTesseract:(G8Tesseract *)tesseract sourceImage:(UIImage *)sourceImage
{
switch (self.customPreprocessingType) {
Expand Down
12 changes: 12 additions & 0 deletions TestsProject/TestsProjectTests/InitializationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import <Kiwi/Kiwi.h>
#import "Defaults.h"
#import "G8RecognitionTestsHelper.h"
#import "UIImage+G8Equal.h"

@interface G8Tesseract (Tests)
+ (void)didReceiveMemoryWarningNotification:(NSNotification*)notification;
Expand Down Expand Up @@ -102,6 +103,17 @@ - (void*)pixForImage:(UIImage *)image;
tesseract.image = [UIImage imageNamed:@"image_sample.jpg"];
});

it(@"Should grayscale", ^{
UIImage *image = [UIImage imageNamed:@"image_sample_bl.png"];
UIImage *grayscaledImage = [image g8_grayScale];

UIImage *patternImage = [UIImage imageNamed:@"grayscaledImage.png"];
NSAssert(patternImage, @"Error! Pattern image doesn't exist");

BOOL equal = [grayscaledImage g8_isEqualToImage:patternImage];
[[theValue(equal) should] beYes];
});

it(@"Should test pixForImage with zero image size", ^{
G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:kG8Languages];

Expand Down

0 comments on commit 07332c5

Please sign in to comment.