2

我尝试在多个大 pdf 文件(约 400-600 页)上使用 Tesseract OCR 执行 OCR。我不一定要从所有页面中提取文本,但我只想要几页(页码已知)。PDF 文件似乎已经对其执行了某种 OCR,但这并不是一项好工作。当我运行我在 Jupyter 中编写的这段代码时:

import pdf2image
from PIL import Image
import pytesseract
import cv2
import numpy as np

pytesseract.pytesseract.tesseract_cmd = r"C:/Program Files/Tesseract-OCR/tesseract.exe"
images = pdf2image.convert_from_path("test2.pdf", first_page=3, last_page=3, poppler_path=r"C:/Program Files/poppler-0.68.0/bin")
images[0].show()

我看到这个输出:[images[0].show()1 的输出

输出应该是这样的: 输入图像

我确实认为在 PDF 上完成的 OCR 在这里造成了一些问题。我不知道如何绕过它,有人可以帮忙吗?

我还通过手动将页面转换为图像(截图工具)来尝试 OCR,并且 OCR 引擎工作。我还尝试在pdf2image.convert_from_path()没有poppler_path选项的情况下使用选项或其他页面。我尝试阅读另一个 PDF 文件,它没有在其上执行OCR,它似乎工作。

4

3 回答 3

1

我遇到了同样的问题,并通过将 poppler 从版本 21.03.0 升级到 21.11.0 来解决它。

于 2022-02-16T09:08:12.920 回答
0

源 OCR 没有任何问题,事实上它比大多数类似的例子要好,确实这里和那里有一个小故障,但这是由于源质量,因此可以预期,我怀疑第二次通过会更糟。

这是突出显示 OCR 文本的来源 在此处输入图像描述

这是 OCR(可作为可搜索文本读取),表示为您建议您希望再次运行的图像,但您所能做的就是变得更糟,永远不会更好,除非您输入任何丢失或格式错误的字符。

在此处输入图像描述

这里是导出到写字板的 TEXT

First Edition, 5,000 Copies, November 1972 

© The Navajivan Trust, 1972 

Principal collaborators: 
Shankar Prasada, ics (retd.) 
Special Secretary, Kashmir Affairs (1958-65) 
Chief Commissioner of Delhi (1948-54) 

B. L. Sharma 
Former Principal Information Officer, Government of India, 
Former Special Officer on Kashmir Affairs in the External Affairs 
Ministry, New Delhi, and author 

Inder Jit 
Director-Editor, India News and Feature Alliance and 
Editor, The States, New Delhi 

Trevor Drieberg 
Political Commentator and Feature Writer 
Former News Editor, The Indian Express, New Delhi 

Uggar Sain 
Former News Editor and Assistant Editor, 
The Hindustan Times, New Delhi 

Printed and Published by Shantilal Harjivan Shah 
Navajivan Press, Ahmedabad-14 

在此处输入图像描述

于 2021-11-21T01:53:16.317 回答
0

我遇到过同样的问题。由于我无法修复它,我决定去另一个图书馆。

在另一个Stack Overflow 帖子和一些谷歌搜索的帮助下,我能够修改 Mohit Chandel 的函数以将 pdf(多页)转换为 jpg

import ghostscript
import locale

def pdf2jpeg(pdf_input_path, jpeg_output_path):
    """
    Source: https://stackoverflow.com/questions/60701262/convert-pdf-to-image-using-python, 
    https://www.kite.com/python/answers/how-to-remove-everything-after-a-character-in-a-string-in-python, 
    https://www.ghostscript.com/doc/current/Use.htm
    """
    args = ["pef2jpeg", # actual value doesn't matter
            "-dNOPAUSE",
            "-sDEVICE=jpeg",
            "-r144",
            "-sOutputFile=" + jpeg_output_path.split(".", 1)[0] + "-%d.jpg",
            pdf_input_path]

    encoding = locale.getpreferredencoding()
    args = [a.encode(encoding) for a in args]

    ghostscript.Ghostscript(*args)
于 2021-11-21T00:45:44.623 回答