【问题标题】:Issues with running pytesseract in script在脚本中运行 pytesseract 的问题
【发布时间】:2018-03-09 22:35:53
【问题描述】:

我正在尝试在我的 python 脚本中使用 pytesseract 来读出图像中的一串文本,但我不断收到错误。我现在正在尝试这段代码:

try:
    import Image
except ImportError:
    from PIL import Image
import pytesseract

pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files 
(x86)\\Tesseract-OCR\\tesseract'
# Include the above line, if you don't have tesseract executable in 
#your PATH
# Example tesseract_cmd: 'C:\\Program Files (x86)\\Tesseract-
#OCR\\tesseract'

# Simple image to string
print(pytesseract.image_to_string(Image.open('IMG_9296.jpg')))

IMG_9296.jpg 文件位于我的桌面上:~/Desktop。我已经安装了 tesseract 和 pytesseract,因为在我的命令行中输入 tesseract 会返回有关它的信息。 当我在命令行中输入pip install pytesseract 时,我会返回:

Requirement already satisfied: pytesseract in /Library/Python/2.7/site-packages
Requirement already satisfied: Pillow in /Library/Python/2.7/site-packages (from pytesseract)

所以我猜/Library/Python/2.7/site-packages 是我通向 pytesseract 的路径,所以我尝试将它放入 pytesseract.pytesseract.tesseract_cmd,但没有奏效。无论哪种方式,我都会收到此错误(我认为来自import pytesseract 行):

ValueError: Attempted relative import in non-package

我是否需要进入不同的路径,或者在某处移动/复制 pytesseract?我不太确定发生了什么。

【问题讨论】:

  • PIL 和枕头不同 - 也许您的导入错误在 PIL 上?

标签: python tesseract valueerror python-tesseract


【解决方案1】:

首先通过输入代码检查您的命令提示符

!pip view pytesseract

如果它返回一些信息,表明你的系统中有它,你还需要查看 PIL 模块。

现在来回答你的主要问题


为了进行 pytesseact,您需要在 pytesseract.pytesseract.tesseract_cmd 路径上:-
此外,您还需要在路径上以及图像所在的位置。
那又如何可能你可以做如下

导入 pytesseract
导入 cv2

导入操作系统 os.chdir(FullPath_where_your_"tesseract.exe" 所在位置)

图像 = cv2.imread('full_path_of_your_image')
pytext = pytesseract.image_to_string(image)
打印(pytext)


如果它不起作用,请告诉我

学习愉快!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多