Trying to make an OCR program that can extract the text from an image, convert it to bytes, and then have pytesseract convert it to string. I am encountering some permission errors. Here is my code right now. I cannot MRE this code, all of it is important.
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)
I tried checking if the directory is correct: it is. I have permission to execute tesseract.exe, and permission to view it. Here is my full traceback:
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
Can you confirm that you have the correct path to the tesseract executable?
Acording to pytesseract docs , the executable path should be something like C:\Program Files (x86)\Tesseract-OCR\tesseract
In your case, you might need to do
pytesseract.pytesseract.tesseract_cmd = "C:\\Users\\hypro\\AppData\\Local\\Programs\\Tesseract-OCR\\tesseract"
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.