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

2024-04-19 13:30:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从图像中检测文本 但是由于一些未知的原因失败了

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

Tags: from图像image文本importptconfignumber
1条回答
网友
1楼 · 发布于 2024-04-19 13:30:44

在原始/原始图像输入上使用tesseract进行OCR可能无法获得预期结果。 对于给定的图像,使用灰度转换,然后使用阈值操作,可以获得稍好的结果

enter image description here

要执行转换和阈值化操作,可以按如下方式使用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引擎discussed here

相关问题 更多 >