最近接觸了一個關於圖文識別的項目,項目組決定使用Tesseract,大概查了一下可是對於ios的支持貌似不是很好,如下是爲Tesseract包裝過的oc版本。php
原文連接:https://github.com/ldiqual/tesseract-ioshtml
Tesseract-ios is an Objective-C wrapper for Tesseract OCR.ios
This project couldn't exist without the Ângelo Suzuki's blog post. A lot of code came from his article.c++
tesseract-ios
as a group, and tessdata
by reference to your project:C++ Standard Library => libstdc++
:Here is the default workflow to extract text from an image:git
#import "Tesseract.h" Tesseract* tesseract = [[Tesseract alloc] initWithDataPath:@"tessdata" language:@"eng"]; [tesseract setVariableValue:@"0123456789" forKey:@"tessedit_char_whitelist"]; [tesseract setImage:[UIImage imageNamed:@"image_sample.jpg"]]; [tesseract recognize]; NSLog(@"%@", [tesseract recognizedText]); [tesseract clear];
- (id)initWithDataPath:(NSString *)dataPath language:(NSString *)language
github
Initialize a new Tesseract
instance.web
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 a eng.traineddata
file in the dataPath
directory.Returns nil
if instanciation failed.app
- (void)setVariableValue:(NSString *)value forKey:(NSString *)key
ide
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.wordpress
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.
- (void) clear
Clears Tesseract object after text has been recognized from image. Preventing memory leaks.
備忘:看過一篇博文提到:爲了提升效率須要對圖片進行預處理(二值化、灰度、傾斜校訂和圖片切割),傾斜校訂和圖片切割能夠用openCV的庫處理
博文連接:http://www.cocoachina.com/bbs/read.php?tid=123463 (該博文有demo這裏就很少鏈了)
比較有用的連接: