【问题标题】:Python: Image to textPython:图像到文本
【发布时间】:2017-11-18 04:51:40
【问题描述】:

我正在尝试使用 openCV 和 python 来构建一个基本的脚本来从图像中读取文本。

现在我被这个错误提示找不到文件,问题是我无法理解这个回溯。哪个文件不存在?任何图书馆或其他一些问题。 它正在编写 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

【问题讨论】:

    标签: python opencv python-imaging-library ocr


    【解决方案1】:

    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.py”顶部的“tesseract_cmd”变量。

    尝试在命令外壳中输入tesseract,如果它不起作用,那么您还没有设置为使用该包。按照他们的安装说明进行操作。

    【讨论】:

    • Tesseract 已经安装了,会不会是因为我在 C:d 目录下安装了 tesseract 并且我在 D: 目录下运行 python 和 pytestesseract
    • 只是那个问题。我从 C 目录卸载了 Tesseract 并安装在 D 目录中。谢谢兄弟
    猜你喜欢
    • 2018-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 2019-08-05
    • 2011-07-21
    • 1970-01-01
    相关资源
    最近更新 更多