1

I've written a python/cv2 image to text converter. When starting up the program I enter C:\Users\mikez\Pictures\examples.png when it was asking for the image.

Thereafter it shows the follwing error:

Traceback: "WindowsError: [Error 2] The system cannot find the file specified".

I don't know what is wrong with my code:

from PIL import Image
import pytesseract
import os
import cv2

ppc = True
im = raw_input("Enter Image: ")
image = cv2.imread(im)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

while ppc == True:
    prepro = raw_input("Enter preprocess: ").lower()
    if prepro == "thresh" or prepro == "t":
        gray = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
        ppc = False

    elif prepro == "blur" or prepro == "b":
        gray = cv2.medianBlur(gray, 3)
        ppc = False    

    elif prepro == "no" or "n":
        ppc = False

    else:
        print "Not and option."
        ppc = True

filename = "{}.png".format(os.getpid())
cv2.imwrite(filename, gray)

text = pytesseract.image_to_string(Image.open(filename))
os.remove(filename)
print(text)

cv2.imshow("Image", image)
cv2.imshow("Output", gray)
cv2.waitKey(0)
Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
Mike Z
  • 11
  • 1

1 Answers1

0

To see what's going on with your code I used below simple debugging implementation and tried it. Then I found out its not the filepath of *.png. Then I saw tempfile-names defined in pytesseract.py. This pytesseract converts and saves the new temp-png file, creates a new temporariy bmp file (changed colorscheme) and when done it passes that as command to Tesseract...

... and because Tesseract is not installed on my machine I get the error which reads like yours:

Traceback (most recent call last):

File "D:......\Image text converter.py", line 55, in text = pytesseract.image_to_string(Image.open(filename))

File "c:......\lib\site-packages\pytesseract\pytesseract.py", line 132, in image_to_string config=config)

File "c:......\lib\site-packages\pytesseract\pytesseract.py", line 50, in run_tesseract proc = subprocess.Popen(command, stderr=subprocess.PIPE)

File "c:......\lib\subprocess.py", line 390, in init errread, errwrite)

File "c:......\lib\subprocess.py", line 640, in _execute_child startupinfo) WindowsError: [Error 2] The system cannot find the file specified

So...

My Conclusion:

WindowsError: [Error 2] The system cannot find the file specified is the systems way of telling you indirectly that Tesseract is not installed/compiled...etc. And has nothing to do with your input *.png filpath.

Suggestion:

Install Tesseract from here and see if the error persist. If solved report it here as answer and "accept answer" which will give us both rep.

Code for debugging purpose:

Implement after "im" in your code:

im = raw_input("Enter Image: ")  # just to shows what "im" was.
print im                         # debuggin: show filepath to use.

if not os.path.exists(im):       # checks if filepath is true.
    print 'filepath "%s" is incorrect, exit..' % im
    exit()
else:
    print "filepath correct, continue.."

... and finally a bonus to be come debugging expert yourself...

Hacking pytesseract.py

When you add:

  1. time.sleep(10) at:

    import time    # needs to be imported too...
    
    def image_to_string(image, lang=None, boxes=False, config=None):
        ...snippet...
        try:
            image.save(input_file_name)
            time.sleep(10)
            status, error_string = run_tesseract(input_file_name,
                                                 output_file_name_base,
                                                 lang=lang,
                                                 boxes=boxes,
                                                 config=config)
    
  2. replace: filename = "{}.png".format(os.getpid()) with filename = "Test_{}.png".format(os.getpid()) in your code ....

... You then will be rewarded and see the creation of the tempfile at: C:\Users\...\AppData\Local\Temp

Enjoy.. and let us know if its fixed by installing the Tesseract library.

ZF007
  • 3,708
  • 8
  • 29
  • 48