简体   繁体   中英

Is there a way to fix permission denied error with pytesseract and python?

I'm trying to create a client/server program in python that sends recognized text from a picture and useful information about it to a client which will then display it on an oled display. But the problem comes on the server side of the program when it tries to use tesseract I get the following error: PermissionError: [WinError 5] Access is denied:'C:\\Users\\BradS\\AppData\\Local\\Temp\\tess_97901m0e'. I am using tesseract 5.0.0 with OpenCV 4.2.0 and python 3.8. I have tried creating a new account uninstalling and reinstalling but nothing seems to work, and yes I am administrator. (Server code below)

import cv2
import pytesseract
from PIL import Image
import numpy as np
from pytesseract import Output
import wolframalpha
import socket
import datetime
import time
import sys

app_id = "********************"
img_counter = 0
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

host = socket.gethostname()
print(host)
port = 60539
s.bind((host, port))
s.listen(5)

cam = cv2.VideoCapture(0)

while True:
  c, addr = s.accept()
  print (f'Got connection from: {addr}')
  break

while True:
  ret, frame = cam.read()
  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

  def remove_noise(gray):
    return cv2.medianBlur(gray, 5)


  def thresholding(gray):
    return cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

  def dilate(image):
    kernel = np.ones((5, 5), np.uint8)
    return cv2.dilate(image, kernel, iterations=1)

  cv2.imshow("test", gray)
  if not ret:
    break
  k = cv2.waitKey(1)

  if k % 256 == 27:
    print("Escape hit, closing...")
    break
  elif k % 256 == 32:

    img_name = "frames_0.png"
    cv2.imwrite(img_name, gray)
    print("frames_0.png written!")
    img = cv2.imread('frames_0.png')
    cv2.destroyAllWindows()

    d = pytesseract.image_to_data(img, output_type = Output.DICT)

    n_boxes = len(d['text'])
    for i in range(n_boxes):
      if int(d['conf'][i]) > 60:
        (x, y, w, h) = (d['left'][i], d['top'][i], d['width'][i], d['height'][i])
        img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)

    cv2.imshow('img', img)
    cv2.waitKey(0)
cam.release()
cv2.destroyAllWindows()

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

image = cv2.imread('frames_0.png')

text = pytesseract.image_to_string(image)
print(text)
cv2.destroyAllWindows

c.send(bytes(text, "utf-8"))
print ("Message sent")

if len(text) <= 0:
  sys.exit()

elif len(text) >= 1:
  query = text
  client = wolframalpha.Client(app_id)
  res = client.query(query)
  print(res.results)
  answer = next(res.results).text
  print (answer)
  c.send(bytes(answer, "utf-8"))
  print("Second message sent")
  c.close()

By referring to https://stackoverflow.com/a/51869555/7961056 , the author stated that even you run from Administrator, it may not solve the issue if the pip is installed inside another userspace. This is because Administrator doesn't own another's userspace directory, thus he can't see (go inside) the inside of the directory that is owned by somebody. Below is an exact solution.

python -m pip install -U pip --user //It solves in Windows. Note: You should provide --user option

Hope this can help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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