如何在tesseract中保留文档结构

Sar*_*009 17 ocr tesseract

我正在使用tesseract ocr从图像中提取文本.保留文档的结构对我来说非常重要.目前tesseract不保留结构,实际上它改变了文本的顺序.我的输入是下图.

输入

我得到的输出如下:

Someto the left
Someto the left

Some in the middle
Some in the middle

Some with some tab
Some with some tab

Some with some space between them
Some with some space between them

Sometext here
Sometext here

this much
this much
Run Code Online (Sandbox Code Playgroud)

如何获得图像中相同结构的所需输出?

即如下:

                                                 Some text here
                                                 Some text here

Some to the left
Some to the left

                    Some in the middle
                    Some in the middle

        Some with some tab
        Some with some tab

Some with some space between them                       this much
Some with some space between them                       this much
Run Code Online (Sandbox Code Playgroud)

Dav*_*vid 20

较新版本的tesseract(3.04)有一个名为的选项preserve_interword_spaces,可以做你想要的.

请注意,tesseract在单词之间检测到的空格数量在类似行之间可能并不总是相同.因此,与它们之前的一系列空格左对齐的单词(如在您的示例中)可能不会以这种方式输出 - 该preserve_interword_spaces选项不会尝试做任何花哨的事情,它只是保留找到的空间提取.默认情况下,tesseract将空格的运行折叠为一个.

这个选项的详细信息在这里.


Kar*_*l S 5

唯一可靠的方法是启用 hOCR 输出并对其进行解析。它将包含页面上每个单词的位置(以像素为单位),就像在原始图像中一样。

您可以通过tessedit_create_hocr 1在 Tesseract 的配置文件或您使用的任何 API 中指定来实现。

hOCR 是 HTML 的一个子集,而 Tesseract 生成的并不总是有效的 XML,因此您可以使用 HTML 解析器或编写自己的解析器,但您不能可靠地使用 XML 解析器。