Tesseract OCR for Xamarin (part 2)

In the previous blog post I brought up the brand new Tesseract OCR wrapper for Xamarin. In this post I will give you some details about how to increase speed or/and quality of image recognition.

1. Pass OcrEngineMode to the Init method

public interface ITesseractApi
{
    Task<bool> Init (string lang, OcrEngineMode? mode = null);
}

public enum OcrEngineMode
{
    /// <summary>
    /// Run Tesseract only - fastest
    /// </summary>
    TesseractOnly,
    /// <summary>
    /// Run Cube only - better accuracy, but slower
    /// </summary>
    CubeOnly,
    /// <summary>
    /// Run both and combine results - best accuracy
    /// </summary>
    TesseractCubeCombined
}

By default OcrEngineMode.TesseractOnly is used. Sometimes the outcome of using different modes could be quite weird so I recommend either not to use any of the values or to use OcrEngineMode.TesseractCubeCombined if you want to achieve the best accuracy.

2. Set proper segmentation mode

public interface ITesseractApi
{
    void SetPageSegmentationMode (PageSegmentationMode mode);
}

Here are the result of using different combinations of engine and page segmentation modes. You can come across some nice and some useless results in the list.

Engine mode: TesseractOnly

Page segmentation mode: AutoOsd, AutoOnly, Auto, SingleColumn, SparseTextOsd the quick brown fox jumps over the lazy dog- THE QUICK BROUN FOX JUHPS OVER THE LAZY DOG-
Page segmentation mode: SingleBlockVertText
ex 5.. Mm XM O fv.FY hz Z aNA W1 UL r O bmwm kt .Kr “R um MW 0.0 Q0 es ES h twmm u U -J J
Page segmentation mode: SingleBlock
the quick brown fox jumps over the lazy dog- THE QUICK BROUN FOX JUMPS OVER THE LAZY DOG.
Page segmentation mode: SingleLine
the quick bruwh fox jumps over the lazy dog- THE QUICK BROUN FOX JUHPS OVER THE LAZY DOG-
Page segmentation mode: SingleWord
j:r1”I:tEh§eEqéuEiECKk:EbEFODEIWEn:EfODgX:::
Page segmentation mode: CircleWord, OsdOnly
No result
Page segmentation mode: SingleChar
m
Page segmentation mode: SparseText
the quick bruwh fox jumps over the lazy dog- THE QUICK BROUN FOX JUHPS OVER THE LAZY DOG-

Engine mode: CubeOnly

Page segmentation mode: AutoOsd, AutoOnly, Auto, SingleColumn, SingleBlock, SingleLine, SparseText, SparseTextOsd
the quick brtngn fax jumps i3Vi2r' the lazy dog. THE ttuICK BR()l1N FOX Junps ()NIER THE LAZY DOG.
Page segmentation mode: SingleBlockVertText, SingleWord, CircleWord, SingleChar, OsdOnly
No result

Engine mode: TesseractCubeCombined

Page segmentation mode: OsdOnly, CircleWord
No result
Page segmentation mode: AutoOsd, AutoOnly, Auto, SingleColumn, SparseTextOsd
the quick brown fax jumps over the lazy dog. THE QUICK BROUN FOX JUHPS OVER THE LAZY DOG.
Page segmentation mode: SingleBlockVertText
ex 5.. Mm XM O fv.FY hz Z aNA W1 UL r O bmwm kt .Kr “R um MW 0.0 Q0 es ES h twmm u U -J J
Page segmentation mode: SingleBlock
the quick brown fax jumps over the lazy dog. THE QUICK BROUN FOX JUMPS OVER THE LAZY DOG.
Page segmentation mode: SingleLine, SparseText
the quick bruwh fax jumps over the lazy dog. THE QUICK BROUN FOX JUHPS OVER THE LAZY DOG.
Page segmentation mode: SingleWord
j:r1”I:tEh§eEqéuEiECKk:EbEFODEIWEn:EfODgX:::
Page segmentation mode: SingleChar
m

For sure there is no silver bullet for the Tesseract engine and you can get different results on your samples. But there are safer options like AutoOsd, AutoOnly, Auto, SparseText unless you know why you are using this particular mode. BTW, if you take a closer look on all these results you’ll find that there is no fully correct result among them.

3. Define black- and white-list

public interface ITesseractApi
{
    void SetWhitelist (string whitelist);
    void SetBlacklist (string blacklist);
}

Thus you can avoid unrelated symbols. For example if trying to recognise a car registration number you’ll need only capital letters and digits. You don’t have to change any of these configurations but if you go after the best quality you’ll have to find the best combination of the parameters depending on the set of data you have.

4. Use platform-specific image formats

This is not about performance or quality, just about ease of using the engine when you are outside of the portable code.
On iOS you can use CoreImage.CIImage.

public class TesseractApi
{
    public async Task<bool> Recognise (CIImage image);
}

On Android you can use Android.Graphics.Bitmap.

public class TesseractApi
{
    public async Task<bool> Recognise (Bitmap image);
}
Advertisements

6 thoughts on “Tesseract OCR for Xamarin (part 2)

  1. Pingback: Tesseract OCR for Xamarin (part 1) | Artur Shamsutdinov's blog

  2. Vlada

    Hi,

    can you please explain a bit more how to set page segmentation mode.
    I am working on some test project in Xamarin.Android.

    Tnx
    Vlad

    Like

    Reply
      1. Vlada

        Tried like this:
        TesseractApi api = new TesseractApi(this, AssetsDeployment.OncePerVersion);
        await api.Init(“eng”);
        api.SetPageSegmentationMode(Tesseract.PageSegmentationMode.SingleColumn);
        await api.SetImage(Assets.Open(“24.png”));
        string text = api.Text;

        Bur no matter what page segmentation option I select result is the same.

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s