Hi guys, and welcome to the last part of our Java OCR tutorial! We’re going to learn how to deploy the Sampler app to our Android phone. The Sampler application allows you to take images of characters / sets of characters to be trained. This is going to be a really short post and I’m really excited to share another OCR engine with you guys. So let’s get started.

Creating A Project From Existing Source

The Sampler app can be found here : /trunk/demos/sampler. Simply create a new Android Project and make sure you check ‘Create project from existing source’ checkbox. You could also import it to your workspace, either way works.

Creating a project from source

Again, you’ll need to link some classes / libraries

  • javaocr-core – /trunk/core/src/main/src/java
  • javaocr-camera-utils – /trunk/demos/camera-utils/src/main/java

Examining The Code

No OCR happening here obviously, this app is for sampling. What we’re interested in is the method saveSample() This is where all the SD card storing action is happening.

final DataOutputStream dos = new DataOutputStream(new FileOutputStream(outdir.getAbsolutePath() + "/" + exp + "_" + (System.currentTimeMillis() / 1000) + ".dat"));

You’ll notice right away that it creates a file with a filename based on the characters you are sampling. So if you’re taking samples of the characters ABC, it’ll create a file with a filename ABC_12*timestamp*514.dat

This can be a real issue when sampling special characters, Android doesn’t allow you to create files with special characters as file names. Bummer.

Putting It All Together

Take samples with the Sampler, get all the .dat files from the SD card, put them in a folder in your PC. Set the trainer to point to that folder via DAT_SOURCE_DIR variable. Retrieve the moments.json and freespaces.json generated by the Trainer. Put these files inside your Recognizer app – /res/raw/.

Note: In the Recognizer app, make sure you’re pointing to the right trained files. You can set them in the readClusterData() method.

InputStream inputStream = getResources().openRawResource(R.raw.freespaces)
inputStream = getResources().openRawResource(R.raw.moments);

And that’s basically it.

Final Thoughts

Java OCR is a really solid OCR engine, and it’s native to Java, so it’s ideal if you want to integrate an OCR engine to your Java project without having to wrap it around JNI, or if you want to learn how OCR works.

The problem is, it’s not fully ported to Android and requires serious sampling and training. After more than a hundred samples, I decided to use Tesseract instead.

Anyway, I’ll be covering a few OCR engines in the following days. Hope you guys enjoyed Java OCR.

It’s been a while since my last post, and I promised that I would teach you guys how to train JavaOCR. Some even took the time to send me messages in Facebook. I have to apologize, was busy preparing for my graduation last March (now I’m employed, yay me). Anyways, this post is all about the JavaOCR trainer.

By the way, I’m doing the Trainer tutorial first, Sampler is pretty straight forward. I promise I’ll be done with the tutorial today. (I hope)

Trainer

Unlike the Recognizer & Sampler, the Trainer is a Java project. I believe it’s possible to port the trainer to Android, but that’s not what we’re going to do. You can find the trainer at /trunk/demos/trainer

What I did was create a new Java project in Eclipse (Github’d) and copied the Trainer class. I did some minor changes to the original trainer, but I’ll get to that later.

As with the Recognizer, it requires a few libraries to import:

import net.sourceforge.javaocr.Image;
import net.sourceforge.javaocr.cluster.FeatureExtractor;
import net.sourceforge.javaocr.matcher.FreeSpacesMatcher;
import net.sourceforge.javaocr.matcher.Match;
import net.sourceforge.javaocr.matcher.MatcherUtil;
import net.sourceforge.javaocr.matcher.MetricContainer;
import net.sourceforge.javaocr.matcher.MetricMatcher;
import net.sourceforge.javaocr.ocr.PixelImage;
import net.sourceforge.javaocr.plugin.cluster.MahalanobisDistanceCluster;
import net.sourceforge.javaocr.plugin.cluster.extractor.FreeSpacesExtractor;
import net.sourceforge.javaocr.plugin.moment.HuMoments;
import com.google.gson.stream.JsonWriter;
import de.pribluda.android.jsonmarshaller.JSONMarshaller;

And you can find them here:

javaocr-core – /trunk/core/src/main/src/java
moment plugin – /trunk/plugins/moment/src/main/java
cluster plugin – /trunk/plugins/cluster/src/main/java
GSONhttp://code.google.com/p/google-gson/downloads/list
JSONUnmarshallerhttps://github.com/ko5tik/jsonserializer
Apache Commons (for MahalanobisDistanceCluster class)http://commons.apache.org/math/download_math.cgi

Simply link / add / copy these to your project.

Note: I’ve already added the GSON, JSONUnmarshaller, and Apache Commons to my own trainer.

For Simplicity, Let’s Use derp-trainer

You don’t really need to use my trainer, there isn’t that much difference from the original one. But for the sake of this tutorial, I’m going to use my trainer. You can get it at derp-trainer@github.

Here are the things you should take note of:

private static final String DAT_SOURCE_DIR
private static final String FREESPACES_OUTPUT
private static final String MOMENTS_OUTPUT
public static final String POSSIBLE

DAT_SOURCE_DIR is the directory where our trainer will get samples (samples are taken with the Sampler app). FREESPACES_OUTPUT and MOMENTS_OUTPUT are the trained files generated by the trainer which will be used by the Recognizer. POSSIBLE is basically the string of characters you will be training (the characters in your sample data).

Simply change the values and viola, you’re all set to train. Now, it’s time to get some samples! Next post will be about the Sampler app.

Java OCR and Android

Posted: January 14, 2012 in Android
Tags: , , ,

I’ve been scouring the web for OCR engines suitable for Android and have come up with a few options. However, most of these engines seem to be written in C/C++ or low level languages (compared to Java). Having little background in these languages; I went with JavaOCR.

Here’s a little tutorial on how to deploy the recognizer demo in your Android phone.

  1. Download the source: http://javaocr.svn.sourceforge.net/viewvc/javaocr/ and extract it.
  2. In Eclipse, create a new Android project (from existing source), you can find it in /trunk/demos/recognizer
  3. You’ll notice there’ll be a few errors around these lines (see code below). Don’t worry, these are just libraries/classes that need importing.

    import net.sf.javaocr.demos.android.R;
    import net.sf.javaocr.demos.android.utils.camera.CameraManager;
    import net.sf.javaocr.demos.android.utils.image.ImageProcessor;
    import net.sf.javaocr.demos.android.utils.image.IntegralImageSlicer;
    import net.sf.javaocr.demos.android.utils.image.SauvolaImageProcessor;
    import net.sourceforge.javaocr.Image;
    import net.sourceforge.javaocr.cluster.FeatureExtractor;
    import net.sourceforge.javaocr.filter.SauvolaBinarisationFilter;
    import net.sourceforge.javaocr.filter.ThresholdFilter;
    import net.sourceforge.javaocr.matcher.FreeSpacesContainer;
    import net.sourceforge.javaocr.matcher.FreeSpacesMatcher;
    import net.sourceforge.javaocr.matcher.Match;
    import net.sourceforge.javaocr.matcher.MatcherUtil;
    import net.sourceforge.javaocr.matcher.MetricMatcher;
    import net.sourceforge.javaocr.ocr.PixelImage;
    import net.sourceforge.javaocr.plugin.cluster.MahalanobisClusterContainer;
    import net.sourceforge.javaocr.plugin.cluster.extractor.FreeSpacesExtractor;
    import net.sourceforge.javaocr.plugin.moment.HuMoments;
    import com.google.gson.stream.JsonReader;
    import de.pribluda.android.jsonmarshaller.JSONUnmarshaller;


    Simply link the classes / paste the libraries to your project. You can find the packages here:

    javaocr-core – /trunk/core/src/main/src/java
    camera utilities – /trunk/demos/camera-utils/src/main/java
    moment plugin – /trunk/plugins/moment/src/main/java
    cluster plugin – /trunk/plugins/cluster/src/main/java
    GSONhttp://code.google.com/p/google-gson/downloads/list
    JSONUnmarshallerhttps://github.com/ko5tik/jsonserializer
    Apache Commons (for MahalanobisDistanceCluster class)http://commons.apache.org/math/download_math.cgi

    Linking the source:
    Linking sources

    Make sure to exclude the classes in the javaocr.scanner package (excluding the classes in the accuracy package), these classes use components from Java that is not part of Android.

  4. Deploy the app in your phone! Note: I’m getting errors deploying in the emulator, but not in the phone.

So that’s it. You can try out the recognizer app. You’ll notice right away that it only recognizes numbers, plus it isn’t really accurate. This is where training comes in. That will be in a different post! Thanks for the team who worked hard on this OCR engine.

I like pimping stuff, my car, my personal sites… so it only makes sense to pimp my favorite IDE!

These are in no specific order:

I like pimping my editor, and what better way to pimp it than to change it’s default colors! Eclipse Color Theme is simply amazing; there are several themes available. (Monokai being my favorite) Plus, you can create your own theme and share it to the world. Sharing is always nice. 

Ah Git… I like Git, so it only makes sense to use it in conjunction with my favorite IDE right? Of course!

I don’t really like SVN; but right now, I don’t really have a choice (my current collab-project is using SVN)… don’t get me wrong, SVN is great, I just prefer Git.

I recently bought an Android phone and I want to make apps for it. Android is just awesome! Open-source is awesome!

Vi is the best text editor in the world. Nuff said. (this plugin, however, does not support regex yet)

This, in my opinion, makes Netbeans obsolete. No offense to Netbeans users out there! (well, maybe a little bit)

Probably the best (IMHO) “bloated” IDE for Ruby on Rails. Old-school terminal is still the way to go.

 

And that’s just about it I guess!

We’re doing it live!

Posted: January 13, 2012 in Uncategorized

Hi, my name is Kevin. I started this blog so I can share stuff that I’ve learned. It’s always nice to share.