如何使用 pytesseract 从图像中检测数字?

How to detect digits from images using pytesseract?

我正在尝试从图像中检测文本 但由于某些未知原因而失败。

import pytesseract as pt
from PIL import Image
import re
image = Image.open('sample.jpg')
custom_config = r'--oem 3 --psm 7 outbase digits'
number = pt.image_to_string(image, config=custom_config)
print('Number: ', number)
Number:  0 50 100 200 250 # This is the output that I am getting.
Expected --> 0,0,0,0,0,1,0,8

OCR 在 crude/raw 图像输入上使用 tesseract 可能不会给您预期的结果。 对于给定的图像,使用灰度转换后阈值操作

可以获得更好的结果

要执行转换和阈值操作,您可以使用 ImageMagick,如下所示:

$ convert input_image.jpg -colorspace gray grayscale_image.jpg
$ convert grayscale_image.jpg -threshold 45% thresholded_image.jpg
$ convert thresholded_image.jpg -morphology Dilate Rectangle:4,3 dilated_binary.jpg
$ python run_tesseract.py
 00000109

一种更强大的 OCR 方法是通过训练 tesseract 引擎