使用pytesser识别简单数字
我正在学习使用 PyTesser
和 Tesseract
进行光学字符识别(OCR)。作为我的第一个小目标,我想写一个工具来识别简单的验证码,这些验证码只包含一些数字。我看了一些教程,写了一个测试程序。
from pytesser.pytesser import *
from PIL import Image, ImageFilter, ImageEnhance
im = Image.open("test.tiff")
im = im.filter(ImageFilter.MedianFilter())
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(2)
im = im.convert('1')
text = image_to_string(im)
print "text={}".format(text)
我用下面的图片测试了我的代码。但结果是 2(T?770
。我还测试了一些其他类似的图片,结果在80%的情况下都是错误的。
我对图像处理不太熟悉,这里有两个问题:
能不能让
PyTesser
只猜测数字呢?我觉得这张图片人类看起来很容易读懂。如果
PyTesser
识别这样只包含数字的图片都这么困难,那有没有其他工具能做得更好呢?
任何提示都非常感谢。
1 个回答
1
我觉得你的代码还不错。它可以识别 207770
。问题出在 pytesser
的安装上。pytesser
中的 Tesseract
版本有点旧了。你需要下载一个最新版本,并替换掉相应的文件。你还需要编辑 pytesser.py
文件,把
tesseract_exe_name = 'tesseract'
改成
import os.path
tesseract_exe_name = os.path.join(os.path.dirname(__file__), 'tesseract')