Tesseract-ios is an Objective-C wrapper for Tesseract OCR.
- iOS SDK 6.0, iOS 5.0+ (there is no support for armv6)
- Tesseract and Leptonica libraries from the tesseract-ios-lib repo.
Here is the default workflow to extract text from an image:
- Instantiate Tesseract with data path and language
- Set variables (character set, …)
- Set the image to analyze
- Start recognition
- Get recognized text
#import "Tesseract.h"
Tesseract* tesseract = [[Tesseract alloc] initWithDataPath:@"tessdata" language:@"eng"];
[tesseract setVariableValue:@"tessedit_char_whitelist" forKey:@"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"];
[tesseract setImage:[UIImage imageNamed:@"image_sample.jpg"]];
[tesseract recognize];
NSLog(@"%@", [tesseract recognizedText]);
- (id)initWithDataPath:(NSString *)dataPath language:(NSString *)language
Initialize a new Tesseract
instance.
dataPath
: a relative path from the application bundle to the.traineddata
files. You can find these files from the tesseract downloads section.language
: language used for recognition. Ex:eng
. Tesseract will search for aeng.traineddata
file in thedataPath
directory.
Returns nil
if instanciation failed.
- (void)setVariableValue:(NSString *)value forKey:(NSString *)key
Set Tesseract variable key
to value
. See http://www.sk-spell.sk.cx/tesseract-ocr-en-variables for a complete (but not up-to-date) list.
For instance, use tessedit_char_whitelist
to restrict characters to a specific set.
- (void)setImage:(UIImage *)image
Set the image to recognize.
- (BOOL)setLanguage:(NSString *)language
Override the language defined with -initWithDataPath:language:
.
- (BOOL)recognize
Start text recognition. You might want to launch this process in background with NSObject
's -performSelectorInBackground:withObject:
.
- (NSString *)recognizedText
Get the text extracted from the image.