简体   繁体   English

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

[英]How to detect digits from images using pytesseract?

I am trying to detect the text from the images but fail due to some unknown reasons.我正在尝试从图像中检测文本,但由于某些未知原因而失败。

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 using tesseract on crude/raw image inputs might not give you expected result.在原始/原始图像输入上使用 tesseract 的 OCR 可能不会给您预期的结果。 For the given image, a somewhat better result can be obtained using grayscale conversion followed by thresholding operation对于给定的图像,使用灰度转换和阈值操作可以获得更好的结果

在此处输入图像描述

To perform the conversion and thresholding operation you may use ImageMagick as follows:要执行转换和阈值操作,您可以使用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

A more robust approach to OCR is via training the tesseract engine discussed here一种更强大的 OCR 方法是通过训练此处讨论的 tesseract 引擎

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM