【问题标题】:Pytesseract Image_to_string returns Windows Error: Access denied error in PythonPytesseract Image_to_string 返回 Windows 错误:Python 中的访问被拒绝错误
【发布时间】:2017-10-04 14:14:37
【问题描述】:

我尝试使用 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)

当我运行脚本时出现错误

    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 

【问题讨论】:

    标签: python-2.7 tesseract python-tesseract windowserror


    【解决方案1】:

    除了pytesseract.pytesseract.tesseract_cmd 的设置之外,您的代码可以正常工作。 tesseract_cmd 应该设置为安装在您机器上的 tesseract executable file

    这是它的一个示例用法。

    pytesseract.pytesseract.tesseract_cmd = "C:/Program Files/Tesseract 4.0.0/tesseract.exe"
    

    如果您的 Windows PC 中没有正确搜索 PATH 设置,则需要 tesseract_cmd

    希望对您有所帮助。


    更新:

    您需要先将tesseract 二进制文件安装到您的PC 中,然后才能使用pytesseract,它使用subprocess 模块从Python 在Windows shell 中运行tesseract。

    点击Tesseract 4.00 alpha 下载 64 位 Windows 版本并安装。然后设置PATHTESSDATA_PREFIX 分别指向您的tesseract.exe~\tessdata 目录。

    如果你需要任何其他语言trained data file,你可以得到它[here]

    如果在您的 Windows 中没有找到~\tessdata 目录,您可以手动创建它并复制至少一个traineddata 文件到那里,例如eng.traineddata 为英文。

    如果tesseract 正在工作,当您在命令提示符下键入tesseract -v 时,它将返回版本信息,如下所示。

    【讨论】:

    • 我尝试将 tesseract 可执行文件路径设置为 tesseract_cmd,如上所述。我得到一个不同的错误,如下所示。
    • raise TesseractError(status, errors) TesseractError: (1, u'Error opening data file \\Python27\\Lib\\site-packages\\pytesseract\\tessdata/eng.traineddata')
    • 我的 pytesseract 中没有那个 testdata 文件夹。我使用 PIP 安装了 pytesseract。你知道我在哪里可以找到它。
    • @chandrakanthChandra 你可以在上面的更新中查看我的回复。
    • 谢谢。我会检查一下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多