0

I trying to capture a part of the current screen to detect some number on the screen, but when the code run got this error:

Traceback (most recent call last):
  File "C:/Users/Administrator/PycharmProjects/bot/detect_num.py", line 12, in <module>
    print(pytesseract.image_to_string(Image.open('test.jpg')))
  File "C:\Python35\lib\site-packages\pytesseract\pytesseract.py", line 161, in image_to_string
    config=config)
  File "C:\Python35\lib\site-packages\pytesseract\pytesseract.py", line 94, in run_tesseract
    stderr=subprocess.PIPE)
  File "C:\Python35\lib\subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "C:\Python35\lib\subprocess.py", line 1224, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

source code:

import pyscreenshot as ImageGrab
from PIL import Image
import subprocess
from pytesseract import *

if __name__=="__main__":
    im = ImageGrab.grab(bbox=(1349, 34, 1357, 45))
    im = im.convert('1')
    im.save('test.jpg', 'JPEG')
    im.show()
    print(pytesseract.image_to_string(Image.open('test.jpg')))

Please some one tell me why, and how to fix it?

AlphaWolf
  • 395
  • 2
  • 7
  • 16
  • Possible duplicate of ["WindowsError: \[Error 2\] The system cannot find the file specified" is not resolving](http://stackoverflow.com/questions/18757127/windowserror-error-2-the-system-cannot-find-the-file-specified-is-not-resol) – Pavneet_Singh Sep 14 '16 at 10:18
  • I have tried to remove the line when call subprocess(another answer advise add it to fix) and add some line of that anwser in your suggest but it still error :(. @PavneetSingh – AlphaWolf Sep 14 '16 at 11:44

3 Answers3

2

When you are using pytesseract, first you have to make sure that you have installed Tesseract-OCR in your system. Then you have to insert the path of the tesseract in your code, as below

from PIL import Image
import pytesseract

pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract 
OCR/tesseract'

You can download the Tesseract-OCR form https://github.com/UB-Mannheim/tesseract/wiki

0

The issue here is that you haven't installed a necessary dependency. When you read pyteseeract's documentation you see the following text:

  • Install google tesseract-ocr from http://code.google.com/p/tesseract-ocr/ . You must be able to invoke the tesseract command as "tesseract". If this isn't the case, for example because tesseract isn't in your PATH, you will have to change the "tesseract_cmd" variable at the top of 'tesseract.py'.

I presume you haven't yet performed that step, so there's no tesseract command to actually do the OCR work required.

holdenweb
  • 33,305
  • 7
  • 57
  • 77
0

https://digi.bib.uni-mannheim.de/tesseract/

from this link, you can get an .exefile, which will make you install the tesseract easier. then you can add the path of the tesseract into environment path. last you can use pytesseract

Tom.chen.kang
  • 173
  • 2
  • 9