I tried to read the text from the image using Pytesseract.I am getting Access denied message when I run the below script.
from PIL import Image
import pytesseract
import cv2
import os
filename=r'C:\Users\ychandra\Documents\teaching-text-structure-3-728.jpg'
pytesseract.pytesseract.tesseract_cmd = r'C:\Python27\Lib\site-packages\pytesseract'
image=cv2.imread(filename)
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
gray=cv2.threshold(gray,0,255,cv2.THRESH_BINARY|cv2.THRESH_OTSU)[1]
gray=cv2.medianBlur(gray,3)
filename='{}.png'.format(os.getpid())
cv2.imwrite(filename,gray)
text=pytesseract.image_to_string(Image.open(filename))
print text
cv2.imshow("image",image)
cv2.imshow("res",gray)
cv2.waitKey(0)
when I run the script I am getting below Error
Traceback (most recent call last):
File "F:\opencv\New_folder_3\text_from_image.py", line 17, in <module>
text=pytesseract.image_to_string(Image.open(filename))
File "C:\Python27\lib\site-packages\pytesseract\pytesseract.py", line 122, in image_to_string
config=config)
File "C:\Python27\lib\site-packages\pytesseract\pytesseract.py", line 46, in run_tesseract
proc = subprocess.Popen(command, stderr=subprocess.PIPE)
File "C:\Python27\lib\subprocess.py", line 390, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 640, in _execute_child
startupinfo)
WindowsError: [Error 5] Access is denied
Your code works except the setting of
pytesseract.pytesseract.tesseract_cmd. Thetesseract_cmdshould set to thetesseract executable fileinstalled in your machine.Here is a sample usage of it.
The
tesseract_cmdis required if no properly searchPATHsetup in your Windows PC.Hope this help.
UPDATE:
You need to have
tesseractbinary installed into your PC before usingpytesseractwhich usessubprocessmodule to run tesseract in Windows shell from Python.Click this Tesseract 4.00 alpha to download a 64-bit Windows version and install it. Then setup the
PATHandTESSDATA_PREFIXpointing to yourtesseract.exeand~\tessdatadirectory respectively.If you need any other language
trained data file, you can get it [here].In case the
~\tessdatadirectory is not found in your Windows, you can create it manually and copy at least onetraineddatafile to there, such aseng.traineddatafor English.If
tesseractis working, it will return the version information when you typetesseract -vin command prompt as below.