ocrmypdf_pdf识别

安装

安装说明

https://ocrmypdf.readthedocs.io/en/latest/installation.html#native-windows
提到需要的软件:

Python 3.7 (64-bit) or later

Tesseract 4.0 or later

Ghostscript 9.50 or later

安装 ocrmypdf

复制代码
pip install ocrmypdf

添加语言包

https://ocrmypdf.readthedocs.io/en/latest/languages.html

https://github.com/tesseract-ocr/tessdata/ ,解压里面的扩展名为traineddata的文件,复制到 C:\Program Files\Tesseract-OCR\tessdata {Tesseract-OCR安装目录}\tessdata

使用

命令行

ocrmypdf -l chi_sim --pdf-renderer tesseract --output-type pdf source.pdf ocr.pdf

-l language的意思,chi_sim对应 C:\Program Files\Tesseract-OCR\tessdata\ 路径下的 chi_sim.traineddata 文件,如果是中英文混排的情况,就把-l chi_sim改成

-l chi_sim+eng
更多使用说明 https://ocrmypdf.readthedocs.io/en/latest/cookbook.html

API

使用说明 Using the OCRmyPDF API --- ocrmypdf 16.0.3.dev5+g14365d1 documentation

复制代码
import ocrmypdf

if __name__ == '__main__':  # To ensure correct behavior on Windows and macOS
    ocrmypdf.ocr('input.pdf', 'output.pdf', deskew=True)

中文处理

问题

由于ocrmypdf对中文pdf识别后存在空格,根据以下链接知道

preserve_interword_spaces option not working on 4.00alpha · Issue #781 · tesseract-ocr/tesseract · GitHub

extra space in the result pdf when the input pdf is in Chinese · Issue #715 · ocrmypdf/OCRmyPDF · GitHub

extra spaces in result when ocr chinese · Issue #991 · tesseract-ocr/tesseract · GitHub

主要去设置tesseract的最关键参数是:

|---|-------------------------------|
| 1 | preserve_interword_spaces=1 |

正如这个链接所问的,extra space in the result pdf when the input pdf is in Chinese · Issue #715 · ocrmypdf/OCRmyPDF · GitHub,那么如何在ocrmypdf中设置呢?

解决过程

首先尝试的试试按照使用**--tesseract-config**方法(下面pdf9.2.5章节):

https://media.readthedocs.org/pdf/ocrmypdf/latest/ocrmypdf.pdf

命令:

复制代码
ocrmypdf  -l chi_sim --tesseract-oem 1 --tesseract-pagesegmode 6 --tesseract-config C:\Users\Administrator\Desktop\my.cfg C:\Users\Administrator\Desktop\11.pdf 121.pdf

或者(二者均可)

复制代码
ocrmypdf  -l chi_sim --tesseract-config C:\Users\Administrator\Desktop\my.cfg C:\Users\Administrator\Desktop\11.pdf 121.pdf

其中my.cfg是一个本地文件:里面内容是

复制代码
preserve_interword_spaces 1

经测试:上面的121.pdf还是无法实现pdf复制为不带空格的文字,但是导出txt可以实现不带空格。

这个评论是错的。https://github.com/ocrmypdf/OCRmyPDF/issues/885#issuecomment-1033367021这个网友说了当你选择OEM选择LSTM模型(如下面说明,oem选择1或者2)时候,--tesseract-config不会生效,事实上会生效的。(被这个误导了很久)

下面资料来源于:All Tesseract OCR options -- Muthukrishnan

也可以参考这里:用于光学字符识别的 Tesseract - 知乎

复制代码
OCR options:
  --tessdata-dir PATH   Specify the location of tessdata path.
  --user-words PATH     Specify the location of user words file.
  --user-patterns PATH  Specify the location of user patterns file.
  -l LANG[+LANG]        Specify language(s) used for OCR.
  -c VAR=VALUE          Set value for config variables.
                        Multiple -c arguments are allowed.
  --psm NUM             Specify page segmentation mode.
  --oem NUM             Specify OCR Engine mode.
NOTE: These options must occur before any configfile.
Page segmentation modes:
  0    Orientation and script detection (OSD) only.
  1    Automatic page segmentation with OSD.
  2    Automatic page segmentation, but no OSD, or OCR.
  3    Fully automatic page segmentation, but no OSD. (Default)
  4    Assume a single column of text of variable sizes.
  5    Assume a single uniform block of vertically aligned text.
  6    Assume a single uniform block of text.
  7    Treat the image as a single text line.
  8    Treat the image as a single word.
  9    Treat the image as a single word in a circle.
 10    Treat the image as a single character.
 11    Sparse text. Find as much text as possible in no particular order.
 12    Sparse text with OSD.
 13    Raw line. Treat the image as a single text line,
       bypassing hacks that are Tesseract-specific.
OCR Engine modes: (see https://github.com/tesseract-ocr/tesseract/wiki#linux)
  0    Legacy engine only.
  1    Neural nets LSTM engine only.
  2    Legacy + LSTM engines.
  3    Default, based on what is available.

另外,我还测试了导出txt的结果,当你使用--tesseract-config C:\Users\Administrator\Desktop\my.cfg 之后可以保证导出的txt是不带空格的,pdf还是复制空格

再次使用命令:(其中--sidecar 121.txt表示输出txt)

复制代码
ocrmypdf  --force-ocr --tesseract-config C:\Users\Administrator\Desktop\my.cfg -l chi_sim --sidecar 121.txt  C:\Users\Administrator\Desktop\11.pdf 121.pdf

**效果:**输出121.txt没有空格,121.pdf复制还有空格。

经过测试:跟这个里面extra space in the result pdf when the input pdf is in Chinese · Issue #715 · ocrmypdf/OCRmyPDF · GitHub说的一样(但是他是ocrmypdf的老版本)即输出txt才会出现没有空格,pdf还是复制有空格。

Ocrmypdf的作者@jbarlow83一直说的是阅读器问题,但是事实上不是阅读器问题。

也就是说我们只是部分解决(曲线救国)了pdf出现文本图层含有多余空格的问题。

目前我测试了其他的大量方法均无效。作者也从未给出有效解决方案,目前日韩网友(Detection of extra spaces while running own trained tesseract for Korean OCR · Issue #1009 · tesseract-ocr/tesseract · GitHub)也存在这个问题。

以上总结:

(1)使用--tesseract-config设置可以实现:导出txt不带空格,但是pdf复制带空格。

(2)不使用--tesseract-config设置效果:导出txt带空格,pdf复制也是带空格。

最终思路

使用config设置,输出txt以复制文字。如果有大神可以继续给我提示,谢谢!

最佳思路:

ocrmypdf --force-ocr --tesseract-config C:\Users\Administrator\Desktop\my.cfg -l chi_sim --sidecar out.txt C:\Users\Administrator\Desktop\input.pdf output.pdf

彩蛋:测试数据与做好的my.cfg

测试数据:11.pdf官方版下载丨最新版下载丨绿色版下载丨APP下载-123云盘 提取码:newt

做好的my.cfg:https://www.123pan.com/s/9Rn9-qhQpH.html

致谢

上面链接分享者。还有这个:https://github.com/dahuoyzs/javapdf/blob/master/OCRmyPDF%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B.md

后续

作者给我的最新回答是,"请理解这个问题是由于Tesseract产生的PDF,一些PDF阅读器不能正确解释,目前没有人有解决办法。"

我测试了tesseract v5.3.1.20230401出现的情况:

tesseract input.png out -l chi_sim --oem 1 --psm 6 -c preserve_interword_spaces=1 pdf

我得到了与ocrmypdf相同的结果:输出txt没有空格,但从pdf复制的文本仍然有空格。

因此,这个问题发生在Tesseract而不是ocrmypdf。这个结论需要让更多的用户知道。

最终篇

目前看来,要想ocrmypdf使得输出pdf不出现复制文字的空格,唯一且不算特别好的解决方案就是使用oem 0(采取非LSTM模型,但是识别效果不好)

复制代码
ocrmypdf  -l chi_sim --tesseract-oem 0 input.pdf output.pdf 

这种方法直接复制pdf的文字,不会出现空格,但是复制的文字有的识别不正确。

这位网友的测试证实了我的说法:Chinese recognition was incorrectly segmented by spaces · Issue #2814 · tesseract-ocr/tesseract · GitHub

相关推荐
qq_393828221 天前
PDF超强无损压缩
pdf·软件需求
haiyangyiba2 天前
Qoppa Software提供的15款PDF产品组件科学学习
pdf·qoppa·jofficeconvert·jpdfprocess·jpdfeditor
williamdsy2 天前
【Vue PDF】Vue PDF 组件初始不加载 pdfUrl 问题分析与修复
前端·javascript·vue.js·pdf
来自外太空的鱼-张小张2 天前
java将pdf文件转换为图片工具类
java·python·pdf
zjsx1382 天前
2025国家卫健委减肥食谱PDF完整版(免费下载打印)
pdf·减肥·国家版减肥
SEO-狼术2 天前
Create and Manage Content Crack
pdf
拓端研究室3 天前
专题:2025中国游戏科技发展白皮书报告汇总解读|附130+份报告PDF汇总下载
科技·游戏·pdf
沉到海底去吧Go3 天前
【工具教程】批量PDF识别提取区域的内容重命名,将PDF指定区域位置的内容提取出来改名的具体操作步骤
pdf·pdf识别改名·pdf区域内容改名·批量pdf识别区域内容重命名
开开心心就好3 天前
高效账号信息管理工具,可安全随机生成密码
javascript·安全·docker·智能手机·pdf·word·excel
CodeCraft Studio3 天前
PDF处理控件Aspose.PDF教程: 在 Java 中编辑 PDF 页面大小
java·pdf·aspose·aspose.pdf