【问题标题】:Why is pytesseract throwing WinError 6?为什么 pytesseract 会抛出 WinError 6?
【发布时间】:2017-01-11 21:52:20
【问题描述】:

这是我第一次使用 pytesseract。我正在尝试对小图像执行简单的 OCR。代码归结为:

from PIL import Image
from pytesseract import image_to_string

test = Image.open(r'C:\test.jpg')
print(image_to_string(test))

这会抛出 OSError: [WinError 6] The handle is invalid

Traceback (most recent call last):
  File "C:\Testing.py", line 5, in <module>
    print(image_to_string(test))
  File "C:\\pytesseract.py", line 161, in image_to_string
    config=config)
  File "C:\\pytesseract.py", line 94, in run_tesseract
    stderr=subprocess.PIPE)
  File "C:\\subprocess.py", line 911, in __init__
    errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  File "C:\\subprocess.py", line 1150, in _get_handles
    c2pwrite = self._make_inheritable(c2pwrite)
  File "C:\\subprocess.py", line 1182, in _make_inheritable
    _winapi.DUPLICATE_SAME_ACCESS)
OSError: [WinError 6] The handle is invalid

我在 Windows 7 上使用 Python 3.5。

提前感谢您的宝贵时间!

【问题讨论】:

    标签: python python-3.x subprocess python-tesseract


    【解决方案1】:

    我不知道真正的问题,但重启电脑后问题就解决了。

    【讨论】:

    • 这更适合评论,因为它不直接帮助“回答”问题
    • 我无权发表评论。如果将来会照顾..谢谢。
    【解决方案2】:

    我遇到了同样的问题。所以我对 Python 的了解不够,无法解决所有各种文件,但我确实注意到问题是由于子进程文件造成的。不知何故,当pytesseract调用子进程时,脚本会抛出错误。

    无论如何,我通过完全跳过 pytesseract 并通过子进程访问 tesseract 解决了这个问题。我的代码如下:

    import subprocess
    from subprocess import Popen, PIPE
    image_file = r"C:\Temp\test.png"
    outlocation = r"C:\Temp\output"
    command = ['tesseract', image_file, outlocation]
    proc = subprocess.Popen(command,
            stderr=subprocess.PIPE)
    

    这在很大程度上是一种创可贴解决方案,但它确实让我能够继续在 python 中使用 tesseract。希望对您有所帮助。

    此外,上述解决方法假设您已经安装了 tesseract。如果你没有,你可以从https://github.com/UB-Mannheim/tesseract/wiki获取它

    【讨论】:

      猜你喜欢
      • 2021-12-20
      • 2021-03-05
      • 2021-03-19
      • 2023-03-14
      • 2021-04-09
      • 2014-12-18
      • 2013-12-08
      • 2010-09-27
      • 2015-05-02
      相关资源
      最近更新 更多