Making an OCR app for Android using Tesseract.


Star on GitHub

Recently I was playing with OCR library by google called as “Tesseract” (cool name for a library!).

App in action.
App in action.

 

Screenshot_2015-08-29-23-08-51

 

 

 

 

 

 

 

 

It was a fun experience. This post shows how you can make a simple OCR app in Android using Tesseract.

We will be using Tess-Two a fork of Tesseract with some additional tools like Liptonica which is an image processing library.

If you want an even easier way to get started with OCR on Android you can try this library built by me. Easy OCR Library. Usage instructions are in the ReadMe.md file there.

Anyways, moving forward I am using Android Studio on Ubuntu 64 bit machine here.

Step 1 : 

Clone the library Tess-Two.


git clone git://github.com/rmtheis/tess-two tess

Step 2 :

Now we need to build  the library.

For building we will need Android NDK.


cd tess
cd tess-two
ndk-build
android update project --path .
ant release

Building may take some time so be patient. Don’t press ctrl+c too soon 😛 .

Step 3  :

Yay! Time to use the library in Android Project.

Copy the tess/tess-two folder into the root folder of your application project.

Step 4 : 

In the tess-two folder you just pasted. Add build.gradle file as Android Studio uses gradle build system.

Add following gradle script in the file.


buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
    }
}

apply plugin: 'android-library'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 22
    }

    sourceSets.main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        resources.srcDirs = ['src']
        res.srcDirs = ['res']
        jniLibs.srcDirs = ['libs']
    }
}

Step 5 : 

Add the following line in project.settings file.

include ':tess-two'

Step 6 :

Now we have successfully included the Tess-Two library in our project and we are ready to use it.

First we need to capture the picture itself. You can use something like this code sample taken from Easy OCR Library.


public void takePicture(){
        Intent e = new Intent("android.media.action.IMAGE_CAPTURE");
        this.filePathOriginal = FileUtils.getDirectory(this.directoryPathOriginal) + File.separator + Calendar.getInstance().getTimeInMillis() + ".jpg";
        e.putExtra("output", Uri.fromFile(new File(this.filePathOriginal)));

        startActivity(e);
    }

Or you can find the code here.

We will also downscale the image a little so that the recognition is fast.

You  can use following code sample from again Easy OCR Library


 private Bitmap getBitmapFromPath() {
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 4;
        Bitmap bitmap = BitmapFactory.decodeFile(this.filePath, bmOptions);
        return bitmap;
    }

Step 7 :

Final step. Recognize the text using the library API.


 private String scanImage(){
        TessBaseAPI baseApi = new TessBaseAPI();
        Log.d(Config.TAG, "Data path : " + FileUtils.getDirectory(this.directoryPath));
        baseApi.init(FileUtils.getDirectory(this.directoryPath) + "/", this.trainedDataCode);
        baseApi.setImage(this.mBitmap);
        String recognizedText = baseApi.getUTF8Text();
        baseApi.end();

        return recognizedText;
    }


Again I would recommend using the Easy OCR Library if you are having facing any problem.

That library has many features :

  1. Very easy setup.
  2. Handles all the image processing part in a background thread.
  3. Provides very interface with relative callbacks for the functions of the library.

 

Advertisements

How to build Tesseract OCR library for Android Studio?

Star On GitHub

If you ever tried to create an OCR app for Android you must have stumbled upon the OCR library by Google Tesseract. And then the problems began.

To use the library in your project you first need to build it. But building the library to be compatible with gradle, which is the new build system for Android projects is little not so easily stated anywhere in the library manual.

When I tried to build the library, it took me freaking 9 hours to figure all the how tos?

So, here I am helping you to save your precious hours. (Don’t waste these watching late night infomercials for god’s sake! ).

Here we go.

Step 1 : 

The first step. Download the NDK. That is used to build the library.

Download it from here. It is around 300+ MB so keep your net plan nourished.

Step 2 : 

Better way to go is to use  a fork of Tesseract, Tess-Two. Tess-Two can be found on GitHub.

Execute following commands to build the library Tess-Two using NDK.


git clone git://github.com/rmtheis/tess-two tess
cd tess
cd tess-two
ndk-build
android update project --path .
ant release

We can live without building eyes-two for this time.

Building will take few minutes. After you have successfully built tess-two, give yourself a little treat. You are almost done.

Step 3 :

Now you are ready to use the library in your Android project.

Copy the tess-two folder (tess/tess-two), in the main folder of the application project.

Suppose name of your application is “MyApp” copy the folder at “MyApp/”.

Step 4 :

Now its time to play the trick. The library was build using ANT. But Android projects use Gradle these days. Interesting…

We need to add a “build.gradle” file at location “tess-two/”.

The build file can be found here.

Then include following line in project.settings file.

include ':tess-two'

Step 5 : 

Build the project and you are just one step away from being done.

Step 6 : 

The most important step. After completing steps 1-5, throw your hands up in the air! 😀 😀 You are now done!

Ready to harness the power of OCR.

 


https://buttons.github.io/buttons.js