Skip to content

Commit

Permalink
Merge pull request #160 from kevincon/fix-orientation
Browse files Browse the repository at this point in the history
Image orientation fix. Fixes #151. Fixes #130.
  • Loading branch information
Kevin Conley committed Apr 3, 2015
2 parents 87614be + 661d210 commit a973a6c
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ DEPENDENCIES:
- Kiwi (~> 2.3.1)

SPEC CHECKSUMS:
Kiwi: 73e1400209055ee9c8ba78c6012b6b642d0fb9f7
Kiwi: f038a6c61f7a9e4d7766bff5717aa3b3fdb75f55

COCOAPODS: 0.36.0.beta.1
COCOAPODS: 0.36.1
8 changes: 8 additions & 0 deletions Tesseract OCR iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
413F03321A5FBBDD000C194B /* UIImage+G8FixOrientation.h in Headers */ = {isa = PBXBuildFile; fileRef = 413F03301A5FBBDD000C194B /* UIImage+G8FixOrientation.h */; };
413F03331A5FBBDD000C194B /* UIImage+G8FixOrientation.m in Sources */ = {isa = PBXBuildFile; fileRef = 413F03311A5FBBDD000C194B /* UIImage+G8FixOrientation.m */; };
73C0A7961A5932C400D823D4 /* G8Tesseract.h in Headers */ = {isa = PBXBuildFile; fileRef = 64A029D617307CD0002B12E7 /* G8Tesseract.h */; settings = {ATTRIBUTES = (Public, ); }; };
73C0A7971A5932C800D823D4 /* G8Tesseract.mm in Sources */ = {isa = PBXBuildFile; fileRef = 64A029D717307CD0002B12E7 /* G8Tesseract.mm */; };
73C0A7981A5932CC00D823D4 /* G8TesseractDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 4141211F1A4C578800583ED4 /* G8TesseractDelegate.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand All @@ -25,6 +27,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
413F03301A5FBBDD000C194B /* UIImage+G8FixOrientation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+G8FixOrientation.h"; sourceTree = "<group>"; };
413F03311A5FBBDD000C194B /* UIImage+G8FixOrientation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+G8FixOrientation.m"; sourceTree = "<group>"; };
4141211F1A4C578800583ED4 /* G8TesseractDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = G8TesseractDelegate.h; sourceTree = "<group>"; };
418997A71A42CC8B00D6477C /* G8Constants.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = G8Constants.h; sourceTree = "<group>"; };
41A95DE81A3AF39B0085093C /* G8TesseractParameters.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = G8TesseractParameters.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -246,6 +250,8 @@
418997A71A42CC8B00D6477C /* G8Constants.h */,
6490748A198A5A5600D728CC /* UIImage+G8Filters.h */,
6490748B198A5A5600D728CC /* UIImage+G8Filters.m */,
413F03301A5FBBDD000C194B /* UIImage+G8FixOrientation.h */,
413F03311A5FBBDD000C194B /* UIImage+G8FixOrientation.m */,
41C7E8231A3F0682000DC42B /* Core */,
41C7E8211A3F0650000DC42B /* Readme */,
64A0293017307C1D002B12E7 /* Supporting Files */,
Expand Down Expand Up @@ -465,6 +471,7 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
413F03321A5FBBDD000C194B /* UIImage+G8FixOrientation.h in Headers */,
73C0A7961A5932C400D823D4 /* G8Tesseract.h in Headers */,
73C0A79D1A5932F900D823D4 /* G8TesseractParameters.h in Headers */,
73C0A7A01A59330A00D823D4 /* UIImage+G8Filters.h in Headers */,
Expand Down Expand Up @@ -548,6 +555,7 @@
73C0A7A11A59330E00D823D4 /* UIImage+G8Filters.m in Sources */,
73C0A79C1A5932F500D823D4 /* G8RecognitionOperation.m in Sources */,
73C0A7971A5932C800D823D4 /* G8Tesseract.mm in Sources */,
413F03331A5FBBDD000C194B /* UIImage+G8FixOrientation.m in Sources */,
73C0A79E1A5932FD00D823D4 /* G8TesseractParameters.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
18 changes: 13 additions & 5 deletions TesseractOCR/G8Tesseract.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "G8Tesseract.h"

#import "UIImage+G8Filters.h"
#import "UIImage+G8FixOrientation.h"
#import "G8TesseractParameters.h"
#import "G8Constants.h"
#import "G8RecognizedBlock.h"
Expand Down Expand Up @@ -375,6 +376,8 @@ - (void)setImage:(UIImage *)image
return;
}

image = [image fixOrientation];

self.imageSize = image.size; //self.imageSize used in the characterBoxes method

Pix *pix = nullptr;
Expand Down Expand Up @@ -490,43 +493,48 @@ - (NSString *)recognizedText
- (G8Orientation)orientation
{
if (self.layoutAnalysed == NO) {
[self analyzeLayout];
[self analyseLayout];
}
return _orientation;
}

- (G8WritingDirection)writingDirection
{
if (self.layoutAnalysed == NO) {
[self analyzeLayout];
[self analyseLayout];
}
return _writingDirection;
}

- (G8TextlineOrder)textlineOrder
{
if (self.layoutAnalysed == NO) {
[self analyzeLayout];
[self analyseLayout];
}
return _textlineOrder;
}

- (CGFloat)deskewAngle
{
if (self.layoutAnalysed == NO) {
[self analyzeLayout];
[self analyseLayout];
}
return _deskewAngle;
}

- (void)analyzeLayout
- (void)analyseLayout
{
tesseract::Orientation orientation;
tesseract::WritingDirection direction;
tesseract::TextlineOrder order;
float deskewAngle;

tesseract::PageIterator *iterator = _tesseract->AnalyseLayout();
if (iterator == NULL) {
NSLog(@"Can't analyse layout. Make sure 'osd.traineddata' available in 'tessdata'.");
return;
}

iterator->Orientation(&orientation, &direction, &order, &deskewAngle);
delete iterator;

Expand Down
19 changes: 19 additions & 0 deletions TesseractOCR/UIImage+G8FixOrientation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// UIImage+G8FixOrientation.h
// Tesseract OCR iOS
//
// Thanks to `an0` for answer from
// http://stackoverflow.com/a/10611036
//
// Created by Nikolay Volosatov on 09/01/15.
// Copyright (c) 2014 Daniele Galiotto - www.g8production.com.
// All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIImage (G8FixOrientation)

- (UIImage *)fixOrientation;

@end
34 changes: 34 additions & 0 deletions TesseractOCR/UIImage+G8FixOrientation.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// UIImage+G8FixOrientation.m
// Tesseract OCR iOS
//
// Thanks to `an0` for answer from
// http://stackoverflow.com/a/10611036
//
// Created by Nikolay Volosatov on 09/01/15.
// Copyright (c) 2014 Daniele Galiotto - www.g8production.com.
// All rights reserved.
//

#import "UIImage+G8FixOrientation.h"

@implementation UIImage (G8FixOrientation)

- (UIImage *)fixOrientation
{
// No-op if the orientation is already correct
if (self.imageOrientation == UIImageOrientationUp) return self;

UIImage *result;

UIGraphicsBeginImageContextWithOptions(self.size, NO, self.scale);

[self drawInRect:(CGRect){0, 0, self.size}];
result = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return result;
}

@end
29 changes: 29 additions & 0 deletions TestsProject/TestsProjectTests/RecognitionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@
[[recognizedText should] containString:@"1234567890"];
});

it(@"Should recognize regardless of orientation", ^{
UIImage *image = helper.image;
UIImage *rotatedImage = [UIImage imageWithCGImage:image.CGImage
scale:image.scale
orientation:UIImageOrientationLeft];

[[theValue(image.imageOrientation) shouldNot] equal:theValue(rotatedImage.imageOrientation)];

[[theBlock(^{
[helper recognizeImage];
}) shouldNot] raise];

NSString *recognizedText = helper.tesseract.recognizedText;
[[recognizedText should] containString:@"1234567890"];

UIImage *thresholdedImage = helper.tesseract.thresholdedImage;
[[theValue(thresholdedImage.imageOrientation) should] equal:theValue(UIImageOrientationUp)];
});

describe(@"Subimage", ^{

beforeEach(^{
Expand Down Expand Up @@ -226,6 +245,16 @@
[[theValue([onceThresholded g8_isEqualToImage:twiceThresholded]) should] beYes];
});

it(@"Should not crash analyze layout", ^{
helper.pageSegmentationMode = G8PageSegmentationModeOSDOnly;

[helper recognizeImage];

[[theBlock(^{
[helper.tesseract deskewAngle];
}) shouldNot] raise];
});

it(@"Should analyze layout", ^{
helper.pageSegmentationMode = G8PageSegmentationModeAutoOSD;

Expand Down

0 comments on commit a973a6c

Please sign in to comment.