繁体   English   中英

pytesseract PermissionError:[WinError 5] CreateProcess 中的访问被拒绝(Windows 10)

[英]pytesseract PermissionError: [WinError 5] Access is denied in CreateProcess (Windows 10)

尝试制作一个 OCR 程序,可以从图像中提取文本,将其转换为字节,然后让 pytesseract 将其转换为字符串。 我遇到了一些权限错误。 这是我现在的代码。 我不能 MRE 这段代码,所有这些都很重要。

import cv2
import pytesseract
import numpy as np

# Set the path to the tesseract executable
pytesseract.pytesseract.tesseract_cmd = "C:\\Users\\hypro\\AppData\\Local\\Programs\\Tesseract-OCR"

img = cv2.imread('C:\\Users\hypro\Downloads\image1.jpg')
#Alternatively: can be skipped if you have a Blackwhite image
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
gray, img_bin = cv2.threshold(gray,128,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
gray = cv2.bitwise_not(img_bin)
kernel = np.ones((2, 1), np.uint8)
img = cv2.erode(gray, kernel, iterations=1)
img = cv2.dilate(img, kernel, iterations=1)
# Encode the image as a JPEG
_, encoded_image = cv2.imencode('.jpg', img)
print(encoded_image)
# Convert the encoded image to a string
image_bytes = encoded_image.tobytes()
out_below = pytesseract.image_to_string(img)
print("OUTPUT:", out_below)

我试着检查目录是否正确:是的。 我有权执行 tesseract.exe,并有权查看它。 这是我的完整回溯:

Traceback (most recent call last):
  File "c:\Users\hypro\ocr2.py", line 21, in <module>
    out_below = pytesseract.image_to_string(img)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hypro\AppData\Local\Programs\Python\Python311\Lib\site-packages\pytesseract\pytesseract.py", line 423, in image_to_string
    return {
           ^
  File "C:\Users\hypro\AppData\Local\Programs\Python\Python311\Lib\site-packages\pytesseract\pytesseract.py", line 426, in <lambda>  
    Output.STRING: lambda: run_and_get_output(*args),
                           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hypro\AppData\Local\Programs\Python\Python311\Lib\site-packages\pytesseract\pytesseract.py", line 288, in run_and_get_output
    run_tesseract(**kwargs)
  File "C:\Users\hypro\AppData\Local\Programs\Python\Python311\Lib\site-packages\pytesseract\pytesseract.py", line 255, in run_tesseract
    proc = subprocess.Popen(cmd_args, **subprocess_args())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\hypro\AppData\Local\Programs\Python\Python311\Lib\subprocess.py", line 1024, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\hypro\AppData\Local\Programs\Python\Python311\Lib\subprocess.py", line 1493, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
PermissionError: [WinError 5] Access is denied

你能确认你有正确的 tesseract 可执行文件路径吗?

根据pytesseract docs ,可执行路径应该类似于C:\Program Files (x86)\Tesseract-OCR\tesseract

在你的情况下,你可能需要做

pytesseract.pytesseract.tesseract_cmd = "C:\\Users\\hypro\\AppData\\Local\\Programs\\Tesseract-OCR\\tesseract"

暂无
暂无

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

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