【问题标题】:Python - Read number in image with PytesseractPython - 使用 Pytesseract 读取图像中的数字
【发布时间】:2020-05-31 02:57:54
【问题描述】:

我正在使用 pyautogui 和 pytesseract 的组合来捕获屏幕上的小区域,然后将数字/文本拉出该区域。我编写的脚本可以完美地读取大多数捕获的图像,但单个数字似乎会导致问题。例如,包含数字的图像的小区域保存到 .png 文件中,数字 11、14 和 18 被完美提取,但数字 7 只是作为空白字符串返回。

问题:什么可能导致这种情况发生?

代码:大幅缩小以使其易于理解:

def get_text(image):
    return pytesseract.image_to_string(image)

answer2 = pyautogui.screenshot('answer2.png',region=(727, 566, 62, 48))
img = Image.open('answer2.png')
answer2 = get_text(img)

此代码重复 4 次,每个图像一次,它适用于 11、14、18,但不适用于 7。

只是为了减慢正在读取的文件,这里是通过 screenshot 命令保存图像后的屏幕截图。

https://gyazo.com/0acbf5be2d970abeb29561113c171fbe

这是我工作的截图:

https://gyazo.com/311913217a1302382b700b07ad3e3439

【问题讨论】:

  • stackoverflow.com/questions/26090597/… 中的 cmets。 tesseract 带有选项 psm 6 可以识别您的单个数字,但我不知道如何将此选项与 pytesseract.image_to_string 一起使用

标签: python


【解决方案1】:

我找到了问题 Why pytesseract does not recognise single digits?,在 cmets 中找到了选项 --psm 6

我检查了tesseract 选项--psm 6,它可以识别您图像上的单个数字。

tesseract --psm 6 number-7.jpg result.txt

我检查了pytesseract.image_to_string() 选项config='--psm 6',它也可以识别您图像上的单个数字。

#!/usr/bin/env python3

from PIL import Image
import pytesseract

img = Image.open('number-7.jpg')

print(pytesseract.image_to_string(img, config='--psm 6'))

【讨论】:

    猜你喜欢
    • 2020-10-28
    • 1970-01-01
    • 2023-02-07
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 2019-04-26
    相关资源
    最近更新 更多