Rene Dohan Rene Dohan - 4 months ago 69
Android Question

Tesseract tessdata folder not found when put in assets folder

I read everywhere that tessdata should be on external storage and there should be

eng.traineddata
for
eng
language. But how to package application to device so it will be there ? I don't know about this is possible so I copied
tessdata
to
assets
folder. I can access it by
context().getAssets().openFd().open("tessdata/eng.traineddata")
but when I when I try !
new File("file:///android_asset/tessdata/").exists()
it is not found and I get exception from tesseract library when initialising , that it is not found. I am using Genymotion as emulator and somehow that direct path is not correct. So I don't know what to do.

Answer

OK I have seen another example on github an he is copying traineddata from assets to sdcard if data there do not exist. So this is the only way probably.

 private void initializeTessApi() {
    _tessApi = new TessBaseAPI();
    File data = new File(application().dataDir(), TESSDATA);
    File traineddataFile = new File(data, LANG + ".traineddata");
    if (!traineddataFile.exists()) {
        try {
            data.mkdirs();
            copyAndClose(context().getAssets().open(TESSDATA + "/" + LANG + ".traineddata"), new FileOutputStream(traineddataFile));
            info("Copied", LANG, "traineddata");
        } catch (IOException e) {
            error(e, "Was unable to copy", LANG, "traineddata ");
        }
    }
    _tessApi.init(application().dataDir().getAbsolutePath(), LANG);
}
Comments