Could not initialize Tesseract API with language=eng

urpanjwani picture urpanjwani · Jul 10, 2016 · Viewed 8.5k times · Source

I am working on an Android app that requires OCR. I have decided to use Tesseract as API but I keep on getting this error:

E/Tesseract(native): Could not initialize Tesseract API with language=eng!

  1. I have already copied file "eng.traineddata" to the location.
  2. I am using Android Studio 2.1.2 (SDK 23)
  3. Testing on device with API 22 Android Lollipop 5.1.1 (Read about Permission issue on Marshmallow)

Here is the code I am using:

public void reads(View view) {

  TextView textView = (TextView) findViewById(R.id.textView);

  int rotation = 0;

  try {
    ExifInterface exifInterface = new ExifInterface(mCurrentPhotoPath);
    int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);

    switch (orientation){
      case ExifInterface.ORIENTATION_ROTATE_90: rotation = 90; break;
      case ExifInterface.ORIENTATION_ROTATE_180: rotation = 180; break;
      case ExifInterface.ORIENTATION_ROTATE_270: rotation = 270; break;
    }
  } catch(Exception e) {

  }

  int w = imageBitmap.getWidth();
  int h = imageBitmap.getHeight();

  if (rotation != 0) {
    Matrix matrix = new Matrix();
    matrix.preRotate(rotation);

    imageBitmap = Bitmap.createBitmap(imageBitmap,0,0,w,h,matrix,false);
  } else {
    imageBitmap = Bitmap.createBitmap(imageBitmap,0,0,w,h);
  }

  imageBitmap = imageBitmap.copy(Bitmap.Config.ARGB_8888,true);

  TessBaseAPI ReadIt = new TessBaseAPI();
  ReadIt.init("/storage/emulated/0/","eng");
  ReadIt.setImage(imageBitmap);

  String Text = ReadIt.getUTF8Text();

  if (Text!=null) textView.setText(Text);

}

I have used this line in my build.gradle dependency:

compile 'com.rmtheis:tess-two:6.0.2'

also, I have copied the"eng.traineddata in the folder named tessdata directly by downloading in the particular stated directory.

Answer

ישו אוהב אותך picture ישו אוהב אותך · Jul 10, 2016

Are you using tess-two?. In your code:

TessBaseAPI ReadIt = new TessBaseAPI();
ReadIt.init("/storage/emulated/0/","eng");

"/storage/emulated/0/" path should be pointing to your data files. You must have a subdirectory named "tessdata". See https://github.com/rmtheis/tess-two/blob/d7a45fd2e08b7ec315cd1e29d1a7e0c72fb24a66/tess-two/src/com/googlecode/tesseract/android/TessBaseAPI.java#L176

Read more at: Could not initialize Tesseract API with language=eng!