您的位置:首页 > 其它

从图片中提取文本

2015-06-12 16:40 295 查看
ocr https://zh.wikipedia.org/wiki/%E5%85%89%E5%AD%A6%E5%AD%97%E7%AC%A6%E8%AF%86%E5%88%AB
tesseract https://code.google.com/p/tesseract-ocr/
pytesseract https://pypi.python.org/pypi/pytesseract
1.PIL报错

[root@test-22 src]# python test.py
Traceback (most recent call last):
File "test.py", line 9, in <module>
print(pytesseract.image_to_string(Image.open('test.png')))
File "/root/xds/pytesseract-0.1.6/src/pytesseract.py", line 143, in image_to_string
if len(image.split()) == 4:
File "/usr/local/lib/python2.7/site-packages/PIL-1.1.7-py2.7-linux-x86_64.egg/Image.py", line 1497, in split
if self.im.bands == 1:
AttributeError: 'NoneType' object has no attribute 'bands'


1494     def split(self):
1495         "Split image into bands"
1496
1497         self.load()
1498         if self.im.bands == 1:
1499             ims = [self.copy()]
1500         else:
1501             ims = []
1502             for i in range(self.im.bands):
1503                 ims.append(self._new(self.im.getband(i)))
1504         return tuple(ims)

2.PIL报错




IOError: decoder zip not available

重装PIL(需要安装支持jpeg和zip的PIL)
http://jj.isgeek.net/2011/09/install-pil-with-jpeg-support-on-ubuntu-oneiric-64bits/ http://stackoverflow.com/questions/21242107/pip-install-pil-dont-install-into-virtualenv
[root@test-22 src]# python test.py
Traceback (most recent call last):
File "test.py", line 9, in <module>
print(pytesseract.image_to_string(Image.open('test.png')))
File "/root/xds/pytesseract-0.1.6/src/pytesseract.py", line 161, in image_to_string
config=config)
File "/root/xds/pytesseract-0.1.6/src/pytesseract.py", line 94, in run_tesseract
stderr=subprocess.PIPE)
File "/usr/local/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/local/lib/python2.7/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 20] Not a directory

3.pytesseract报错

[root@test-22 src]# python pytesseract.py test.png
Traceback (most recent call last):
File "pytesseract.py", line 203, in <module>
main()
File "pytesseract.py", line 188, in main
print(image_to_string(image))
File "pytesseract.py", line 162, in image_to_string
config=config)
File "pytesseract.py", line 94, in run_tesseract
stderr=subprocess.PIPE)
File "/usr/local/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/local/lib/python2.7/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 20] Not a directory
pdb跟踪发现是tesseract(可执行文件)找不着
https://code.google.com/p/tesseract-ocr/wiki/Compiling 源码安装tesseract-orc,然后还要从这个官网下载各种语言的数据(训练数据)

结果

[root@test-22 src]# cat test.py
#encoding=utf-8

if __name__ == '__main__':
try:
import Image
except ImportError:
from PIL import Image
import pytesseract
print(pytesseract.image_to_string(Image.open('test.png')))
print('-'*30)
print(pytesseract.image_to_string(Image.open('ch.png'), lang='chi_sim'))
[root@test-22 src]# python test.py
This is a lot of 12 point text to test the
ocr code and see if it works on all types
of file format.

The quick brown dog jumped over the
lazy fox. The quick brown dog jumped
over the lazy fox. The quick brown dog
jumped over the lazy fox. The quick
brown dog jumped over the lazy fox.
------------------------------
在图1z.1和12.2 中出现的 priority 参数是级另哑(1eveI)和设施(faciHty)的组合.message
与 printf 所用的格式化字符串类似,还增力口了%m,它将由又寸应当前eTrn。值的出错消息所
取儡弋. 在字肖z息的结尾可以力口换行符,儡旦这不是必、需的。




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: