I tried to read the text from the image using Pytesseract.I am getting Access denied message when I run the below script.
我试图使用Pytesseract从图像中读取文本。当我运行下面的脚本时,我将获得访问拒绝消息。
from PIL import Image
import pytesseract
import cv2
import os
filename=r'C:\Users\ychandra\Documents\teaching-text-structure-3-728.jpg'
pytesseract.pytesseract.tesseract_cmd = r'C:\Python27\Lib\site-packages\pytesseract'
image=cv2.imread(filename)
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
gray=cv2.threshold(gray,0,255,cv2.THRESH_BINARY|cv2.THRESH_OTSU)[1]
gray=cv2.medianBlur(gray,3)
filename='{}.png'.format(os.getpid())
cv2.imwrite(filename,gray)
text=pytesseract.image_to_string(Image.open(filename))
print text
cv2.imshow("image",image)
cv2.imshow("res",gray)
cv2.waitKey(0)
when I run the script I am getting below Error
当我运行这个脚本时,我得到了下面的错误
Traceback (most recent call last):
File "F:\opencv\New_folder_3\text_from_image.py", line 17, in <module>
text=pytesseract.image_to_string(Image.open(filename))
File "C:\Python27\lib\site-packages\pytesseract\pytesseract.py", line 122, in image_to_string
config=config)
File "C:\Python27\lib\site-packages\pytesseract\pytesseract.py", line 46, in run_tesseract
proc = subprocess.Popen(command, stderr=subprocess.PIPE)
File "C:\Python27\lib\subprocess.py", line 390, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
startupinfo)
WindowsError: [Error 5] Access is denied
1 个解决方案
#1
4
Your code works except the setting of pytesseract.pytesseract.tesseract_cmd
. The tesseract_cmd
should set to the tesseract executable file
installed in your machine.
除了设置pytesserac .pytesserac .tesserac .tesseract_cmd之外,您的代码可以正常工作。tesseract_cmd应该设置为安装在您的计算机中的tesseract可执行文件。
Here is a sample usage of it.
这是它的一个示例用法。
pytesseract.pytesseract.tesseract_cmd = "C:/Program Files/Tesseract 4.0.0/tesseract.exe"
The tesseract_cmd
is required if no properly search PATH
setup in your Windows PC.
如果Windows PC中没有正确的搜索路径设置,则需要使用tesseract_cmd。
Hope this help.
希望这个有帮助。
UPDATE:
更新:
You need to have tesseract
binary installed into your PC before using pytesseract
which uses subprocess
module to run tesseract in Windows shell from Python.
在使用pytesseract(使用子进程模块在Python的Windows shell中运行tesseract)之前,您需要将tesseract二进制文件安装到您的PC中。
Click this Tesseract 4.00 alpha to download a 64-bit Windows version and install it. Then setup the PATH
and TESSDATA_PREFIX
pointing to your tesseract.exe
and ~\tessdata
directory respectively.
单击这个Tesseract 4.00 alpha下载一个64位Windows版本并安装它。然后设置指向tesseract的路径和TESSDATA_PREFIX。exe和~\tessdata目录。
If you need any other language trained data file
, you can get it [here].
如果您需要任何其他经过语言训练的数据文件,您可以在[这里]获得它。
In case the ~\tessdata
directory is not found in your Windows, you can create it manually and copy at least one traineddata
file to there, such as eng.traineddata
for English.
如果在Windows中找不到~\tessdata目录,您可以手动创建它,并将至少一个traineddata文件复制到那里,例如eng。traineddata为英语。
If tesseract
is working, it will return the version information when you type tesseract -v
in command prompt as below.
如果tesseract正在工作,当您在命令提示符中键入tesseract -v时,它将返回版本信息,如下所示。
#1
4
Your code works except the setting of pytesseract.pytesseract.tesseract_cmd
. The tesseract_cmd
should set to the tesseract executable file
installed in your machine.
除了设置pytesserac .pytesserac .tesserac .tesseract_cmd之外,您的代码可以正常工作。tesseract_cmd应该设置为安装在您的计算机中的tesseract可执行文件。
Here is a sample usage of it.
这是它的一个示例用法。
pytesseract.pytesseract.tesseract_cmd = "C:/Program Files/Tesseract 4.0.0/tesseract.exe"
The tesseract_cmd
is required if no properly search PATH
setup in your Windows PC.
如果Windows PC中没有正确的搜索路径设置,则需要使用tesseract_cmd。
Hope this help.
希望这个有帮助。
UPDATE:
更新:
You need to have tesseract
binary installed into your PC before using pytesseract
which uses subprocess
module to run tesseract in Windows shell from Python.
在使用pytesseract(使用子进程模块在Python的Windows shell中运行tesseract)之前,您需要将tesseract二进制文件安装到您的PC中。
Click this Tesseract 4.00 alpha to download a 64-bit Windows version and install it. Then setup the PATH
and TESSDATA_PREFIX
pointing to your tesseract.exe
and ~\tessdata
directory respectively.
单击这个Tesseract 4.00 alpha下载一个64位Windows版本并安装它。然后设置指向tesseract的路径和TESSDATA_PREFIX。exe和~\tessdata目录。
If you need any other language trained data file
, you can get it [here].
如果您需要任何其他经过语言训练的数据文件,您可以在[这里]获得它。
In case the ~\tessdata
directory is not found in your Windows, you can create it manually and copy at least one traineddata
file to there, such as eng.traineddata
for English.
如果在Windows中找不到~\tessdata目录,您可以手动创建它,并将至少一个traineddata文件复制到那里,例如eng。traineddata为英语。
If tesseract
is working, it will return the version information when you type tesseract -v
in command prompt as below.
如果tesseract正在工作,当您在命令提示符中键入tesseract -v时,它将返回版本信息,如下所示。