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);
}

17 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

  3. Malik Irfan

    Hi Artur,
    I am trying to use Tesseract in Xamarin Forms application. I am using Dependency Service for extracting MRZ string from Passport Image. In Android service method, I have a code:
    TesseractApi api = new TesseractApi(Android.App.Application.Context, AssetsDeployment.OncePerVersion);
    var isInitialized = await api.Init(“eng”);

    But IsInitialized is always false. Can you please guide on this?

    Thanks,
    Irfan

    Like

    Reply
      1. Malik Irfan

        Yes, I do have tessdata files. I created an issue in Github and already submitted my complete VS project there. Appreciate your prompt support.

        Like

  4. Andreas Ostlund

    HI!
    Is there a way to add a custom config file? I am looking to disable load_system_dawg but it needs to be set before init so I cannot use SetVariable(“load_system_dawg”, “0”); Looking at the gali8 documentation it seems that a config file path can be set in init but perhaps this is not available in your Xamarin version?

    Like

    Reply
  5. Gagan

    I am getting most of the result as weird symbols and time taken for result is also more.Can you help with this issue.

    Like

    Reply
      1. Gagan

        Hi i am using Tesseract Version 0.3.4 nuget package for Xamarin and my requirement is to get result line by line and my data is in a table format basically a receipt but when i scan it the result is not complete most of it has unnecessary symbols.How can i avoid that ?
        I am using
        var tessResult = await _tesseractApi.SetImage(imageBytes);
        where i am passing imageBytes after coverting it from Media File capturing by the iPads camera
        and getting result using this _tesseractApi.Text;

        Like

      2. Artur Shamsutdinov Post author

        Still, I want to see the images. If you read my article then you should know that just passing some image from a camera won’t give you any meaningful result. How do you process it?

        Like

Leave a comment