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



几个月前我写了这段代码,想通过它来清理它并添加一些新功能。这是一个简单的工具,我用来拍摄屏幕照片并从中获取可写的单词。我使用的是一台新计算机,而不是我最初编写代码的计算机;但是,我通过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:UsersdanteAnaconda3libsite-packagespytesseractpytesseract.py", line 184, in run_tesseract
proc = subprocess.Popen(cmd_args, **subprocess_args())
File "C:UsersdanteAnaconda3libsubprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "C:UsersdanteAnaconda3libsubprocess.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:UsersdanteAnaconda3libsite-packagespytesseractpytesseract.py", line 309, in image_to_string
}[output_type]()
File "C:UsersdanteAnaconda3libsite-packagespytesseractpytesseract.py", line 308, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "C:UsersdanteAnaconda3libsite-packagespytesseractpytesseract.py", line 218, in run_and_get_output
run_tesseract(**kwargs)
File "C:UsersdanteAnaconda3libsite-packagespytesseractpytesseract.py", line 186, in run_tesseract
raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path```

问题在于我对模块缺乏了解。 pytesseract不是OCR,它只是一个允许用户使用谷歌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,假设您已正确安装所有内容。我花了比我愿意承认的时间更长的时间才能找到这个问题的明显解决方案,但希望将来有这个问题的人比我更快地解决它!谢谢,祝你有美好的一天。