我可以采取哪些图像预处理步骤来使该图像OCR可读?

问题描述 投票:0回答:1

我正在使用 tesseract ocr 从中提取文本。虽然它能够从其他类似图像中提取一些信息,尽管存在轻微的拼写错误,但它无法从该图像中检测到单个单词。它已被二值化并应用了高斯模糊。我还可以采取哪些其他步骤来使其对 OCR 更加友好?预先感谢:)

image-processing ocr tesseract python-tesseract
1个回答
0
投票

TLDR;尝试

--psm 4
- 如果这不起作用,请切换到 PaddleOCR

当使用 Tesseract 对非扫描文档(具有大块格式一致的文本)进行 OCR 时,您需要寻找最适合文本格式的页面分割模式。这些模式针对不同类型的输入进行了优化,因此 Tesseract 将仅仅因为使用了正确/错误的 PSM 而产生显着优越/较差的结果。

例如,这样的功能可以让您尝试不同的 PSM,并测试一些基本图像预处理的效果。

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

def testTesseract(someFilename, psmValue=3, processABit=True):
    if processABit:
        img = np.array(pillowImageLoader.open(someFilename))
        norm_img = np.zeros((img.shape[0], img.shape[1]))
        img = cv2.normalize(img, norm_img, 0, 255, cv2.NORM_MINMAX)
        img = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY)[1]
        img = cv2.GaussianBlur(img, (1, 1), 0)
    try:
        someText = pytesseract.image_to_string(someFilename, config='--psm {}'.format(psmValue))
        return someText
    except:
        return None

然后运行每个 psm 并检查输出:

for i in range(1,14):
    someText = testTesseract(imageFilename, i, processABit=True)
    print("PSM: {}".format(i))
    print(someText
    print("="*40)

如果您无法使用 Tesseract 获得任何可接受的结果,那么像 PaddleOCR 这样的工具通常能够获得结果:

from paddleocr import PaddleOCR
ocr = PaddleOCR(use_angle_cls=True, lang='en',use_gpu=False)
result = ocr.ocr(imageFilename, cls=True)
print("Paddle Found: {} text segments".format(len(result[0])))
print()
for t in result[0]:
        print("{}  (Conf: {:.1%})".format(t[1][0], t[1][1]))
© www.soinside.com 2019 - 2024. All rights reserved.