2016-09-14 42 views
0

我试图捕捉当前画面的一部分检测屏幕上的一些数字,但是当在运行的代码得到这个错误:蟒蛇FileNotFoundError使用pytesseract

Traceback (most recent call last): 
    File "C:/Users/Administrator/PycharmProjects/bot/detect_num.py", line 12, in <module> 
    print(pytesseract.image_to_string(Image.open('test.jpg'))) 
    File "C:\Python35\lib\site-packages\pytesseract\pytesseract.py", line 161, in image_to_string 
    config=config) 
    File "C:\Python35\lib\site-packages\pytesseract\pytesseract.py", line 94, in run_tesseract 
    stderr=subprocess.PIPE) 
    File "C:\Python35\lib\subprocess.py", line 947, in __init__ 
    restore_signals, start_new_session) 
    File "C:\Python35\lib\subprocess.py", line 1224, in _execute_child 
    startupinfo) 
FileNotFoundError: [WinError 2] The system cannot find the file specified 

源代码:

import pyscreenshot as ImageGrab 
from PIL import Image 
import subprocess 
from pytesseract import * 

if __name__=="__main__": 
    im = ImageGrab.grab(bbox=(1349, 34, 1357, 45)) 
    im = im.convert('1') 
    im.save('test.jpg', 'JPEG') 
    im.show() 
    print(pytesseract.image_to_string(Image.open('test.jpg'))) 

请有人告诉我为什么,以及如何解决它?

+0

可能重复:\ [错误2 \]系统找不到指定的文件“不解析”(http://stackoverflow.com/questions/18757127/windowserror-error-2-the-system-cannot-find-the-file-specified-is-not-resol) –

+0

我试图删除线时调用子进程(另一个答案建议添加它来修复),并在你的建议中添加一些该anwser的行,但它仍然错误:(。@PavneetSingh – AlphaWolf

回答

0

这里的问题是您没有安装必要的依赖项。当你读pyteseeract's documentation你看到下面的文字:

  • 安装谷歌从http://code.google.com/p/tesseract-ocr/的Tesseract-OCR。 您必须能够将tesseract命令作为“tesseract”调用。如果这个 不是这种情况,例如因为tesseract不在你的PATH中,你将不得不改变“tesseract.py”顶部的“tesseract_cmd”变量。

我认为你还没有执行那一步,所以没有tesseract命令来实际执行所需的OCR工作。

0

当您使用pytesseract时,首先您必须确保您已在您的系统中安装了Tesseract-OCR。然后,你必须插入在代码中的tesseract的路径,如下

from PIL import Image 
import pytesseract 

pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract 
OCR/tesseract' 

您也可以下载的Tesseract-OCR形式的[“WindowsError https://github.com/UB-Mannheim/tesseract/wiki