简体   繁体   English

有没有办法用 pytesseract 和 python 修复权限被拒绝的错误?

[英]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.我正在尝试在 python 中创建一个客户端/服务器程序,它将图片中识别的文本和有关它的有用信息发送到客户端,然后客户端将其显示在 oled 显示器上。 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'.但是问题出在程序的服务器端,当它尝试使用 tesseract 时,我收到以下错误: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.我正在将 tesseract 5.0.0 与 OpenCV 4.2.0 和 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.参考https://stackoverflow.com/a/51869555/7961056 ,作者说即使你从管理员运行,如果pip安装在另一个用户空间中,它可能无法解决问题。 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.希望这可以帮到你。

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

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