2017-06-15 66 views
-1

我想我的手在openCV和python上建立一个基本的脚本从图像中读取文本。Python:图像到文本

现在我对这个说File Not Found的错误感到困惑,问题是我无法理解这个回溯。哪个文件不在那里?任何图书馆或其他问题。 它正在写thresh.png和removednoise.png。

我正在使用python 3.6,如果有任何兼容性问题,请让我知道。 如果需要更多信息,请让我知道。

通过指向一个方向帮助我。在此先感谢

这是我的代码。

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

#src_path ="D:/python'/practice" 

def get_string(img_path): 
    img = cv2.imread(img_path) 
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
    kernel = np.ones((1, 1), np.uint8) 
    img = cv2.dilate(img, kernel, iterations=1) 
    img = cv2.erode(img, kernel, iterations=1) 

    cv2.imwrite("removed_noise.png", img) 
    img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) 
    cv2.imwrite("thres.png", img) 
    result = pytesseract.image_to_string(Image.open("thres.png")) 

    return result 


print ('------------TEXT------------') 

print (get_string("imag1.png")) 

错误消息:

D:\python'\practice>python OCR_text.py 
------------TEXT------------ 
Traceback (most recent call last): 
    File "OCR_text.py", line 25, in <module> 
    print (get_string("imag1.png")) 
    File "OCR_text.py", line 18, in get_string 
    result = pytesseract.image_to_string(Image.open("thres.png")) 
    File "D:\Python\lib\site-packages\pytesseract\pytesseract.py", line 122, in image_to_string 
    config=config) 
    File "D:\Python\lib\site-packages\pytesseract\pytesseract.py", line 46, in run_tesseract 
    proc = subprocess.Popen(command, stderr=subprocess.PIPE) 
    File "D:\Python\lib\subprocess.py", line 707, in __init__ 
    restore_signals, start_new_session) 
    File "D:\Python\lib\subprocess.py", line 990, in _execute_child 
    startupinfo) 
FileNotFoundError: [WinError 2] The system cannot find the file specified 

回答

0

FileNotFoundError是由subprocess,其正在尝试启动一个外部进程提高。没有找到它尝试启动的命令。你可以看到一对夫妇排队在回溯它说:

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

要使用pytesseract,你需要在系统上安装一些其他的依赖关系,请参阅本节:

https://pypi.python.org/pypi/pytesseract/0.1

安装:

  • Python-tesseract需要Python 2.5或更高版本。

  • 您将需要Python图像库(PIL)。在Debian/Ubuntu下,这是“python-imaging”软件包。

  • http://code.google.com/p/tesseract-ocr/安装google tesseract-ocr。您必须能够将tesseract命令作为“tesseract”调用 。如果不是这种情况,对于 示例,因为tesseract不在您的PATH中,您必须更改 “tesseract_cmd”变量,位于'tesseract.py'的顶部。

尝试在命令shell中键入tesseract,如果它不工作,那么你没有设置为使用这个包呢。按照他们的安装说明。

+0

Tesseract已安装,是否可以因为我在C:ddirectory中安装了tesseract,并且正在D:目录 –

+0

中运行python和pytestesseract,这只是该问题。我从C目录卸载了Tesseract并安装在D目录中。多谢兄弟 –