使用Tika的Tesseract:结果只包含换行符

时间:2017-03-08 20:51:26

标签: tesseract apache-tika

我尝试使用Apache Tika和Tesseract for Windows解析包含扫描文本的PNG文件。

虽然从命令行运行Tesseract确实能够正确识别文本,但Tika返回的内容仅包含换行符(“\ n”)。

这是我的代码:

ByteArrayInputStream inputstream = new ByteArrayInputStream(document.getFileContent());
byte[] content = document.getFileContent();
Parser parser = new AutoDetectParser();
BodyContentHandler handler = new BodyContentHandler(Integer.MAX_VALUE); //to process long files
Metadata metadata = new Metadata();

ParseContext parseContext = new ParseContext();
TesseractOCRConfig config = new TesseractOCRConfig();
config.setTesseractPath("C:\\Program Files (x86)\\Tesseract-OCR");
config.setTessdataPath("C:\\Program Files (x86)\\Tesseract-OCR\\tessdata");
config.setMaxFileSizeToOcr(Integer.MAX_VALUE);
parseContext.set(TesseractOCRConfig.class, config);
parseContext.set(Parser.class, parser);

parser.parse(inputstream, handler, metadata, parseContext);

String contentString = handler.toString();
System.out.println(contentString);      

我试图调试并发现TesseractOCRParser.doOcr()应该运行一个执行命令的进程:

tesseract C:\Users\admin\AppData\Local\Temp\apache-tika-6655676641285964446.tmp C:\Users\admin\AppData\Local\Temp\apache-tika-2151149415666715558.tmp -l eng -psm 1 txt

但是,看起来该过程不会运行。如果我从另一个会话运行相同的命令,则识别的内容会出现。

1 个答案:

答案 0 :(得分:0)

我发现问题出在这一行:

config.setTessdataPath("C:\\Program Files (x86)\\Tesseract-OCR\\tessdata");

该行应省略,解析器将找到正确的路径。