【问题标题】:How to improve Tesseract accuracy如何提高 Tesseract 的准确性
【发布时间】:2020-12-08 06:02:18
【问题描述】:

我正在尝试对一组相似但大小可能不同的图像运行 OCR。由于某种原因,我无法得到可预测的结果。有什么我可以做的以获得更好的结果。

使用或不使用 cv2 预处理的 Tesseract 在某些图像上效果很好,而在某些图像上却失败了,并且没有模式。图像或多或少相似。 Upper image represents processed image

def filter_img(img):
  # Read pil image as cv2
  img = np.array(img)
  img = cv2.resize(img, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC)

  # Converting image to grayscale (important for applying threshold)
  img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

  #Apply dilation and erosion to remove some noise
  kernel = np.ones((1, 1), np.uint8)
  # img = cv2.dilate(img, kernel, iterations=1)
  img = cv2.erode(img, kernel, iterations=1)
  # Apply blur to smooth out the edges
  img = cv2.GaussianBlur(img, (5, 5), 0)
  # img = cv.medianBlur(img,5)
  # Apply threshold to get image with only b&w (binarization)
  img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
  img = Image.fromarray(img)
  img = ImageOps.expand(img,border=2,fill='black')
  visualize.show_labeled_image(img,boxes)
  return img

# Applying Tesseract OCR
def run_tesseract(img):    
    # Tesseract cmd setup
    # pytesseract.pytesseract.tesseract_cmd = "tesseract"
    whitelist = string.ascii_uppercase + string.digits + ".-"
    parameters = '-c load_freq_dawg=0 -c tessedit_char_whitelist="{}"'.format(whitelist)
    psm = 8
    custom_oem_psm_config = "--dpi 300 --oem 3 --psm {psm} {parameters}".format(parameters=parameters, psm=psm)
    try:
      text = pytesseract.image_to_string(img, config=custom_oem_psm_config, timeout=2)
      return text.strip()
    except RuntimeError:
        print ("TIMEOUT")
    return ""

【问题讨论】:

    标签: python computer-vision python-imaging-library tesseract cv2


    【解决方案1】:

    如果您的图像格式高度一致,您可以考虑使用拆分图像。并且在图像ocr之后,对容易出错的区域的第一个字母或数字使用条件判断,比如0和O容易混淆。当然,以上都只有在图像高度一致的情况下才有效。

    enter code here
        import cv2
        import numpy as np
        import pytesseract
        import matplotlib.pyplot as plt
        pytesseract.pytesseract.tesseract_cmd = 'D://Program Files/Tesseract- 
        OCR/tesseract.exe'
    
        img = cv2.imread('vATKQ.png')
    
        img2 = img[100:250, 180:650]  #split to region you want
        plt.imshow(img2)
        text=pytesseract.image_to_string(img2)
        print(text)
    

    【讨论】:

    • 图像有些一致,超大图不是分图吗?你能详细说明你的答案吗?谢谢:)
    猜你喜欢
    • 1970-01-01
    • 2021-06-30
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 2012-03-17
    相关资源
    最近更新 更多