几个月前,我编写了这段代码,并希望通过它来清理它并添加一些新功能。它是我用来拍摄屏幕图片并从中获取可写单词的简单工具。我使用的是我最初编写代码的计算机所在的新计算机;但是,我通过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```
答案 0 :(得分:0)
问题出在我对模块的了解不足。 pytesseract不是OCR,它只是允许用户使用Google OCR的翻译器。这意味着,要使用此软件包,用户必须安装Google的OCR(我从此处https://sourceforge.net/projects/tesseract-ocr-alt/files/下载了我的OCR)。
这不是;但是,解决所有问题。 pytesseract软件包需要知道实际的OCR程序在哪里。 pytesseract.py脚本的第35行上有一行告诉pytesseract在哪里可以找到实际的Google OCR tesseract程序
total = 0
with open('numbers.txt', 'r') as inp, open('outputnumbers.txt', 'w') as outp:
for line in inp:
try:
num = float(line)
total += num
outp.write(line)
except ValueError:
print('{} is not a number!'.format(line))
print('Total of all numbers: {}'.format(total))
如果您在Windows上,并且尚未手动将tesseract添加到路径中(如果您不知道这意味着什么,请按照以下步骤进行操作),则需要将该行替换为Google OCR的实际位置在你的电脑。将该行替换为
tesseract_cmd = 'tesseract'
应该允许您在正确安装所有组件的情况下运行pytesseract。比让我承认要找到明显解决该问题的办法花了我更长的时间,但希望将来有这个问题的人能比我更快地解决它!谢谢,祝你有美好的一天。