【问题标题】:Pytesseract, trying to detect text from on screenPytesseract,试图从屏幕上检测文本
【发布时间】:2018-01-05 17:04:08
【问题描述】:

我将 MSS 与 pytesseract 结合使用来尝试在屏幕上读取,以确定来自被监控区域的字符串。我的代码如下:

import Image
import pytesseract
import cv2
import os
import mss
import numpy as np

with mss.mss() as sct:
    mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}

    im = sct.grab(mon)
    im = np.asarray(im)
    im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    #im_gray = plt.imshow(im_gray, interpolation='nearest')

    cv2.imwrite("test.png", im_gray)
    #cur_dir = os.getcwd()
    text = pytesseract.image_to_string(Image.open(im_gray))
    print(text)

    cv2.imshow("Image", im)
    cv2.imshow("Output", im_gray)
    cv2.waitKey(0)

然后我返回以下错误:AttributeError: 'numpy.ndarray' object has no attribute 'read'

我还尝试使用 pyplot 将其转换回图像,如代码示例中的注释行所示。然而,这会打印出错误:TypeError: img is not a numpy array, not a scalar

我对 Python 有点陌生(周日才开始涉足它)。但是,我在其他检测图像方面的尝试相当成功。但是,为了达到我的最终目标,我需要能够阅读屏幕上的字符。它们将始终具有相同的字体和相同的大小,因此我不必担心缩放问题,但目前我正试图通过将图像存储在内存中来了解它是如何工作的(不保存到文件)从桌面上的回收站图标,并试图从图像中抓取字符串“回收站”。

更新 我想我可能会有一些突破,但是如果我试图同时显示流,就会出现一些问题。但是,我可以通过使用临时文件来足够快地处理流。

我的更新代码如下:

from PIL import Image
from PIL import ImageGrab
import pytesseract
import cv2
import os
import mss
import numpy as np
from matplotlib import pyplot as plt
import tempfile

png = tempfile.NamedTemporaryFile(mode="wb")

with mss.mss() as sct:
    #while True:
    mon = {'top': 0, 'left': 0, 'width': 150, 'height': 150}
    im = sct.grab(mon)
    im_array = np.asarray(im)
    #im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    #with tempfile.NamedTemporaryFile(mode="wb") as png:
    png.write(im_array)
    im_name = png.name
    print(png.name)

    #cv2.imwrite("test.png", im_gray)
    #cur_dir = os.getcwd()
    #text = pytesseract.image_to_string(Image.open(im_name))
    #print(text)
    cv2.imshow("Image", im_array)
#cv2.imshow("Output", im_gray)
cv2.waitKey(0)

这个目前吐出一个permission is denied错误,如下:

File "C:\Python\Python36-32\Lib\idlelib\ocr.py", line 27, in <module>
    text = pytesseract.image_to_string(Image.open(im_name))
  File "C:\Python\Python36-32\lib\site-packages\PIL\Image.py", line 2543, in open
    fp = builtins.open(filename, "rb")
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\JMCOLL~1\\AppData\\Local\\Temp\\tmp7_mwy2k9'

我怀疑这是否正常,我将在家里的笔记本电脑上尝试此更新。这可能是由于对工作笔记本电脑的限制,我只是没有时间解决这个问题。

我很困惑为什么在没有 while True: 循环的情况下显示图像可以正常工作,作为屏幕截图。但是,将其放入 while True: 循环会导致窗口冻结。

【问题讨论】:

    标签: python numpy pytesser python-mss


    【解决方案1】:

    我可以让这段代码工作:

    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)
    

    睡眠时间可能是一件好事,不会让你的 CPU 爆炸。

    【讨论】:

    • 对不起,我一直忙于学习和工作。我目前不得不重新映像我的笔记本电脑并且还没有安装库,但我一回到家就会尝试这个。谢谢你没有让这个死,它一直让我发疯!
    猜你喜欢
    • 2014-08-21
    • 1970-01-01
    • 2022-01-22
    • 2023-03-11
    • 2021-04-16
    • 1970-01-01
    • 2012-11-09
    • 2015-06-14
    相关资源
    最近更新 更多