【问题标题】:pytesseract can't read dot-punched numberspytesseract 无法读取打点数字
【发布时间】:2021-04-14 10:20:20
【问题描述】:

我正在尝试使用 pytesseract 来读取这样的图像:

我认为这应该很容易,但到目前为止我还不能让它工作。我尝试了不同类型的预处理,但它不起作用:

image = cv2.imread('number.png')
custom_config = r'-c tessedit_char_whitelist=0123456789 --psm 10'

text_i = pytesseract.image_to_string(img, config=custom_config)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
text_g = pytesseract.image_to_string(img, config=custom_config)
thresh = cv2.threshold(image, 50, 255, cv2.THRESH_BINARY)[1]
text_t = pytesseract.image_to_string(img, config=custom_config)

plt.axis('off')
plt.imshow(image)
plt.show()
print("Recognized number: ", text_i)

plt.axis('off')
plt.imshow(gray)
plt.show()
print("Recognized number: ", text_g)

plt.axis('off')
plt.imshow(thresh)
plt.show()
print("Recognized number: ", text_t)

在所有情况下,返回的字符串都是空的。有没有人有想法,该怎么做?

【问题讨论】:

  • 将这些“简单”数字称为有点过于乐观了,显然 Tesseract 并不认为它们简单——简单的数字将是完整的字形而不是模糊的东西。您可以尝试使用扩张/侵蚀来让点连接起来吗?确保以白色背景上的黑色结束。
  • 你可以尝试寻找圆,并用线连接它们的中心。
  • 感谢@barny 的建议,它帮助我找到了解决方案。

标签: python opencv ocr cv2 python-tesseract


【解决方案1】:

使用 erode 是成功的关键。我的脚本现在如下所示:

# Load image
img = cv2.imread('number.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
plt.axis('off')
plt.imshow(img, cmap='gray')
plt.show()

# Set number pool
pool = [31138796, 31130796]

# Set time limit
time_limit = 3

# Set config for pytesseract
custom_config = r'-c tessedit_char_whitelist=0123456789 --psm 10'

while True:
    start = time.time()
    # Iterate over kernel sizes for erode
    for k_erode in range(10, 1, -1):
        # Iterate over thresholds
        for thresh in range(20, 81, 10):
            
            # Check if time limit is exceeded
            if time.time() > start + time_limit:
                print("Time limit exceeded. Make a better picture! ;)")
                break
                
            # Preprocess image
            img_erode = cv2.erode(img, np.ones((k_erode, k_erode), dtype=np.uint8), 1)
            img_out = cv2.threshold(img_erode, thresh, 255, cv2.THRESH_BINARY)[1]
            number = pytesseract.image_to_string(img_out, config=custom_config)
            if number == '\x0c':
                continue
                
            # Check if number is in pool
            if int(number) in pool:
                print("The number is:", str(number))
                print("Is this your number? (y/n)")
                time_curr1 = time.time()
                answer = input()
                time_curr2 = time.time()
                time_limit += time_curr2 - time_curr1
                if answer == "y":
                    plt.axis('off')
                    plt.imshow(img_out, cmap='gray')
                    plt.show()
                    break
                else:
                    pool.remove(int(number))
                    continue
        else:
            continue
        break
    break

【讨论】:

    猜你喜欢
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 2020-04-01
    • 2022-08-21
    • 2020-10-28
    • 2022-09-28
    • 1970-01-01
    相关资源
    最近更新 更多