简体   繁体   English

Pytesseract 因 PermissionError 失败:[WinError 5] 由于不可删除的文件,访问被拒绝

[英]Pytesseract is failing with PermissionError: [WinError 5] Access is denied due to undeletable file

I Installed the 64bit version from https://github.com/UB-Mannheim/tesseract/wiki then pip install pytesseract cv2 didn't cause any issues我从https://github.com/UB-Mannheim/tesseract/wiki安装了 64 位版本,然后pip install pytesseract cv2 没有引起任何问题

My code:我的代码:

import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd=r"C:\Program Files\Tesseract-OCR\tesseract.exe"

img = cv2.imread("test.png")
text = pytesseract.image_to_string(img)
print(text)

my code fails at line 6 with the我的代码在第 6 行失败

PermissionError: [WinError 5] Access is denied: 'C:\\Users\\nicol\\AppData\\Local\\Temp\\tess_puh3wgus'

it seems it fails at the function似乎它在功能上失败了

def cleanup(temp_name):
    """ Tries to remove temp files by filename wildcard path. """
    for filename in iglob(temp_name + '*' if temp_name else temp_name):
        try:
            remove(filename)
        except OSError as e:
            if e.errno != ENOENT:
                raise e

line 131 in cleanup function at remove(filename). remove(filename) 处的清理函数中的第 131 行。

It seems like it tries to remove the temp files but fails since the system denies access so-它似乎试图删除临时文件,但由于系统拒绝访问而失败,所以 -

I tried running the Spyder IDE with admin rights, I tried giving C:\\Program Files\\Tesseract-OCR\\tesseract.exe full permissions.我尝试以管理员权限运行 Spyder IDE,我尝试授予 C:\\Program Files\\Tesseract-OCR\\tesseract.exe 完全权限。 I ran a full anaconda update.我运行了完整的 anaconda 更新。 I also tried changing the paths of TEMP and TMP system variables to places were admin rights(C:\\Temp) are not required.我还尝试将 TEMP 和 TMP 系统变量的路径更改为不需要管理员权限(C:\\Temp)的地方。

PermissionError: [WinError 5] Access is denied: 'C:\\Temp\\tess_hxw9iwvr.PNG'

So far I have identified the problem the pytesseract.py script creates a duplicate of the provided image, a text file with the desired output and a 0B empty file all synonymous in the format tess_[a-z0-9].extension.到目前为止,我已经确定了 pytesseract.py 脚本创建所提供图像的副本、具有所需输出的文本文件和格式为 tess_[a-z0-9].extension 的 0B 空文件的问题。 The problem is caused by the empty file when I try to delete it requires elevation (which I have since I am my system admin) I press the ok it briefly goes to 0% deletion and it says after access is denied because admin rights are required try again or cancel.问题是由空文件引起的,当我尝试删除它时需要提升(因为我是系统管理员,所以我有)我按确定它会短暂地进入 0% 删除,它说访问被拒绝,因为需要管理员权限重试或取消。

prompt try again提示再试一次

I guess The cleanup() function tries to remove() the file and gets access denied.我猜 cleanup() 函数尝试 remove() 文件并拒绝访问。 Disabling the cleanup() function of the pytesseract.py covers up the problem but I managed to get the expected output with the collateral of 3 undeleted "temporary" files.禁用 pytesseract.py 的 cleanup() 函数掩盖了问题,但我设法通过 3 个未删除的“临时”文件的抵押品获得了预期的输出。 Running the script again seems to replace the 0B file with a new one only if I tried to delete the file.只有当我尝试删除文件时,再次运行脚本似乎才会用新的文件替换 0B 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM