2
votes

I am developing an Android OCR project. In my project, I am using tess-two. But I am having problem with initializing trainned data from tessdata folder for tess-two.

I installed tess-two in grandle like this

dependencies {
    compile 'com.rmtheis:tess-two:6.0.2'
}

Then I tried detect text from bitmap like this

public String detectText(Bitmap bitmap) {

        TessBaseAPI tessBaseAPI = new TessBaseAPI();
        tessBaseAPI.setDebug(true);
        tessBaseAPI.init(DATA_PATH, "eng"); //Init the Tess with the trained data file, with english language

        //For example if we want to only detect numbers
        tessBaseAPI.setVariable(TessBaseAPI.VAR_CHAR_WHITELIST, "1234567890");
        tessBaseAPI.setVariable(TessBaseAPI.VAR_CHAR_BLACKLIST, "!@#$%^&*()_+=-qwertyuiop[]}{POIU" +
                "YTREWQasdASDfghFGHjklJKLl;L:'\"\\|~`xcvXCVbnmBNM,./<>?");


        tessBaseAPI.setImage(bitmap);

        String text = tessBaseAPI.getUTF8Text();

        tessBaseAPI.end();

        return text;
    }

That is fired when a button is clicked. My activity includes only that function. The problem is with data path variable.

This is my project structure

enter image description here

Yes, I added two tessdata folders to demonstrate two scenarios.

First try. I set DATA_PATH like this

DATA_PATH = Environment.getRootDirectory().getPath()+"/data/";

That gives me "Data path does not exist!" error. So I found these solutions Tesseract OCR Android tessdata directory not found and Get path of data directory(android) .

So I tried to set DATA_PATH like this

DATA_PATH = Environment.getRootDirectory().getPath();

It gives me "Data path must contain subfolder tessdata!" error.

Then I tried this

DATA_PATH = Environment.getRootDirectory().getPath()+"/data/tessdata/";

Then I gives me this error again "Data path does not exist!".

Then I tried this way

DATA_PATH = Environment.getRootDirectory().getPath()+"/tessdata/";

It gives me this error "Data path does not exist!".

So all the ways I tried are not working. How can I initialize tessdata folder correctly? I have no idea to set the path for it.

2
In that link, it is saying to use like this baseApi.init("/mnt/sdcard/tesseract", "eng"); . It is from sdcard. , my tessdata folder is in project parent directory. So in my cause, I must reference like this? "/tessdata".Wai Yan Hein
I tried it and not working.Wai Yan Hein
I tried this "/data" and this "/"Wai Yan Hein
When I tried this "/data", it is giving me this error "Data path must contain subfolder tessdata!". Please how can I fix it?Wai Yan Hein
I tried this as well "/data/" according to doc.Wai Yan Hein

2 Answers

0
votes

Copy your tessdata directory in your internal storage or phone memory. Then put your DATA_PATH variable as

DATA_PATH = Environment.getExternalStorageDirectory() + "/";

And then try to run it. I've recently found out that Environment.getExternalStorageDirectory() may not always point to your SD card or your external storage. In my case it actually pointed to my internal storage and thus copying tessdata folder in my internal storage with the above DATA_PATH worked fine for me. So, this should directly point to your internal memory which contains the tessdata folder.

0
votes

You could put the files in assets folder and then copying them to somewhere like /storage/emulated/0/tesseract/tessdata/ using AssetManager.