1

I want to build project that reads texts continuously from part of my pc screen and shows them on the console of pycharm

I am using python 3, all modules are installed using pip from pycharm console.

I used this code:

import time
import cv2
import mss
import numpy
import pytesseract


mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}

with mss.mss() as sct:
    while True:
        im = numpy.asarray(sct.grab(mon))
        # im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

        text = pytesseract.image_to_string(im)
        print(text)

        cv2.imshow('Image', im)

        # Press "q" to quit
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

        # One screenshot per second
        time.sleep(1)

got this error which I couldn't deal with :

C:\Users\ali91\PycharmProjects\OCR\venv\Scripts\python.exe
"C:/Users/ali91/PycharmProjects/OCR/lesson 1.py" Traceback (most
recent call last):   File
"C:\Users\ali91\PycharmProjects\OCR\venv\lib\site-packages\pytesseract\pytesseract.py",
line 252, in run_tesseract
    proc = subprocess.Popen(cmd_args, **subprocess_args())   File "C:\Program Files\Python39\lib\subprocess.py", line 947, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,   File "C:\Program Files\Python39\lib\subprocess.py", line 1416, in
_execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args, FileNotFoundError: [WinError 2] The system cannot find the file
specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last):   File
"C:\Users\ali91\PycharmProjects\OCR\lesson 1.py", line 15, in <module>
    text = pytesseract.image_to_string(im)   File "C:\Users\ali91\PycharmProjects\OCR\venv\lib\site-packages\pytesseract\pytesseract.py",
line 413, in image_to_string
    return {   File "C:\Users\ali91\PycharmProjects\OCR\venv\lib\site-packages\pytesseract\pytesseract.py",
line 416, in <lambda>
    Output.STRING: lambda: run_and_get_output(*args),   File "C:\Users\ali91\PycharmProjects\OCR\venv\lib\site-packages\pytesseract\pytesseract.py",
line 284, in run_and_get_output
    run_tesseract(**kwargs)   File "C:\Users\ali91\PycharmProjects\OCR\venv\lib\site-packages\pytesseract\pytesseract.py",
line 256, in run_tesseract
    raise TesseractNotFoundError() pytesseract.pytesseract.TesseractNotFoundError: tesseract is not
installed or it's not in your PATH. See README file for more
information.
BoarGules
  • 16,440
  • 2
  • 27
  • 44
Alimo
  • 11
  • 1
  • 1
  • 4
  • 1
    The message says that you don't have the tesseract application installed, or if you do, not somewhere that your code can find it. – BoarGules Jul 08 '21 at 16:38
  • See [this answer](https://stackoverflow.com/a/57912342/9705687) for explanation of how to tell tesseract where to look for executable. – bfris Jul 08 '21 at 17:32
  • Duplicate: [How do I resolve a TesseractNotFoundError?](https://stackoverflow.com/questions/50655738/how-do-i-resolve-a-tesseractnotfounderror) – HansHirse Jul 09 '21 at 06:36

1 Answers1

3
  1. Verify that you have installed pytesseract correctly (globally or virtual environment) by pip install pytesserect
  2. Add the pytesseract variable/executable in your PATH to use it in your project location as:

For global installation:

pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'

For Virtual environment installation:

pytesseract.pytesseract.tesseract_cmd = 'LOCATION_TO_VENV\Tesseract-OCR\tesseract.exe'

Replace USER by your computer's username and LOCATION_TO_VENV to absolute location of your virtual environment.

Jishan Shaikh
  • 1,572
  • 2
  • 13
  • 31