Search code examples
pythonopencvtextocrpython-tesseract

Problem in Using opencv and pytesseract for extracting text out of images in Python3.7 interpreter


I intend to use opencv and pytesseract to extract text of out images. On executing the following code in a Python 3.7 Interpreter, I am receiving a error I'm not sure I understand.

import cv2
import pytesseract
#tesseract.exe location provided 
pytesseract.tesseract_cmd=r'C:\\Users\\shree\\AppData\\Local\\Programs\\Tesseract-OCR\\tesseract.exe'
#image location provided
img=cv2.imread('C:\\Users\\shree\\Desktop\\hey.jpeg')
text=pytesseract.image_to_string(img)
print(text)

error shown

P.S:

  1. I have installed opencv and pytessearct in "C:\Users\shree\AppData\Local\Programs\Python\Python37-32\Scripts"
  2. Python 3.7 Interpreter is installed in "C:\Users\shree\AppData\Local\Programs\Python\Python37-32"
  3. Please excuse if this is an obvious/dumb question. Actually, I'm just starting out. I read about this at https://towardsdatascience.com/read-text-from-image-with-one-line-of-python-code-c22ede074cac

A Detailed answer and an alternate way(if required) would be highly appreciated. Thanks!


Solution

  • Replace the line,

    pytesseract.tesseract_cmd=r'C:\\Users\\shree\\AppData\\Local\\Programs\\Tesseract-OCR\\tesseract.exe'
    

    with,

    pytesseract.pytesseract.tesseract_cmd = r'C:\\Users\\shree\\AppData\\Local\\Programs\\Tesseract-OCR\\tesseract.exe'
    

    This should fix the issue if tesseract is installed properly.