【问题标题】:TypeError: Expected Ptr<cv::UMat> for argument 'mat' using mss library in pythonTypeError: Expected Ptr<cv::UMat> for argument 'mat' using mss library in python
【发布时间】:2020-12-26 18:23:36
【问题描述】:

我正在尝试使用 mss 库截屏,并使用下面的代码显示它,但每次都会出现相同的错误。 这个错误有解决办法吗

TypeError: Expected Ptr<:umat> for argument 'mat'

** 我在 Macos 中使用这个而不是在 windows 中

import cv2 as cv
import numpy as np
import os
from time import time
from mss import mss

os.chdir(os.path.dirname(os.path.abspath(__file__)))

loop_time = time()
with mss() as sct:
    while (True):
        
        monitor_1 = sct.monitors[1]  # Identify the display to capture
        screenshot = sct.grab(monitor_1)
        
        cv.imshow('result', screenshot)
        
        print('FPS {}',format(1 / (time() - loop_time)))
        loop_time = time()
        
        if cv.waitKey(1) == ord('q'):
            cv.destroyAllWindows()
            break
    

print('done')

【问题讨论】:

    标签: python opencv python-mss


    【解决方案1】:

    您必须将“屏幕截图”转换为 cv::UMat :

    import time
    
    import cv2
    import mss
    import numpy
    
    
    with mss.mss() as sct:
        # Part of the screen to capture
        monitor = {"top": 40, "left": 0, "width": 800, "height": 640}
    
        while "Screen capturing":
            last_time = time.time()
    
            # Get raw pixels from the screen, save it to a Numpy array
            img = numpy.array(sct.grab(monitor))
    
            # Display the picture
            cv2.imshow("OpenCV/Numpy normal", img)
    
            # Display the picture in grayscale
            # cv2.imshow('OpenCV/Numpy grayscale',
            #            cv2.cvtColor(img, cv2.COLOR_BGRA2GRAY))
    
            print("fps: {}".format(1 / (time.time() - last_time)))
    
            # Press "q" to quit
            if cv2.waitKey(25) & 0xFF == ord("q"):
                cv2.destroyAllWindows()
                break
    

    这是来自here的示例

    【讨论】:

      猜你喜欢
      • 2019-08-03
      • 2020-10-03
      • 2021-04-15
      • 2021-01-29
      • 2019-12-13
      • 2021-05-29
      • 2019-06-12
      • 1970-01-01
      相关资源
      最近更新 更多