我如何在Python窗口中摆脱黑色酒吧?

发布于 2025-01-23 16:13:12 字数 2068 浏览 2 评论 0原文

我正在使用OpenCV和Python制作游戏Cuphead的对象检测项目。现在,我正在尝试实时捕获对象,但是当检测窗口显示时,我将这个稀有的黑色栏放在顶部,我不知道如何摆脱它,这是我看到的,在左侧我的对象检测窗口和右侧的cuphead游戏窗口中。

这是用于此类的类的代码:

import numpy as np
import win32gui, win32ui, win32con

class WindowCapture:

    # define monitor's width and height
    w = 0
    h = 0
    hwnd = None

    # constructor
    def __init__(self, window_name):
        
        if window_name is None: # if we don't pass any window names capture desktop
            self.hwnd = win32gui.GetDesktopWindow()
        else:
            # Find the game window
            self.hwnd = win32gui.FindWindow(None, window_name)
            if not self.hwnd:
                raise Exception("Window not founnd: {}".format(window_name))

            # define window's widht and height. the resolution we'll work with
            window_rect = win32gui.GetWindowRect(self.hwnd)
            self.w = window_rect[2] - window_rect[0]
            self.h = window_rect[3] - window_rect[1]

    def get_screenshot(self):
        

        # get the window image data
        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)

        # create the screenshot image that we want to return to be processed
        signedIntsArray = dataBitMap.GetBitmapBits(True)
        img = np.fromstring(signedIntsArray, dtype='uint8')
        img.shape = (self.h, self.w, 4)

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

        # get rid of the alpha channel in the img
        img = img[..., :3]
        img = np.ascontiguousarray(img)

        return img

I'm working on a Object Detection project for the game Cuphead using OpenCV and Python. Now I'm trying to capture objects in real time but when the detection window displays I get this rare black bar on the top and I don't know how to get rid of it, here's what I see, on the left my object detection window and in the right the Cuphead game window.

Here's the code for the class used for this:

import numpy as np
import win32gui, win32ui, win32con

class WindowCapture:

    # define monitor's width and height
    w = 0
    h = 0
    hwnd = None

    # constructor
    def __init__(self, window_name):
        
        if window_name is None: # if we don't pass any window names capture desktop
            self.hwnd = win32gui.GetDesktopWindow()
        else:
            # Find the game window
            self.hwnd = win32gui.FindWindow(None, window_name)
            if not self.hwnd:
                raise Exception("Window not founnd: {}".format(window_name))

            # define window's widht and height. the resolution we'll work with
            window_rect = win32gui.GetWindowRect(self.hwnd)
            self.w = window_rect[2] - window_rect[0]
            self.h = window_rect[3] - window_rect[1]

    def get_screenshot(self):
        

        # get the window image data
        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)

        # create the screenshot image that we want to return to be processed
        signedIntsArray = dataBitMap.GetBitmapBits(True)
        img = np.fromstring(signedIntsArray, dtype='uint8')
        img.shape = (self.h, self.w, 4)

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

        # get rid of the alpha channel in the img
        img = img[..., :3]
        img = np.ascontiguousarray(img)

        return img

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-01-30 16:13:12

似乎img.shape =(self.h,self.w,4)导致问题。如 iinsable说,

在Windows Vista中,后来,Window Rect现在包括该区域
被滴阴影占据。

It seems img.shape = (self.h, self.w, 4) causes the problem. As GetWindowRect and @IInspectable said,

In Windows Vista and later, the Window Rect now includes the area
occupied by the drop shadow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文