Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python OpenCv窗口在显示';点击_Python_Python 3.x_Opencv_Pywin32 - Fatal编程技术网

Python OpenCv窗口在显示';点击

Python OpenCv窗口在显示';点击,python,python-3.x,opencv,pywin32,Python,Python 3.x,Opencv,Pywin32,我写这段代码是为了通过每秒钟截图来捕捉屏幕画面,但是当点击OpenCv窗口时,它会级联屏幕 当您将鼠标悬停在Opencv窗口上时,它会显示没有100个副本的屏幕。究竟是什么错误?图片: 使用I类的代码: from window_capture import Window_Capture from time import time import cv2 as cv import numpy loop_time = time() wincap = Window_Capture() whi

我写这段代码是为了通过每秒钟截图来捕捉屏幕画面,但是当点击OpenCv窗口时,它会级联屏幕

当您将鼠标悬停在Opencv窗口上时,它会显示没有100个副本的屏幕。究竟是什么错误?图片:

使用I类的代码:

from window_capture import Window_Capture
from time import time
import cv2 as cv
import numpy

loop_time = time()

wincap = Window_Capture()

while True:
    screenshot = wincap.screenshare()

    cv.imshow('Screen', screenshot)

    print("FPS {}".format(round(1 / (time() - loop_time))))
    loop_time = time()

    if cv.waitKey(1) == ord('q'):
        print("Capture Ended")
        cv.destroyWindow(screenshot)
用于屏幕捕获的类:

import pyautogui
import numpy
import cv2 as cv
from time import time
from PIL import ImageGrab
import win32gui, win32ui, win32con
from win32api import GetSystemMetrics
import time


class Window_Capture:
    w = 0
    h = 0
    hwnd = None

    def __init__(self, window_name=None):
        # checking is windowname exists

        if window_name is None:
            print("No window set to capture: Setting to active Window")
            time.sleep(3)
            self.hwnd = win32gui.GetDesktopWindow()
            print(win32gui.GetWindowText(win32gui.GetDesktopWindow()))
        else:
            # self.hwnd = win32gui.FindWindow(None, window_name)
            self.hwnd = win32gui.FindWindow(None, window_name)
            print("Window Found: Setting to preferred Window")

        # define monitor height and width and hwnd
        self.w = GetSystemMetrics(0)
        self.h = GetSystemMetrics(1)

    def screenshare(self):

        wDC = win32gui.GetWindowDC(self.hwnd)
        dcObj = win32ui.CreateDCFromHandle(wDC)
        cDC = dcObj.CreateCompatibleDC()
        dataBitMap = win32ui.CreateBitmap()
        dataBitMap.CreateCompatibleBitmap(dcObj, self.w, self.h)
        cDC.SelectObject(dataBitMap)
        cDC.BitBlt((0, 0), (self.w, self.h), dcObj, (0, 0), win32con.SRCCOPY)

        signedIntsArray = dataBitMap.GetBitmapBits(True)
        img = numpy.frombuffer(signedIntsArray, dtype='uint8')
        img.shape = (self.h, self.w, 4)

        dcObj.DeleteDC()
        cDC.DeleteDC()
        win32gui.ReleaseDC(self.hwnd, wDC)
        win32gui.DeleteObject(dataBitMap.GetHandle())

        img = img[..., :3]
        img = numpy.ascontiguousarray(img)

        return img
此错误阻止我进入图像检测项目的下一部分。

更好的解决方案是:

import time

import cv2
import numpy as np
from mss import mss

#import pyautogui
#width, height= pyautogui.size()
width=900
height=900

mon = {'top': 0, 'left': 0, 'width': width, 'height': height}
with mss() as sct:
    # mon = sct.monitors[0]
    while True:
        last_time = time.time()
        img = sct.grab(mon)
        print('fps: {0}'.format(1 / (time.time()-last_time)))
        cv2.imshow('test', np.array(img))
        if cv2.waitKey(25) & 0xFF == ord('q'):
            cv2.destroyAllWindows()
            break
注:

当你悬停时,你不会看到很多窗口,因为你看到的是完整的窗口,而opencv窗口没有打开,否则你会看到很多窗口,你会看到一个层叠,因为你会重复拍摄窗口本身的屏幕截图

就像站在两面镜子之间