【问题标题】:Tesseract OCR having trouble detecting numbersTesseract OCR 无法检测数字
【发布时间】:2021-07-28 14:18:59
【问题描述】:

我正在尝试在 python 中使用 tesseract 检测一些数字。您将在下面找到我的起始图像以及我可以理解的内容。这是我用来获取它的代码。

import pytesseract
import cv2
import numpy as np
pytesseract.pytesseract.tesseract_cmd = "C:\\Users\\choll\\AppData\\Local\\Programs\\Tesseract-OCR\\tesseract.exe"

image = cv2.imread(r'64normalwart.png')
lower = np.array([254, 254, 254])
upper = np.array([255, 255, 255])
image = cv2.inRange(image, lower, upper)
image = cv2.bitwise_not(image)
#Uses a language that should work with minecraft text, I have tried with and without, no luck 
text = pytesseract.image_to_string(image, lang='mc')
print(text)
cv2.imwrite("Wartthreshnew.jpg", image)
cv2.imshow("Image", image)
cv2.waitKey(0)

我最终得到了白色背景上的黑色数字,这看起来不错,但 tesseract 仍然无法检测到这些数字。我还注意到这些数字非常参差不齐,但我不知道如何解决这个问题。有没有人建议我如何让 tesseract 能够识别这些数字?

Starting Image

What I end up with

【问题讨论】:

  • 您可以尝试cv2.blur() 来平滑数字的粗糙边缘。它会使图像整体更加模糊,但 tesseract 可能更容易识别数字。
  • 感谢建议,图片可能太小了还是看不到。
  • 尝试像这样添加配置 psm 6 或 7:pytesseract.image_to_string(img, config='--psm 6')
  • 好主意。我找到的解决方案是使用 --psm 8 并将其视为一个单词,并将其限制为数字。 stackoverflow.com/questions/44619077/… 对于以后看到这个的人来说是一个有用的资源。

标签: python opencv image-processing tesseract python-tesseract


【解决方案1】:

您的问题在于页面分段模式。 Tesseract 以不同的方式分割每个图像。当您没有选择合适的 PSM 时,它适用于模式 3,这是自动的,可能不适合您的情况。我刚刚试用了您的图片,它与 PSM 6 完美搭配。

df = pytesseract.image_to_string(np.array(image),lang='eng', config='--psm 6')

这些都是目前可用的 PSM:

  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.

【讨论】:

    【解决方案2】:

    使用 pytesseract.image_to_string(img, config='--psm 8') 或尝试不同的配置来查看图像是否会被识别。有用的链接在这里Pytesseract OCR multiple config options

    【讨论】:

      猜你喜欢
      • 2021-03-11
      • 1970-01-01
      • 2011-07-25
      • 1970-01-01
      • 2014-04-21
      • 1970-01-01
      • 2017-08-27
      • 1970-01-01
      相关资源
      最近更新 更多