我最近看到有人穿着一件背面有一些 Perl 代码的 T 恤。我拍了一张照片并裁剪出代码:
接下来,我尝试通过 OCR 从图像中提取代码,因此我安装了Tesseract OCR及其 Python 绑定pytesser。
Pytesser 仅适用于 TIFF 图像,因此我在 Gimp 中转换了图像并输入了以下代码(Ubuntu 9.10):
>>> from pytesser import *
>>> image = Image.open('code.tif')
>>> print image_to_string(image)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pytesser.py", line 30, in image_to_string
util.image_to_scratch(im, scratch_image_name)
File "util.py", line 7, in image_to_scratch
im.save(scratch_image_name, dpi=(200,200))
File "/usr/lib/python2.6/dist-packages/PIL/Image.py", line 1406, in save
save_handler(self, fp, filename)
File "/usr/lib/python2.6/dist-packages/PIL/BmpImagePlugin.py", line 197, in _save
raise IOError("cannot write mode %s as BMP" % im.mode)
IOError: cannot write mode RGBA as BMP
>>> r,g,b,a = image.split()
>>> img = Image.merge("RGB", (r,g,b))
>>> print image_to_string(img)
Tesseract Open Source OCR Engine
éi _ l_` _ t
’ ‘" fY`
{ W IKQW
· __·_ ‘ ·-»·
:W Z
·· I A n 1
;f
` `
`T .' V _ ‘
I {Z.; » ;,. , ; y i- 4 : %:,,
`· » V; ` ?
‘,—·.
H***li¥v·•·}I§¢ ` _ »¢is5#__·¤G$++}§;“»‘7·
71 ’ Q { NH IQ
ytéggygi { ;g¤qg;gm·;,g(g,,3) {3;;+-
§ {Jf**$d$ }‘$p•¢L#d¤ Sc}
» i ` i A1:
这显然是来自 OCR 引擎的胡言乱语。所以,我的问题是:
- 我该怎么做才能从 Tesseract 中获得更好的 OCR 结果?
- 或者,其他人是否有更好的运气以另一种方式从上面的图像中提取代码?