如何使用python直接从屏幕读取文本

pvabu6sv  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(359)

此问题已在此处找到答案

如何解决TesseRactNotFound错误((24个答案)
昨天关门了。
我想建立一个项目,从我的电脑屏幕的一部分连续读取文本,并显示在pycharm的控制台上
我使用的是Python3,所有模块都是使用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.
6za6bjd0

6za6bjd01#

验证您是否已通过以下方式正确安装PyteSeract(全局或虚拟环境) pip install pytesserect 在路径中添加pytesseract变量/可执行文件,以便在项目位置中使用它,如下所示:
对于全局安装: pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe' 对于虚拟环境安装: pytesseract.pytesseract.tesseract_cmd = 'LOCATION_TO_VENV\Tesseract-OCR\tesseract.exe' 将用户替换为计算机的用户名,并将位置替换为虚拟环境的绝对位置。

相关问题