在学opencv的时候,跟着up主做一个文档识别的小项目,但是在pytesseract这一块代码运行的时候,报错显示PermissionError拒绝访问。
代码如下:
import cv2
import pytesseract
import os
from PIL import Image
preprocess = 'blur'
image = cv2.imread('scan.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
if preprocess == "thresh":
gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
if preprocess == "blur":
gray = cv2.medianBlur(gray, 3)
filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)
text = pytesseract.image_to_string(Image.open(filename))
print(text)
os.remove(filename)
cv2.imshow("Image", image)
cv2.imshow("Output", gray)
cv2.waitKey(0)
运行结果及报错内容如下:
Traceback (most recent call last):
File "D:\pycharm_prj\wenzishibie\test.py", line 20, in <module>
text = pytesseract.image_to_string(Image.open(filename))
File "D:\python\lib\site-packages\pytesseract\pytesseract.py", line 416, in image_to_string
return {
File "D:\python\lib\site-packages\pytesseract\pytesseract.py", line 419, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "D:\python\lib\site-packages\pytesseract\pytesseract.py", line 286, in run_and_get_output
run_tesseract(**kwargs)
File "D:\python\lib\site-packages\pytesseract\pytesseract.py", line 257, in run_tesseract
raise e
File "D:\python\lib\site-packages\pytesseract\pytesseract.py", line 254, in run_tesseract
proc = subprocess.Popen(cmd_args, **subprocess_args())
File "D:\python\lib\subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "D:\python\lib\subprocess.py", line 1420, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
PermissionError: [WinError 5] 拒绝访问。
因为从来没有遇见过这种问题,在网上搜索了几种办法(环境变量已经添加,也已经把pytesseract.py中的路径改为了绝对路径),所在文件夹的权限也是完全控制,可是问题还没有解决,所以来求助,感谢。