python - 在与以前工作正常的完全相同的代码上收到 "pytesseract not in your path"错误

标签 python python-3.x pycharm python-tesseract

我几个月前编写了这段代码,并希望通过它来清理它并添加一些新功能。它是一个简单的工具,我用来给屏幕拍照并从中获取可写的文字。我现在使用的电脑是我最初编写代码的电脑;但是,我通过 pycharm 模块管理器检查并安装了每个模块。但是,当我运行代码时,即使我已在路径中找到该包,我仍然会收到此错误。任何帮助将不胜感激。

我已经查找了我的问题的几种不同变体,但它们似乎都有不同的原因和修复方法,当然,没有一个对我有用。

    if c ==2:
        img = ImageGrab.grab(bbox=(x1-5, y1-5, x2+5,y2+5))  # bbox specifies region (bbox= x,y,width,height)
        img_np = np.array(img)
        frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
        c = 0
        x = 0
        string = str(pytesseract.image_to_string(frame)).lower()
        print(string)

这是代码中唯一引用 pytesseract 的部分,当然除了“import pytesseract”之外。希望我能够再次启动并运行此代码以及 pytesseract 模块,因为它是我的许多脚本不可或缺的一部分。预先感谢您的帮助。

  File "C:\Users\dante\Anaconda3\lib\site-packages\pytesseract\pytesseract.py", line 184, in run_tesseract
    proc = subprocess.Popen(cmd_args, **subprocess_args())
  File "C:\Users\dante\Anaconda3\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "C:\Users\dante\Anaconda3\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/dante/Desktop/DPC/processes/screen_to_text.py", line 29, in <module>
    string = str(pytesseract.image_to_string(frame)).lower()
  File "C:\Users\dante\Anaconda3\lib\site-packages\pytesseract\pytesseract.py", line 309, in image_to_string
    }[output_type]()
  File "C:\Users\dante\Anaconda3\lib\site-packages\pytesseract\pytesseract.py", line 308, in <lambda>
    Output.STRING: lambda: run_and_get_output(*args),
  File "C:\Users\dante\Anaconda3\lib\site-packages\pytesseract\pytesseract.py", line 218, in run_and_get_output
    run_tesseract(**kwargs)
  File "C:\Users\dante\Anaconda3\lib\site-packages\pytesseract\pytesseract.py", line 186, in run_tesseract
    raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path```

最佳答案

问题在于我对模块缺乏了解。 pytesseract 不是 OCR,它只是一个允许用户使用 google OCR 的翻译器。这意味着,为了使用这个包,用户必须安装谷歌的 OCR(我从这里下载了我的 https://sourceforge.net/projects/tesseract-ocr-alt/files/ )。

这不是;但是,解决整个问题。 pytesseract 包需要知道实际的 OCR 程序所在的位置。在 pytesseract.py 脚本的第 35 行,有一行告诉 pytesseract 在哪里可以找到实际的 google OCR tesseract 程序

tesseract_cmd = 'tesseract'

如果您使用的是 Windows,并且尚未手动将 tesseract 添加到您的路径中(如果您不知道这意味着什么,只需按照以下步骤操作),那么您需要将该行替换为 google OCR 的实际位置在你的电脑。将该行替换为

tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract'

假设您已经正确安装了所有内容,应该允许您运行 pytesseract。我花了比我愿意承认的更长的时间来找到这个问题的明显解决方案,但希望将来遇到这个问题的人比我更快地解决它!谢谢,祝你有美好的一天。

关于python - 在与以前工作正常的完全相同的代码上收到 "pytesseract not in your path"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55582511/

相关文章:

python-3.x - Python Selenium find_element_by_xpath

Pycharm跨2个显示器全屏: is this possible?

python - PyCharm,从项目文件夹中解析一个库

python - 遍历文件从列表中查找单词时的 findall() 正则表达式

python - 如何使用列表对 Pandas 数据框进行子集化

python - 为什么 C、C++ 和 LISP 在嵌入式设备和机器人中如此流行?

python - 在 python : Unknown characters 中读取 arduino 串行连接

json - 读取json并访问多个key

pycharm - 设置默认解释器并在 PyCharm 2017 中不使用 virtualenv

Python 从字符串中删除嵌套的 unicode 'u' 符号