Apache Tika提取扫描的PDF文件。

9
我遇到了一些使用Apache TIKA(版本1.10)的麻烦。我有一些PDF文件,它们只是扫描出来的纸张,这意味着每一页都是一个图像。我的目标是无论如何提取PDF文件的文本内容。
我的tesseract设置正确,并且提取JPG和PNG文件非常顺利。我正在使用以下代码(不要注意缺少的异常处理):
public String extractText(InputStream stream) {
    AutoDetectParser parser = new AutoDetectParser();
    BodyContentHandler handler = new BodyContentHandler(Integer.MAX_VALUE);
    Metadata metadata = new Metadata();
    ParseContext context = new ParseContext();
    parser.parse(stream, handler, metadata, context);
    String text = handler.toString();
    return text;
}

我搜索了很多,但是没有找到适合我的解决方案。我已经尝试了PDFParserConfig类的setExtractInlineImages方法,但这并没有改变任何事情。使用自定义ParsingEmbeddedDocumentExtractor提取嵌入式文档可以提取doc文件的嵌入资源,但对于我的PDF文件则不行。

如果您中的任何人能够提供一些帮助,那将是太棒了:)


你是否在上下文中附加了一个设置了该选项的PDFParserConfig - Gagravarr
我在官方的Apache TIKA-JIRA中创建了一个工单。所有对更新感兴趣的人都可以查看这里:https://issues.apache.org/jira/browse/TIKA-1729。 - LorisBachert
你是否能在没有安装Tesseract的情况下正常运行呢? - Bilal BBB
不,它需要Tesseract。 - LorisBachert
最好在这里编写你的解决方案,这样每个人都可以使用它。 - Bilal BBB
显示剩余6条评论
1个回答

14

Tim Allison提供了解决方案:

Parser parser = new AutoDetectParser();
BodyContentHandler handler = new BodyContentHandler(Integer.MAX_VALUE);

TesseractOCRConfig config = new TesseractOCRConfig();
PDFParserConfig pdfConfig = new PDFParserConfig();
pdfConfig.setExtractInlineImages(true);

ParseContext parseContext = new ParseContext();
parseContext.set(TesseractOCRConfig.class, config);
parseContext.set(PDFParserConfig.class, pdfConfig);
parseContext.set(Parser.class, parser); //need to add this to make sure recursive parsing happens!

parser.parse(stream, handler, new Metadata(), parseContext);

这对我有用:)

编辑: 以下是完整解决方案:

import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.parser.ocr.TesseractOCRConfig;
import org.apache.tika.parser.pdf.PDFParserConfig;
import org.apache.tika.sax.BodyContentHandler;
import org.xml.sax.SAXException;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * @since 8/26/16
 */
public class Sample {
    public static void main(String[] args)
            throws IOException, TikaException, SAXException {
        Parser parser = new AutoDetectParser();
        BodyContentHandler handler = new BodyContentHandler(Integer.MAX_VALUE);

        TesseractOCRConfig config = new TesseractOCRConfig();
        PDFParserConfig pdfConfig = new PDFParserConfig();
        pdfConfig.setExtractInlineImages(true);

        ParseContext parseContext = new ParseContext();
        parseContext.set(TesseractOCRConfig.class, config);
        parseContext.set(PDFParserConfig.class, pdfConfig);
        //need to add this to make sure recursive parsing happens!
        parseContext.set(Parser.class, parser);

        FileInputStream stream = new FileInputStream("samplepdf.pdf");
        Metadata metadata = new Metadata();
        parser.parse(stream, handler, metadata, parseContext);
        System.out.println(metadata);
        String content = handler.toString();
        System.out.println("===============");
        System.out.println(content);
        System.out.println("Done");
    }
}

Maven 依赖:

<dependencies>
    <dependency>
      <groupId>org.apache.tika</groupId>
      <artifactId>tika-parsers</artifactId>
      <version>1.13</version>
    </dependency>
    <dependency>
      <groupId>com.levigo.jbig2</groupId>
      <artifactId>levigo-jbig2-imageio</artifactId>
      <version>1.6.5</version>
    </dependency>
  </dependencies>

1
我已经尝试了解决方案并遵循了Apache Tika-Jira,但它没有起作用。我没有收到任何错误,但输出为空。 - Rana
1
我的问题已经解决。请参考:http://stackoverflow.com/questions/39762841/unable-to-extract-scanned-pdf-using-tesseractocrconfig-apache-tika/39792337#39792337 - Rana
Thamme,非常感谢您。请更新以包括以下依赖项(感谢Rana上面的链接)并警告levigo和jai的许可证影响。<dependency> <groupId>com.github.jai-imageio</groupId> <artifactId>jai-imageio-core</artifactId> <version>1.3.1</version> </dependency> - Tim Allison
嗨,我使用了上面的代码,发现无论是否包含tesseract,提取结果都没有区别。你能告诉我为什么要使用tesseract吗?先谢谢了。 - charmi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接