在过去工作正常的完全相同的代码上收到 "pytesseract not in your path" 错误

Receiving "pytesseract not in your path" error on the exact same code that used to work fine

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

我查找了我的问题的几种不同变体,但它们似乎都有不同的原因和解决方法,当然,none 其中对我有用。

    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" 除外。希望我可以再次启动此代码并 运行ning 以及一般的 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,它只是一个允许用户使用 googles OCR 的翻译器。这意味着,为了使用这个包,用户必须安装 google 的 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 假设你已经正确安装了所有东西。我花了比我愿意承认的时间更长的时间来找到这个问题的明显解决方案,但希望将来遇到这个问题的人能比我更快地解决它!谢谢,祝你有美好的一天。