1

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;
}
Lupurus
  • 3,618
  • 2
  • 29
  • 59
  • Does it work without block? – IvanRublev Apr 18 '14 at 10:27
  • Yes, no problem without dispatch_async (instead of the fact, that the application is blocked) – Lupurus Apr 18 '14 at 10:31
  • I believe you know that if you are doing anything with the string in your handler for UI components, like setting the string to any ui components etc, it should be done on the main thread. – Zen Apr 18 '14 at 10:56
  • You should show where you create "tess." -- I have a hard time believing it's an ARC problem if tess doesn't inherit from NSObject. Tess wouldn't even have a referenceCount for ARC to mangle. – stevesliva Apr 18 '14 at 16:59

1 Answers1

1

Thanks for all your help.

I recently found the solution. I did the complete code where I create my Tesseract class and where I call the getText method into the dispatch block. Now it works.

Lupurus
  • 3,618
  • 2
  • 29
  • 59
  • 2
    Please update your answer to include your working code! Otherwise anyone who comes across this question in the future won’t benefit from what you’ve learned. – bdesham Apr 18 '14 at 18:41