I want to use Tesseract
in my Mac App, but recognizing text blocks the complete application. Therefore I tried to use a dispatch_async
, but my app crashes with a EXC_BAD_ACCESS (at "tess->Recognize(0)".
Here is the method, I'm using:
- (void)getText:(void(^)(NSString *response))handler {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
tess->Recognize(0);
char *text = tess->GetUTF8Text();
NSString *string = [NSString stringWithUTF8String:text];
delete [] text;
handler(string);
});
}
I think it's a problem with ARC and the c++ instance of tesseract. Using __block
didn't help.
UPDATE:
Here is the working code. Hopefully this will maybe help somebody.
AppDelegate.m
...
[self processPage:myImage withCompletionHandler:^(NSString *text) {
NSLog(@"%@", text);
}];
...
- (void)processPage:(NSImage*)image withCompletionHandler:(void(^)(NSString *text))completion {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
Tesseract *tess = [[Tesseract alloc] initWithLanguage:@"deu"];
[tess setImageWithImage:image];
NSString *text = [tess getText];
completion(text);
});
}
Tesseract.mm
- (NSString*)getText {
tess->Recognize(0);
char *text = tess->GetUTF8Text();
NSString *string = [NSString stringWithUTF8String:text];
delete [] text;
return string;
}