1

I am using tesseract ocr in my app. In order to use tesseract i need to use several language files that are located at a directory called - 'tessdata'.

This is my method code:

    public String detectText(Bitmap bitmap) {
    TessBaseAPI tessBaseAPI = new TessBaseAPI();
    String DATA_PATH = Environment.getRootDirectory().getPath() + "/tessdata/";

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

    tessBaseAPI.setImage(bitmap);

    String text = tessBaseAPI.getUTF8Text();

    tessBaseAPI.end();

    return text;
}

I've used many variations of:

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

and every time the app fails with "path not found" exception. I need a good way to put this directory in the android phone and get the path of it regardless of which phone it is. Right now the 'tessdata' directory can be found at the app root directory.

How can i do that?

Montoya
  • 2,819
  • 3
  • 37
  • 65
  • What do you mean "located at a sub directory called - 'tessdata'." A subdirectory of what? The android project? You can load files contained in the res subdirectory of the app using `ctx.getResources().openRawResource(R.raw.);`, for example, to get an input stream to a file in `res/raw/.` – rcbevans Sep 14 '15 at 15:05
  • try this http://stackoverflow.com/questions/25905649/absolute-path-to-directory-in-the-internal-storage-in-android – VIjay J Sep 14 '15 at 15:06
  • 1
    `getRootDirectory()` is almost certainly a problem unless the directory trying to be opened it at `/tessdata/` as from http://developer.android.com/reference/android/os/Environment.html#getRootDirectory() you can see `getRootDirectory()` returns the root of the "system" partition holding the core Android OS – rcbevans Sep 14 '15 at 15:09
  • I've edited my question. The comment above didn't solved my problem. – Montoya Sep 14 '15 at 15:24
  • String DATA_PATH = Environment.getRootDirectory().getPath() check what string is this returning? May be you won't need "/" as a prefix. Not sure though. – Techidiot Sep 14 '15 at 15:32

2 Answers2

2

Don't include "/tessdata/" in your DATA_PATH variable--just leave that part off, but be sure that subfolder exists within the directory specified by DATA_PATH.

rmtheis
  • 5,992
  • 12
  • 61
  • 78
0

From sourcecode TessBaseAPI#init

public boolean init(String datapath, String language) {
    ...
    if (!datapath.endsWith(File.separator))
        datapath += File.separator;

    File tessdata = new File(datapath + "tessdata");
    if (!tessdata.exists() || !tessdata.isDirectory())
        throw new IllegalArgumentException("Data path must contain subfolder tessdata!");

That means

  • the tessdata-subdirectory must exist.
  • init gets the parent-folder of "tessdata"

You can create it like this:

File dataPath = Environment.getDataDirectory(); 
   // or any other dir where you app has file write permissions

File tessSubDir = new File(dataPath,"tessdata");

tessSubDir.mkdirs(); // create if it does not exist

tessBaseAPI.init(dataPath.getAbsolutePath(), "eng");
k3b
  • 14,517
  • 7
  • 53
  • 85
  • 2
    I've used that code and it throws an exception: Caused by: java.lang.IllegalArgumentException: Data path must contain subfolder tessdata! I have a 'tessdata' folder at the root app directory. – Montoya Sep 14 '15 at 16:20