Tesseract: "Warning. Invalid resolution 0 dpi. Using 70 instead." with 321-Tesseract-4 branch

Created on 7 Mar 2018  Â·  13Comments  Â·  Source: charlesw/tesseract

I am using the 321-Tesseract-4 branch for LSTM support, I am capturing a section of the screen and creating a Bitmap object to pass to the TesseractEngine but am only getting the "Warning. Invalid resolution 0 dpi. Using 70 instead." error.

I tried setting Bitmap object resolutionwit "Bitmap.setResolution(96, 96);"
as well as trying "engine.SetVariable("image_default_resolution", input.VerticalResolution);"

When I used the Tesseract 3 nuget package I did not encounter this error in the same project. Is this an issue from the newer version of leptonica? or something I can work around programmatically?

Most helpful comment

@wilfredcho after changing to using Tesseract 4, I had kept the same PageSegMode setting that I had used for previous version:
```C#
var page = engine.Process(input, PageSegMode.LSTM)

or something of that nature. But in Tesseract 4, the options have changed to the following:

```C#
namespace Tesseract
{
    public enum PageSegMode
    {
        OsdOnly = 0,
        AutoOsd = 1,
        AutoOnly = 2,
        Auto = 3,
        SingleColumn = 4,
        SingleBlockVertText = 5,
        SingleBlock = 6,
        SingleLine = 7,
        SingleWord = 8,
        CircleWord = 9,
        SingleChar = 10,
        SparseText = 11,
        SparseTextOsd = 12,
        RawLine = 13,
        Count = 14
    }
}

All 13 comments

You’ll notice that the wrapper’s bitmap to Pix converter has a TODO comment for setting the resolution. It doesn’t set X/Y res. and I think this is what causes Tesseract to report the warning.

I see that now. So is this branch not usable then? I can't seem to get any kind of accurate OCR results using it while I get the dpi errors.

Are you certain there is a correlation between the warning and the accuracy? All branches have that problem. If I read the code right you can enhance Pix.Create() to pass the resolution and then use the LeptonicaAPI.cs wrapper’s pixSetResolution().

When I use the Tesseract Nuget package that is hosted in the Nuget database, the OCR works for the most part. But I wanted to try out Tesseract 4 LSTM for increased accuracy.

So is this due to a change in just the Leptonica API, I wonder?

I will dig around and see what I can do with modifying Pix.Create(). I'm not hopeful though, very novice over here.

Thanks for your suggestions!

I can’t say for sure, but I would look at your traindata files first and be certain you used the LSTM versions in conjunction with 4.0. That warning message has been there for quite a long time. The builds can turn off the messages so it is quite possible that the conditions that cause it are not changed by switching versions – the 4 version was obviously built with the warning enabled. Having said that it does feel like the wrapper should be passing the resolution when converting Bitmaps to Pix.

I've tried the traineddata files from both:
https://github.com/tesseract-ocr/tessdata
https://github.com/tesseract-ocr/tessdata_fast

I've also tried using EngineMode.Default and EngineMode.LstmOnly
I'm using these on very legible, small data set to test with, and its not getting ANY of it correct. Is there another set of traineddata you would recommend?

I'm wondering if the lack of dpi setting is causing the image to bee too scrunched or stretched to be legible.

I've implemented the overloaded Pix.Create() to pass the resolution and then and used
LeptonicaAPI.cs wrapper’s pixSetResolution() to set that before OCRing... I no longer get the dpi error! But still not getting legible OCR data. I wonder if I'm setting the DPI to the correct value by using:

public Pix Convert(Bitmap img)
{
var pixDepth = GetPixDepth(img.PixelFormat);
var dpiX = (int)img.HorizontalResolution;
var dpiY = (int)img.VerticalResolution;
var pix = Pix.Create(img.Width, img.Height, pixDepth, dpiX, dpiY);
...
}

Or if there really is an issue with training data.

        var handle = Interop.LeptonicaApi.Native.pixCreate(width, height, depth);
        if (handle == IntPtr.Zero) throw new InvalidOperationException("Failed to create pix, this normally occurs because the requested image size is too large, please check Standard Error Output.");
        Interop.LeptonicaApi.Native.pixSetResolution(handle, xres, yres);
        return Create(handle);

I had to use a snippet from the Pix constructor to turn handle (and Handle object) into a HandleRef object. But I have that part working now, no more DPI warning. But I still don't have a useable OCR reading. I'm going to try re-downloading the eng.trandata.

I found out that my problem was that the options for PageSegMode are different in the 4.0 wrapper.

Everything works as intended now!

@masterisk Can you elaborate the problem you faced with the PageSegMode please and which option you used?

@wilfredcho after changing to using Tesseract 4, I had kept the same PageSegMode setting that I had used for previous version:
```C#
var page = engine.Process(input, PageSegMode.LSTM)

or something of that nature. But in Tesseract 4, the options have changed to the following:

```C#
namespace Tesseract
{
    public enum PageSegMode
    {
        OsdOnly = 0,
        AutoOsd = 1,
        AutoOnly = 2,
        Auto = 3,
        SingleColumn = 4,
        SingleBlockVertText = 5,
        SingleBlock = 6,
        SingleLine = 7,
        SingleWord = 8,
        CircleWord = 9,
        SingleChar = 10,
        SparseText = 11,
        SparseTextOsd = 12,
        RawLine = 13,
        Count = 14
    }
}

Is there a way to detect PNM images resolution?

Was this page helpful?
0 / 5 - 0 ratings