Search code examples
javatesseracttess4j

Tesseract failed loading language (Tess4j / Java / Netbeans)


I'm currently working on a program which should detect letters and numbers in an image using OpenCV and Tessj4. For that I downloaded and installed Tesseract (Version 5.0.0 alpha) from https://github.com/UB-Mannheim/tesseract/wiki, downloaded the Tess4j API (Version 3.4.8) from http://tess4j.sourceforge.net and added the .jar files (tess4j-3.4.8.jar + all the .jar files inside the lib folder) to my project.

Furthermore I included the tesseract directory (C:/Program Files/Tesseract-OCR) to the systems path and added TESSDATA_PREFIX with the value (C:/Program Files/Tesseract-OCR/tessdata) to my environment variables.

However, when I try to run this 4 simple lines of code, I get the following error:

Tesseract tesseract = new Tesseract();
tesseract.setDatapath("C:/Program Files/Tesseract-OCR/tessdata");
tesseract.setLanguage("eng");
System.out.println(tesseract.doOCR(new File("screen.png")));

Failed loading language 'eng'
Tesseract couldn't load any languages!
Exception in thread "main" java.lang.Error: Invalid memory access
   at com.sun.jna.Native.invokePointer(Native Method)
   at com.sun.jna.Function.invokePointer(Function.java:470)
   at com.sun.jna.Function.invoke(Function.java:404)
   at com.sun.jna.Function.invoke(Function.java:315)
   at com.sun.jna.Library$Handler.invoke(Library.java:212)
   at com.sun.proxy.$Proxy0.TessBaseAPIGetUTF8Text(Unknown Source)
   at net.sourceforge.tess4j.Tesseract.getOCRText(Unknown Source)
   at net.sourceforge.tess4j.Tesseract.doOCR(Unknown Source)
   at net.sourceforge.tess4j.Tesseract.doOCR(Unknown Source)
   at net.sourceforge.tess4j.Tesseract.doOCR(Unknown Source)
   at app.Main.main(Main.java:41)

But when I use the console to run the tesseract program and then reading the content of the file that is created, all works properly.

Process p = Runtime.getRuntime().exec("cmd /c tesseract screen.png text -l eng");
while(p.isAlive()) 
   Thread.sleep(5);
    
BufferedReader reader = new BufferedReader(new FileReader(new File("text.txt")));
StringBuilder stringBuilder = new StringBuilder();
String line;
    
while((line = reader.readLine()) != null)
   stringBuilder.append(line).append("\n");
    
reader.close();
System.out.println(stringBuilder.toString().trim());

Does anyone know how to fix this issue?
Thanks, Ypselon.


Solution

  • Okay I found out that for some reason my language file got corrupted.
    I simply replaced the "eng.traineddata" file inside the "C:\Program Files\Tesseract-OCR\tessdata" directory.

    Now all is working properly.