1

我想构建一个项目,从我的电脑屏幕的一部分连续读取文本并将它们显示在 pycharm 的控制台上

我正在使用 python 3,所有模块都是使用 pycharm 控制台中的 pip 安装的。

我使用了这段代码:

import time
import cv2
import mss
import numpy
import pytesseract


mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}

with mss.mss() as sct:
    while True:
        im = numpy.asarray(sct.grab(mon))
        # im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)

        text = pytesseract.image_to_string(im)
        print(text)

        cv2.imshow('Image', im)

        # Press "q" to quit
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break

        # One screenshot per second
        time.sleep(1)

得到这个我无法处理的错误:

C:\Users\ali91\PycharmProjects\OCR\venv\Scripts\python.exe
"C:/Users/ali91/PycharmProjects/OCR/lesson 1.py" Traceback (most
recent call last):   File
"C:\Users\ali91\PycharmProjects\OCR\venv\lib\site-packages\pytesseract\pytesseract.py",
line 252, in run_tesseract
    proc = subprocess.Popen(cmd_args, **subprocess_args())   File "C:\Program Files\Python39\lib\subprocess.py", line 947, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,   File "C:\Program Files\Python39\lib\subprocess.py", line 1416, in
_execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args, 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\ali91\PycharmProjects\OCR\lesson 1.py", line 15, in <module>
    text = pytesseract.image_to_string(im)   File "C:\Users\ali91\PycharmProjects\OCR\venv\lib\site-packages\pytesseract\pytesseract.py",
line 413, in image_to_string
    return {   File "C:\Users\ali91\PycharmProjects\OCR\venv\lib\site-packages\pytesseract\pytesseract.py",
line 416, in <lambda>
    Output.STRING: lambda: run_and_get_output(*args),   File "C:\Users\ali91\PycharmProjects\OCR\venv\lib\site-packages\pytesseract\pytesseract.py",
line 284, in run_and_get_output
    run_tesseract(**kwargs)   File "C:\Users\ali91\PycharmProjects\OCR\venv\lib\site-packages\pytesseract\pytesseract.py",
line 256, in run_tesseract
    raise TesseractNotFoundError() pytesseract.pytesseract.TesseractNotFoundError: tesseract is not
installed or it's not in your PATH. See README file for more
information.
4

1 回答 1

0
  1. 通过以下方式验证您是否已正确安装 pytesseract(全局或虚拟环境) pip install pytesserect
  2. 在 PATH 中添加 pytesseract 变量/可执行文件以在项目位置中使用它:

对于全局安装:

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

对于虚拟环境安装:

pytesseract.pytesseract.tesseract_cmd = 'LOCATION_TO_VENV\Tesseract-OCR\tesseract.exe'

将 USER 替换为您计算机的用户名,并将 LOCATION_TO_VENV 替换为您的虚拟环境的绝对位置。

于 2021-07-08T17:17:10.363 回答