简体   繁体   English

FileNotFoundError:[WinError 2] | 使用Python,OpenCV和OCR从图像读取文本时出错

[英]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. 我在此示例代码中遇到了有关如何使用Python,OpenCV和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. 这段代码是使用python 2.7构建的,而我使用的是python 3.6,所以也许我错过了这些版本之间的一些更改。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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