Skip to content

Commit

Permalink
nsoperation: Add simple implementation for RecognitionOperation
Browse files Browse the repository at this point in the history
  • Loading branch information
bamx23 committed Dec 12, 2014
1 parent 67b18bd commit bd375ba
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// RecognitionOperation.h
// Tesseract OCR iOS
//
// Created by Nikolay Volosatov on 12.12.14.
// Copyright (c) 2014 Daniele Galiotto - www.g8production.com. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "TesseractOCR.h"

typedef void(^RecognitionOperationComplete)(Tesseract *tesseract);

@interface RecognitionOperation : NSOperation

@property (nonatomic, strong, readonly) Tesseract *tesseract;
@property (nonatomic, weak) id<TesseractDelegate> delegate;

@property (nonatomic, assign, readonly) CGFloat progress;

@property (nonatomic, copy) RecognitionOperationComplete recognitionCompleteBlock;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
#define Tesseract_OCR_iOS_TesseractOCR_h

#import "Tesseract.h"
#import "RecognitionOperation.h"

#endif
Binary file modified Products/TesseractOCR.framework/Versions/A/Resources/Info.plist
Binary file not shown.
Binary file modified Products/TesseractOCR.framework/Versions/A/TesseractOCR
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ @interface G8ViewController ()
{

}

@property (nonatomic, strong) NSOperationQueue *operationQueue;

@end

@implementation G8ViewController
Expand Down Expand Up @@ -45,12 +48,17 @@ - (void)viewDidLoad
// set up the delegate to recieve tesseract's callback
// self should respond to TesseractDelegate and implement shouldCancelImageRecognitionForTesseract: method
// to have an ability to recieve callback and interrupt Tesseract before it finishes


self.operationQueue = [[NSOperationQueue alloc] init];
[self recognizeSampleImage:nil];
}

-(void)recognizeImageWithTesseract:(UIImage *)img
{
RecognitionOperation *operation = [[RecognitionOperation alloc] init];
operation.tesseract.language = @"eng+ita";
operation.delegate = self;

//only for test//
UIImage *testb = [img blackAndWhite];

Expand All @@ -62,28 +70,23 @@ -(void)recognizeImageWithTesseract:(UIImage *)img
//only for test//
});

Tesseract* tesseract = [[Tesseract alloc] initWithLanguage:@"eng+ita"];
tesseract.delegate = self;
[operation.tesseract setVariableValue:@"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" forKey:@"tessedit_char_whitelist"]; //limit search

[tesseract setVariableValue:@"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" forKey:@"tessedit_char_whitelist"]; //limit search

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

NSString *recognizedText = [tesseract recognizedText];

NSLog(@"%@", recognizedText);

dispatch_async(dispatch_get_main_queue(), ^{
[self.activityIndicator stopAnimating];

[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 = ^(Tesseract *tesseract) {
NSString *recognizedText = [tesseract recognizedText];

NSLog(@"%@", recognizedText);

[self.activityIndicator stopAnimating];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Tesseract OCR iOS" message:recognizedText delegate:nil cancelButtonTitle:@"Yeah!" otherButtonTitles:nil];
[alert show];

});

tesseract = nil; //deallocate and free all memory
};

[self.operationQueue addOperation:operation];
}

//DD TODO
Expand Down Expand Up @@ -117,18 +120,14 @@ - (IBAction)openCamera:(id)sender
}

- (IBAction)recognizeSampleImage:(id)sender {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
[self recognizeImageWithTesseract:[UIImage imageNamed:@"image_sample.jpg"]];
});
[self recognizeImageWithTesseract:[UIImage imageNamed:@"image_sample.jpg"]];
}

#pragma mark - UIImagePickerController Delegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = info[UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
[self recognizeImageWithTesseract:image];
});
[self recognizeImageWithTesseract:image];
}
@end
8 changes: 8 additions & 0 deletions Tesseract OCR iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
/* End PBXAggregateTarget section */

/* Begin PBXBuildFile section */
41A95DEE1A3AFBF30085093C /* RecognitionOperation.h in Headers */ = {isa = PBXBuildFile; fileRef = 41A95DEC1A3AFBF30085093C /* RecognitionOperation.h */; settings = {ATTRIBUTES = (Public, ); }; };
41A95DEF1A3AFBF30085093C /* RecognitionOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 41A95DED1A3AFBF30085093C /* RecognitionOperation.m */; };
6490748D198A5A5600D728CC /* UIImage+Filters.m in Sources */ = {isa = PBXBuildFile; fileRef = 6490748B198A5A5600D728CC /* UIImage+Filters.m */; };
64907491198A5DF200D728CC /* UIImage+Filters.h in Headers */ = {isa = PBXBuildFile; fileRef = 6490748A198A5A5600D728CC /* UIImage+Filters.h */; settings = {ATTRIBUTES = (Public, ); }; };
64A0293417307C1D002B12E7 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 64A0293217307C1D002B12E7 /* InfoPlist.strings */; };
Expand Down Expand Up @@ -175,6 +177,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
41A95DEC1A3AFBF30085093C /* RecognitionOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RecognitionOperation.h; sourceTree = "<group>"; };
41A95DED1A3AFBF30085093C /* RecognitionOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RecognitionOperation.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 @@ -361,6 +365,8 @@
64A029DD17307CED002B12E7 /* TesseractOCR.h */,
64A029D617307CD0002B12E7 /* Tesseract.h */,
64A029D717307CD0002B12E7 /* Tesseract.mm */,
41A95DEC1A3AFBF30085093C /* RecognitionOperation.h */,
41A95DED1A3AFBF30085093C /* RecognitionOperation.m */,
6490748A198A5A5600D728CC /* UIImage+Filters.h */,
6490748B198A5A5600D728CC /* UIImage+Filters.m */,
64A0293E17307CCF002B12E7 /* include */,
Expand Down Expand Up @@ -633,6 +639,7 @@
64A02A0817307D65002B12E7 /* blread.h in Headers */,
64A02A0917307D65002B12E7 /* boxread.h in Headers */,
64A02A0A17307D65002B12E7 /* boxword.h in Headers */,
41A95DEE1A3AFBF30085093C /* RecognitionOperation.h in Headers */,
64A02A0B17307D65002B12E7 /* capi.h in Headers */,
64A02A0C17307D65002B12E7 /* ccstruct.h in Headers */,
64A02A0D17307D65002B12E7 /* ccutil.h in Headers */,
Expand Down Expand Up @@ -822,6 +829,7 @@
buildActionMask = 2147483647;
files = (
64E9A53319194B47007EE74A /* Tesseract.mm in Sources */,
41A95DEF1A3AFBF30085093C /* RecognitionOperation.m in Sources */,
6490748D198A5A5600D728CC /* UIImage+Filters.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
23 changes: 23 additions & 0 deletions TesseractOCR/RecognitionOperation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// RecognitionOperation.h
// Tesseract OCR iOS
//
// Created by Nikolay Volosatov on 12.12.14.
// Copyright (c) 2014 Daniele Galiotto - www.g8production.com. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "TesseractOCR.h"

typedef void(^RecognitionOperationComplete)(Tesseract *tesseract);

@interface RecognitionOperation : NSOperation

@property (nonatomic, strong, readonly) Tesseract *tesseract;
@property (nonatomic, weak) id<TesseractDelegate> delegate;

@property (nonatomic, assign, readonly) CGFloat progress;

@property (nonatomic, copy) RecognitionOperationComplete recognitionCompleteBlock;

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

#import "RecognitionOperation.h"

@interface RecognitionOperation() <TesseractDelegate>

@property (nonatomic, strong, readwrite) Tesseract *tesseract;
@property (nonatomic, assign, readwrite) CGFloat progress;

@end

@implementation RecognitionOperation

- (id)init
{
self = [super init];
if (self != nil) {
_tesseract = [[Tesseract alloc] init];
_tesseract.delegate = self;

__weak __typeof(self) weakSelf = self;
self.completionBlock = ^{
__strong __typeof(weakSelf) strongSelf = weakSelf;

if (strongSelf.recognitionCompleteBlock != nil) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
strongSelf.recognitionCompleteBlock(strongSelf.tesseract);
}];
}
};
}
return self;
}

- (void)main
{
[self.tesseract recognize];
}

- (void)progressImageRecognitionForTesseract:(Tesseract*)tesseract
{
self.progress = self.tesseract.progress / 100.0f;
if ([self.delegate respondsToSelector:@selector(progressImageRecognitionForTesseract:)]) {
[self.delegate progressImageRecognitionForTesseract:tesseract];
}
}

- (BOOL)shouldCancelImageRecognitionForTesseract:(Tesseract*)tesseract
{
BOOL canceled = self.cancelled;
if ([self.delegate respondsToSelector:@selector(shouldCancelImageRecognitionForTesseract:)]) {
canceled = canceled || [self.delegate shouldCancelImageRecognitionForTesseract:tesseract];
}
return canceled;
}

@end
1 change: 1 addition & 0 deletions TesseractOCR/TesseractOCR.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
#define Tesseract_OCR_iOS_TesseractOCR_h

#import "Tesseract.h"
#import "RecognitionOperation.h"

#endif

0 comments on commit bd375ba

Please sign in to comment.