Skip to content

Commit

Permalink
new-features: Add G8RecognizedBlock for data returned by characterBox…
Browse files Browse the repository at this point in the history
…es and confidencesByIteratorLevel:
  • Loading branch information
bamx23 committed Dec 18, 2014
1 parent 00b9ccb commit 50d1a1a
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 64 deletions.
26 changes: 26 additions & 0 deletions Products/TesseractOCR.framework/Versions/A/Headers/G8Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,30 @@ typedef NS_ENUM(NSUInteger, G8OCREngineMode){
// G8OCREngineModeDefault,
};

/**
* Result iteration level
*/
typedef NS_ENUM(NSUInteger, G8PageIteratorLevel){
/**
* Block of text/image/separator line.
*/
G8PageIteratorLevelBlock,
/**
* Paragraph within a block.
*/
G8PageIteratorLevelParagraph,
/**
* Line within a paragraph.
*/
G8PageIteratorLevelTextline,
/**
* Word within a textline.
*/
G8PageIteratorLevelWord,
/**
* Symbol/character within a word.
*/
G8PageIteratorLevelSymbol,
};

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// G8RecognizedBlock.h
// Tesseract OCR iOS
//
// Created by Nikolay Volosatov on 18.12.14.
// Copyright (c) 2014 Daniele Galiotto - www.g8production.com. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "G8Constants.h"

@interface G8RecognizedBlock : NSObject <NSCopying>

@property (nonatomic, copy, readonly) NSString *text;
@property (nonatomic, assign, readonly) CGRect boundingBox;
@property (nonatomic, assign, readonly) CGFloat confidence;
@property (nonatomic, assign, readonly) G8PageIteratorLevel level;

- (instancetype)initWithText:(NSString *)text
boundingBox:(CGRect)boundingBox
confidence:(CGFloat)confidence
level:(G8PageIteratorLevel)level;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@
// This NSDictionary uses NSValue encoded CGRects as keys and the recognized character (NSString) as the value
// CGRects are in UIKit's coordinate space (origin is in the top left)
@property (nonatomic, readonly) NSArray *characterBoxes;

@property (nonatomic, readonly) NSArray *getConfidenceByWord;
@property (nonatomic, readonly) NSArray *getConfidenceBySymbol;
@property (nonatomic, readonly) NSArray *getConfidenceByTextline;
@property (nonatomic, readonly) NSArray *getConfidenceByParagraph;
@property (nonatomic, readonly) NSArray *getConfidenceByBlock;
- (NSArray *)confidencesByIteratorLevel:(G8PageIteratorLevel)pageIteratorLevel;

@property (nonatomic, weak) id<G8TesseractDelegate> delegate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
#define Tesseract_OCR_iOS_TesseractOCR_h

#import "G8Tesseract.h"
#import "G8RecognizedBlock.h"
#import "G8TesseractParameters.h"
#import "G8RecognitionOperation.h"
#import "G8Constants.h"
#import "UIImage+G8Filters.h"

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ -(void)recognizeImageWithTesseract:(UIImage *)img
});

operation.tesseract.charWhitelist = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //limit search
operation.tesseract.image = [img blackAndWhite]; //image to check

[operation.tesseract setImage:[img blackAndWhite]]; //image to check
//[operation.tesseract setRect:CGRectMake(20, 20, 100, 100)]; //optional: set the rectangle to recognize text in the image

operation.recognitionCompleteBlock = ^(G8Tesseract *tesseract) {
Expand Down
8 changes: 8 additions & 0 deletions Tesseract OCR iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
41A95DEB1A3AF39B0085093C /* G8TesseractParameters.m in Sources */ = {isa = PBXBuildFile; fileRef = 41A95DE91A3AF39B0085093C /* G8TesseractParameters.m */; };
41A95DEE1A3AFBF30085093C /* G8RecognitionOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = 41A95DEC1A3AFBF30085093C /* G8RecognitionOperation.h */; settings = {ATTRIBUTES = (Public, ); }; };
41A95DEF1A3AFBF30085093C /* G8RecognitionOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 41A95DED1A3AFBF30085093C /* G8RecognitionOperation.m */; };
41B41A511A42ED7600D46246 /* G8RecognizedBlock.h in Headers */ = {isa = PBXBuildFile; fileRef = 41B41A4F1A42ED7600D46246 /* G8RecognizedBlock.h */; settings = {ATTRIBUTES = (Public, ); }; };
41B41A521A42ED7600D46246 /* G8RecognizedBlock.m in Sources */ = {isa = PBXBuildFile; fileRef = 41B41A501A42ED7600D46246 /* G8RecognizedBlock.m */; };
6490748D198A5A5600D728CC /* UIImage+G8Filters.m in Sources */ = {isa = PBXBuildFile; fileRef = 6490748B198A5A5600D728CC /* UIImage+G8Filters.m */; };
64907491198A5DF200D728CC /* UIImage+G8Filters.h in Headers */ = {isa = PBXBuildFile; fileRef = 6490748A198A5A5600D728CC /* UIImage+G8Filters.h */; settings = {ATTRIBUTES = (Public, ); }; };
64A0293417307C1D002B12E7 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 64A0293217307C1D002B12E7 /* InfoPlist.strings */; };
Expand Down Expand Up @@ -185,6 +187,8 @@
41A95DE91A3AF39B0085093C /* G8TesseractParameters.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = G8TesseractParameters.m; sourceTree = "<group>"; };
41A95DEC1A3AFBF30085093C /* G8RecognitionOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = G8RecognitionOperation.h; sourceTree = "<group>"; };
41A95DED1A3AFBF30085093C /* G8RecognitionOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = G8RecognitionOperation.m; sourceTree = "<group>"; };
41B41A4F1A42ED7600D46246 /* G8RecognizedBlock.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = G8RecognizedBlock.h; sourceTree = "<group>"; };
41B41A501A42ED7600D46246 /* G8RecognizedBlock.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = G8RecognizedBlock.m; sourceTree = "<group>"; };
645C864A18698EBA00B27B19 /* README_howto_compile_libaries.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = README_howto_compile_libaries.md; sourceTree = "<group>"; };
645C864D18698ECE00B27B19 /* build_dependencies.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = build_dependencies.sh; sourceTree = "<group>"; };
646772C318083B5600C85662 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = README.md; path = ../README.md; sourceTree = "<group>"; };
Expand Down Expand Up @@ -388,6 +392,8 @@
64A029DD17307CED002B12E7 /* TesseractOCR.h */,
64A029D617307CD0002B12E7 /* G8Tesseract.h */,
64A029D717307CD0002B12E7 /* G8Tesseract.mm */,
41B41A4F1A42ED7600D46246 /* G8RecognizedBlock.h */,
41B41A501A42ED7600D46246 /* G8RecognizedBlock.m */,
41A95DEC1A3AFBF30085093C /* G8RecognitionOperation.h */,
41A95DED1A3AFBF30085093C /* G8RecognitionOperation.m */,
41A95DE81A3AF39B0085093C /* G8TesseractParameters.h */,
Expand Down Expand Up @@ -655,6 +661,7 @@
64A029FF17307D65002B12E7 /* ambigs.h in Headers */,
64A02A0017307D65002B12E7 /* apitypes.h in Headers */,
64A02A0117307D65002B12E7 /* baseapi.h in Headers */,
41B41A511A42ED7600D46246 /* G8RecognizedBlock.h in Headers */,
64A02A0217307D65002B12E7 /* basedir.h in Headers */,
64A02A0317307D65002B12E7 /* bits16.h in Headers */,
64A02A0417307D65002B12E7 /* bitvector.h in Headers */,
Expand Down Expand Up @@ -859,6 +866,7 @@
64E9A53319194B47007EE74A /* G8Tesseract.mm in Sources */,
41A95DEF1A3AFBF30085093C /* G8RecognitionOperation.m in Sources */,
41A95DEB1A3AF39B0085093C /* G8TesseractParameters.m in Sources */,
41B41A521A42ED7600D46246 /* G8RecognizedBlock.m in Sources */,
6490748D198A5A5600D728CC /* UIImage+G8Filters.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
26 changes: 26 additions & 0 deletions TesseractOCR/G8Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,30 @@ typedef NS_ENUM(NSUInteger, G8OCREngineMode){
// G8OCREngineModeDefault,
};

/**
* Result iteration level
*/
typedef NS_ENUM(NSUInteger, G8PageIteratorLevel){
/**
* Block of text/image/separator line.
*/
G8PageIteratorLevelBlock,
/**
* Paragraph within a block.
*/
G8PageIteratorLevelParagraph,
/**
* Line within a paragraph.
*/
G8PageIteratorLevelTextline,
/**
* Word within a textline.
*/
G8PageIteratorLevelWord,
/**
* Symbol/character within a word.
*/
G8PageIteratorLevelSymbol,
};

#endif
24 changes: 24 additions & 0 deletions TesseractOCR/G8RecognizedBlock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// G8RecognizedBlock.h
// Tesseract OCR iOS
//
// Created by Nikolay Volosatov on 18.12.14.
// Copyright (c) 2014 Daniele Galiotto - www.g8production.com. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "G8Constants.h"

@interface G8RecognizedBlock : NSObject <NSCopying>

@property (nonatomic, copy, readonly) NSString *text;
@property (nonatomic, assign, readonly) CGRect boundingBox;
@property (nonatomic, assign, readonly) CGFloat confidence;
@property (nonatomic, assign, readonly) G8PageIteratorLevel level;

- (instancetype)initWithText:(NSString *)text
boundingBox:(CGRect)boundingBox
confidence:(CGFloat)confidence
level:(G8PageIteratorLevel)level;

@end
83 changes: 83 additions & 0 deletions TesseractOCR/G8RecognizedBlock.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// G8RecognizedBlock.m
// Tesseract OCR iOS
//
// Created by Nikolay Volosatov on 18.12.14.
// Copyright (c) 2014 Daniele Galiotto - www.g8production.com. All rights reserved.
//

#import "G8RecognizedBlock.h"

@interface G8RecognizedBlock ()

@property (nonatomic, copy) NSString *text;
@property (nonatomic, assign) CGRect boundingBox;
@property (nonatomic, assign) CGFloat confidence;
@property (nonatomic, assign) G8PageIteratorLevel level;

@end

@implementation G8RecognizedBlock

- (instancetype)init
{
return [self initWithText:nil
boundingBox:CGRectZero
confidence:0.0f
level:G8PageIteratorLevelBlock];
}

- (instancetype)initWithText:(NSString *)text
boundingBox:(CGRect)boundingBox
confidence:(CGFloat)confidence
level:(G8PageIteratorLevel)level
{
self = [super init];
if (self != nil) {
_text = [text copy];
_boundingBox = boundingBox;
_confidence = confidence;
_level = level;
}
return self;
}

- (id)copyWithZone:(NSZone *)zone
{
return self;
}

- (BOOL)isEqual:(id)other
{
if (other == self) {
return YES;
}
else if ([super isEqual:other] == NO) {
return NO;
}
else if ([other isKindOfClass:[self class]] == NO) {
return NO;
}
else {
G8RecognizedBlock *otherBlock = other;

BOOL result = self.level == otherBlock.level;
result = result && ABS(self.confidence - otherBlock.confidence) < FLT_EPSILON;
result = result && CGRectEqualToRect(self.boundingBox, otherBlock.boundingBox);
result = result && (self.text == otherBlock.text || [self.text isEqual:otherBlock.text]);

return result;
}
}

- (NSUInteger)hash
{
return self.level + (NSUInteger)(self.confidence * 10000) * 11813 + self.text.hash * 13411;
}

- (NSString *)description
{
return [NSString stringWithFormat:@"(%.2f%%) '%@'", self.confidence, self.text];
}

@end
7 changes: 1 addition & 6 deletions TesseractOCR/G8Tesseract.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@
// This NSDictionary uses NSValue encoded CGRects as keys and the recognized character (NSString) as the value
// CGRects are in UIKit's coordinate space (origin is in the top left)
@property (nonatomic, readonly) NSArray *characterBoxes;

@property (nonatomic, readonly) NSArray *getConfidenceByWord;
@property (nonatomic, readonly) NSArray *getConfidenceBySymbol;
@property (nonatomic, readonly) NSArray *getConfidenceByTextline;
@property (nonatomic, readonly) NSArray *getConfidenceByParagraph;
@property (nonatomic, readonly) NSArray *getConfidenceByBlock;
- (NSArray *)confidencesByIteratorLevel:(G8PageIteratorLevel)pageIteratorLevel;

@property (nonatomic, weak) id<G8TesseractDelegate> delegate;

Expand Down
Loading

0 comments on commit 50d1a1a

Please sign in to comment.