如何使用 pytesseract 用简单的数字读取该图像中的文本?

问题描述 投票:0回答:1

image_processed
变量是所附图片。

    custom_config = r'--oem 3 --psm 7 -c tessedit_char_whitelist= 0123456789/'
    result = pytesseract.image_to_string(image_processed, lang='eng', config=custom_config)

输出:

43659 [44 38

该应用程序截取屏幕截图,并用数字裁剪指定的坐标,然后应用反向阈值在白板上获取黑色数字。我正在尝试使用 pytesseract 读取裁剪后的数字,但它不会输出可靠的文本输出。

如何使用 pytesseract 从该图像中读取带有简单数字的文本?

python ocr python-tesseract
1个回答
0
投票

按照文档中的建议,通过在图像上方和下方添加 10 个像素的空白并沿轮廓设置黑色边框来解决该图片中识别数字的主要问题。

使用

Pillow
库的解决方案。

import pytesseract
from PIL import Image, ImageOps

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

image_processed = Image.open(r"jiyYb.jpg")

image_processed = ImageOps.expand(image_processed, border=10, fill='#ffffff')
image_processed = ImageOps.expand(image_processed, border=10, fill='#000000')

custom_config = r'--psm 7 -c tessedit_char_whitelist=" /0123456789"'
result = pytesseract.image_to_string(image_processed, config=custom_config)
print(result)

-------------

5367 /5438

使用

opencv-python
的解决方案。

import cv2
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"

image_processed = cv2.imread('jiyYb.jpg')

image_processed = cv2.copyMakeBorder(src=image_processed, top=10, bottom=10, left=0, right=0,
                                     borderType=cv2.BORDER_CONSTANT, value=[255, 255, 255])
image_processed = cv2.copyMakeBorder(src=image_processed, top=1, bottom=1, left=1, right=1,
                                     borderType=cv2.BORDER_CONSTANT)

custom_config = r'--psm 7 -c tessedit_char_whitelist=" /0123456789"'
data = pytesseract.image_to_string(image_processed, config=custom_config)
print(data)

cv2.imshow('image_processed', image_processed)
cv2.waitKey(0)
© www.soinside.com 2019 - 2024. All rights reserved.