pytesseract OCR python错误 - 示例代码

时间:2016-02-15 22:40:26

标签: python windows python-3.x ocr

您好我尝试了解如何使用pytesseract(https://pypi.python.org/pypi/pytesseract),因此我运行示例代码:

try:
    import Image
except ImportError:
    from PIL import Image
import pytesseract
im=Image.open('test.png')
print(pytesseract.image_to_string(im))

但我有错误:

Traceback (most recent call last):
  File "C:/Users/K/PycharmProjects/untitled/zad1.py", line 7, in <module>
    print(pytesseract.image_to_string(im))
  File "C:\Users\K\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pytesseract-0.1.6-py3.5.egg\pytesseract\pytesseract.py", line 161, in image_to_string
  File "C:\Users\K\AppData\Local\Programs\Python\Python35-32\lib\site-packages\pytesseract-0.1.6-py3.5.egg\pytesseract\pytesseract.py", line 94, in run_tesseract
  File "C:\Users\K\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 950, in __init__
    restore_signals, start_new_session)
  File "C:\Users\K\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 1220, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

我的python版本是3.5,pytesseract版本是0.1.6。 我在Windows 7上工作。

2 个答案:

答案 0 :(得分:2)

我的Ubuntu也遇到了这个问题。感谢@Newbie,我在安装tesseract后解决了这个问题。

sudo apt-get install tesseract-ocr
sudo apt-get install tesseract-ocr-chi-sim

似乎

sudo pip install pytesseract

只安装python界面而不是自行测试

以下是我的错误信息:

OSError                                   Traceback (most recent call last)
<ipython-input-4-aaddc46c52ee> in <module>()
----> 1 pytesseract.image_to_string(Image.open('./static/files/captcha'))

/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.pyc in image_to_string(image, lang, boxes, config)
    159                                              lang=lang,
    160                                              boxes=boxes,
--> 161                                              config=config)
    162         if status:
    163             errors = get_errors(error_string)

/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.pyc in run_tesseract(input_filename, output_filename_base, lang, boxes, config)
     92 
     93     proc = subprocess.Popen(command,
---> 94             stderr=subprocess.PIPE)
     95     return (proc.wait(), proc.stderr.read())
     96 

/usr/lib/python2.7/subprocess.pyc in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags)
    708                                 p2cread, p2cwrite,
    709                                 c2pread, c2pwrite,
--> 710                                 errread, errwrite)
    711         except Exception:
    712             # Preserve original exception in case os.close raises.

/usr/lib/python2.7/subprocess.pyc in _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, to_close, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
   1333                         raise
   1334                 child_exception = pickle.loads(data)
-> 1335                 raise child_exception
   1336 
   1337 

OSError: [Errno 2] No such file or directory

答案 1 :(得分:0)

我解决了我的问题。解决方案非常简单: 安装tesseract-ocr。