【问题标题】:Tesseract returning gibberish when performing OCR on image对图像执行 OCR 时,Tesseract 返回乱码
【发布时间】:2020-05-13 09:54:46
【问题描述】:

我正在尝试使用 Tesseract 读取图像,但它返回乱码。我知道我需要做一些预处理,但我在网上找到的内容似乎不适用于我的图像。我尝试this回答将图片从黑色背景/白色字母变为白色背景/黑色字母,但没有成功。

这是图片。

还有我的简单代码:

from PIL import Image
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r'D:\Tesseract-OCR\tesseract'

img = Image.open("2020-01-25_17-57-49_UTC.jpg")
print(pytesseract.image_to_string(img))

【问题讨论】:

    标签: python python-3.x ocr tesseract python-tesseract


    【解决方案1】:

    在 SO 上找到的代码

    from PIL import Image
    import PIL.ImageOps
    import pytesseract
    
    img = Image.open("8pjs0.jpg")
    inverted_image = PIL.ImageOps.invert(img)
    print(pytesseract.image_to_string(inverted_image))
    

    给我

    Dolar Hoy en Cucuta
    
    25-Enero-20
    01:00PM
    
    78.048
    VENTA
    

    我认为您需要一些用于重音字符的语言包。

    【讨论】:

    【解决方案2】:

    一个简单的 Otsu 阈值来获得二值图像,然后反转来获得黑色字母和白色背景似乎工作。我们使用--psm 3 告诉 Pytesseract 执行自动页面分割。查看Pytesseract OCR multiple config options 了解更多配置选项。这是预处理后的图像

    Pytesseract OCR 的结果

    Dolar Hoy en Cucuta
    
    25-Enero-20
    01:00PM
    
    78.048
    VENTA
    

    代码

    import cv2
    import numpy as np
    import pytesseract
    
    pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
    
    # Load image, grayscale, threshold, invert
    image = cv2.imread('1.jpg')
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
    result = 255 - thresh
    
    # Perfrom OCR with Pytesseract
    data = pytesseract.image_to_string(result, config='--psm 3')
    print(data)
    
    cv2.imshow('result', result)
    cv2.waitKey()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 2015-04-04
      • 2015-05-10
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      • 2013-08-31
      相关资源
      最近更新 更多