Install and use tesseract on iOS with tesseract-ios

Update 10/10/12

I updated tesseract-ios-lib with a (fake) armv7s slice. There is no need to remove armv7s from build architectures anymore. More informations about this change on my previous post.

Introduction

In my last article about how to compile tesseract for iOS SDK 6, I quickly posted two links to my github repos:

Some comments complained about the lack of guide to install and use this wrapper. I’m going to show you how to build a new iOS project with tesseract, from scratch.

Create the project

Create a simple Single View Application from XCode:

Name it whatever you want:

Project structure

Download tesseract-ios, tesseract-ios-lib, and your language trained data.

Then, open your project folder in Finder, and organise it like this:

Add “tesseract-ios” and “tesseract-ios-lib” in your project by creating groups:

Then, add “tessdata” by reference:

This step is extremely important, double check that you’re adding it by reference or your application will crash.

Project settings

Go to your project configuration, select your target, and click on “Build Settings”. Then, set the C++ compiler to default:

And set the build architecture to “armv7″ (instead of “armv7 armv7s”, as tesseract-ios-lib doesn’t contain an armv7s slice for now):

We’re ready to use tesseract.

Usage

Add an image called “image_sample.jpg” to your project.

Then, in ViewController.m, add #import tesseract.h and put the following lines in -viewDidLoad:

1
2
3
4
5
Tesseract* tesseract = [[Tesseract alloc] initWithDataPath:@"tessdata" language:@"eng"];
[tesseract setImage:[UIImage imageNamed:@"image_sample.jpg"]];
[tesseract recognize];

NSLog(@"%@", [tesseract recognizedText]);

Methods reference is available on the project repo.

You should get the image-extracted text in your console.

Project code

You can download the tutorial source code right here: TesseractSample.

Loïs Di Qual: I'm an iOS developer based in Bordeaux, France. This is my blog.

  • http://www.facebook.com/bouaziz Jonathan Bouaziz

    Thanks! Really easy to use and it works!
    Just got some issues as it doesn’t recognize text very well.. Is there any configuration to set up in order to improve the recognition?

  • Mike

    Thanks for the great information. If I create a new eng.traineddatafile, do I use the same eng.cube…. files? I really don’t know what those files are for.

  • Stan

    Thanks for creating this great tutorial! Could you do a step-by-step tutorial on creating tessearct-ocr libraries for Xcode? BTW I ran your code and error popped up. I’m running on Xcode 4.5.2 and noticed you need to have #import “Tesseract.h” in ViewController.m

  • Stan

    Hey Lois, I keep getting this error “Please call SetImage before attempting recognition” when compiling. Any Ideas?

    • ldiqual

      Well, I just tried to run TesseractSample.xcodeproj with Xcode 4.5.2 and I have no problem at all. Sorry :/

      • Tobi

        If I run the App in Xcode 4.6 in with the Simulator I have no problems but if I run the App on my iPhone 5 with iOS 6.1, I also get the same error: Please call SetImage before attempting recognition – Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘*** +[NSString stringWithUTF8String:]: NULL cString’

        Do you know what the problem could be?

        I do exactly the same 4 line codes like in your tudorial described. In Simulator it works on the device it doesn’t.

        • krishna

          Uncheck use auto layout option..

    • Roger

      I have the exact same error… Have you solved it? I’d appreciate it!

  • Alessandro Blasi

    Thanks! It works! Absolutely no problem with black and white pages, but with color page I’m having some trouble.. Btw, thanks again!!

  • Seph

    Great instructions for getting all set with Tesseract. Sure saved me several hours.

  • Thompson

    Complete newbie here and following your directions I have had complete success; this would have been nigh-on impossible without your post, thank you!

  • Costantino

    Hi, In my project I’m using OpenCV and I want to use Tessaract also. But, if I use “Compiler default” I get many errors in modules of OpenCV, while if I use libc++ I get errors in Tessaract function. How can I solve this? Thanks.

    • Bart Kappenburg

      I have exactly the same problem…. did you solve this?

    • salim

      i have also the same problem i am getting lex error than i change the eng.cube.lm file to eng.cube.data now i am geeting 46 errors

  • Tim

    Thanks dude!

  • Pat

    Article is absolutely amazing, thank you so much

  • DDuck

    Hi,

    Thank you for your work.

    Can you perhaps recommend an approach to get at the underlying data for the image positions of the recognized words? I know that Tesseract can do it, but it’s not exposed in the library.

  • Jeremy Langer

    I was having issues getting this working with iOS 7 (I got like 46 linker errors). To solve this, you need to set the “C++ Standard Library” to “libstdc++”. Thanks for the tutorial!

    • Alex

      Thanks man. You really help me!

    • ridho RPL Oye

      hi, could you tell me how did you do this ( i am a noob on xcode and obj-c )

      • ridho RPL Oye

        well.. it compiles after i added “libstdc++.6.dylib” in the build phases -> “link binary with libraries”

        • Wanbok

          You save my several hours! Thank you so much. :)

    • Joel

      Wow thanks :)

    • Hugo

      You rocks dude !!!

  • Salim Ullah

    i am using the default compiler i am getting the lex error

  • salim

    The sample app work for me but when i implemented the code in my app i am getting lex error

  • Sujit Kaulavkar

    I am working with iOS 7. I am getting following error after adding the 2 files and changing the setting as per mentioned.
    Error message : clang: error: linker command failed with exit code 1 (use -v to see invocation)

  • Srikumaran

    Hi Friend,

    i have problem and getting lex error on eng.cube.lm and here i attached the images what i got errors….!
    So please Help me dude….! Give Solution for this errors….!

    • Obaid Al Mashjari

      isn’t much better to make a video and uploaded on you tube ?

    • Manjot Kaur

      @srikumaran:disqus Hello , could you please Explain how you solve this problem . For lex error on eng.cube.lm .. ?

  • nferocious76

    yes this also help me. I am unable to compile my code but thanks to this thread i have fixed it. I only need to remove arm7s and 6 because it isn’t supported. :D

  • Saleh Shafi

    Excellent tutorial, worked very well or me! Thanks :)

  • Sovan Das Maity

    when i try to add tesseract-ios, tesseract-ios-lib, and language trained data through drag an drop ,after then when going to compile there has apple linker error. How to add tesseract-ios.tesseract-ios-lib and language trained data in my project and how to link …
    can i get brief description please help me …..

  • Andrei

    I am trying to do this with Xcode 6.2, but I get linking errors. Is anyone still using this?

  • ngvlc

    tesseract ocr output is not showing sometimes shows null