Tesseract加载语言失败(Tess4j / Java / Netbeans)

huangapple go评论75阅读模式
英文:

Tesseract failed loading language (Tess4j / Java / Netbeans)

问题

我目前正在处理一个程序,该程序应使用OpenCV和Tessj4在图像中检测字母和数字。为此,我从https://github.com/UB-Mannheim/tesseract/wiki下载并安装了Tesseract(版本5.0.0 alpha),从http://tess4j.sourceforge.net下载了Tess4j API(版本3.4.8),并将.jar文件(tess4j-3.4.8.jar + lib文件夹中的所有.jar文件)添加到了我的项目中。

此外,我将tesseract目录(C:/Program Files/Tesseract-OCR)添加到系统路径,并将TESSDATA_PREFIX与值(C:/Program Files/Tesseract-OCR/tessdata)添加到我的环境变量中。

然而,当我尝试运行以下这4行简单的代码时,我遇到了以下错误:

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)
   ...

但是,当我使用控制台运行tesseract程序,然后读取创建的文件内容时,一切正常运行。

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());

有人知道如何解决这个问题吗?
谢谢,Ypselon。

英文:

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?
<br>Thanks, Ypselon.

答案1

得分: 2

好的,我发现由于某种原因,我的语言文件损坏了。<br>
我只是在“C:\Program Files\Tesseract-OCR\tessdata”目录中替换了“eng.traineddata”文件。
<br><br>现在一切正常工作。

英文:

Okay I found out that for some reason my language file got corrupted.<br>
I simply replaced the "eng.traineddata" file inside the "C:\Program Files\Tesseract-OCR\tessdata" directory.
<br><br>Now all is working properly.

huangapple
  • 本文由 发表于 2020年8月29日 15:37:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63644614.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定