简体   繁体   中英

FileNotFoundError: [WinError 2] | Errors reading text from image with Python, OpenCV and OCR

I'm having issues with this example code of how to read text from images using Python, OpenCV and OCR.

This code was built with python 2.7, and I'm using python 3.6 so maybe I'm missing some changes between these versions.

import cv2
import numpy as np
import pytesseract
from PIL import Image
src_path = "C:/Users/crist/Desktop/borrar/lectura/"
def get_string(img_path):
# Read image with opencv
img = cv2.imread(img_path)

# Convert to gray
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Apply dilation and erosion to remove some noise
kernel = np.ones((1, 1), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)
img = cv2.erode(img, kernel, iterations=1)

# Write image after removed noise
cv2.imwrite(src_path + "removed_noise.png", img)

#  Apply threshold to get image with only black and white
#img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 2)

# Write the image after apply opencv to do some ...
cv2.imwrite(src_path + "thres.png", img)

# Recognize text with tesseract for python
result = pytesseract.image_to_string(Image.open(src_path + "thres.png"))


#os.remove(temp)

return result


print ('--- Start recognize text from image ---')
print (get_string(src_path + "2.png"))

print ("------ Done -------")

Errors:

--- Start recognize text from image ---
Traceback (most recent call last):
  File "C:/Users/crist/PycharmProjects/LectorTexto/lectorCapcha.py", line 40, in <module>
    print (get_string(src_path + "2.png"))
  File "C:/Users/crist/PycharmProjects/LectorTexto/lectorCapcha.py", line 31, in get_string
    result = pytesseract.image_to_string(Image.open(src_path + "thres.png"))
  File "C:\Users\crist\AppData\Local\Programs\Python\Python36\lib\site-packages\pytesseract\pytesseract.py", line 122, in image_to_string
    config=config)
  File "C:\Users\crist\AppData\Local\Programs\Python\Python36\lib\site-packages\pytesseract\pytesseract.py", line 46, in run_tesseract
    proc = subprocess.Popen(command, stderr=subprocess.PIPE)
  File "C:\Users\crist\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 707, in __init__
    restore_signals, start_new_session)
  File "C:\Users\crist\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 992, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] El sistema no puede encontrar el archivo especificado

Process finished with exit code 1

Import all the followings->>>>>>>

import cv2
import numpy as np
import pytesseract
import imutils
from PIL import Image
from pytesseract import image_to_string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM