gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 23:11:52 26 4
gpt4 key购买 nike

关于如何使用 Python、OpenCV 和 OCR 从图像中读取文本的示例代码,我遇到了问题。

这段代码是用 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 -------")

错误:

--- 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 cv2
import numpy as np
import pytesseract
import imutils
from PIL import Image
from pytesseract import image_to_string

关于python - FileNotFoundError : [WinError 2] | Errors reading text from image with Python, OpenCV 和 OCR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48324203/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com