在Windows上尝试Tesseract CMD
我在使用Tesseract-OCR和Pytesseract Python包装器时遇到问题。 我想这个问题可能来自Tesseract本身,而不是来自包装。 于是,我就在的Tesseract CMD:在Windows上尝试Tesseract CMD
C:\Users\Thomas\Desktop>tesseract.exe 'blabla.jpg' 'out.txt'
它返回以下行:
Tesseract Open Source OCR Engine v3.05.01 with Leptonica
Error in fopenReadStream: file not found
Error in findFileFormat: image file not found
Error during processing.
我已经做了以下安装的Tesseract:
- 从那里安装:https://github.com/UB-Mannheim/tesseract/wiki
- 将tesseract.exe的路径添加到PATH环境变量
而且通过我在哪里运行我的Python代码的方式,问题:
from PIL import Image
import pytesseract
text = pytesseract.image_to_string(Image.open('blabla.jpg')
print(text)
是:
Traceback (most recent call last):
File "<ipython-input-1-01e77f902509>", line 1, in <module>
runfile('D:/anaconda/projects/OCR/ocr.py', wdir='D:/anaconda/projects/OCR')
File "D:\anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
execfile(filename, namespace)
File "D:\anaconda\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "D:/anaconda/projects/OCR/ocr.py", line 48, in <module>
text = pytesseract.image_to_string(a)
File "D:\anaconda\lib\site-packages\pytesseract\pytesseract.py", line 122, in image_to_string
config=config)
File "D:\anaconda\lib\site-packages\pytesseract\pytesseract.py", line 46, in run_tesseract
proc = subprocess.Popen(command, stderr=subprocess.PIPE)
File "D:\anaconda\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "D:\anaconda\lib\subprocess.py", line 990, in _execute_child
startupinfo)
PermissionError: [WinError 5] Access refused
运行代码以管理员身份不解决问题
非常感谢您的帮助!
首先,验证tesseract
作品或不从Windows command prompt
,使用" "
代替' '
如果图像和/或输出文件名由space
。否则,不需要引用符号。
C:\Users\Thomas\Desktop>tesseract.exe blabla.jpg out.txt
其次,使用完整的文件路径来指定图像文件。比如,
pytesseract.pytesseract.tesseract_cmd = 'C:/path/to/tesseract.exe'
text = pytesseract.image_to_string(Image.open('D:/path/to/blabla.jpg'))
注意,正斜杠/
用于特定的任何文件路径,而不是反斜杠\
,或者你使用双反斜线\\
,例如'D:\\path\\to\\blabla.jpg'
。
希望得到这个帮助。
感谢您的回复! 它解决了我的第一个问题。但是,即使使用完整的图像路径,我的python代码仍然不起作用。我仍然得到“访问被拒绝” –
你会尝试添加'pytesseract.pytesseract.tesseract_cmd ='到特定的'tesseract.exe'文件路径吗?检查示例命令的更新答案。 – thewaywewere
请阅读[“我应该怎么做当有人回答我的问题?”](http://stackoverflow.com/help/someone-answers)。 – thewaywewere