From 3b3c59fef5362f0b06bf2a913d32cff688e75073 Mon Sep 17 00:00:00 2001 From: pbyf83ift <1030227026@qq.com> Date: Tue, 2 Jul 2024 22:04:26 +0800 Subject: [PATCH] ADD file via upload --- region_detection.py | 80 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 region_detection.py diff --git a/region_detection.py b/region_detection.py new file mode 100644 index 0000000..711af57 --- /dev/null +++ b/region_detection.py @@ -0,0 +1,80 @@ + +import win32gui,win32ui,win32con +import numpy as np + + +class Region_Detector(object): + def __init__(self,win_name,win_more): + self.win_name = win_name + self.win_more = win_more + + def getWindowHandle(self): + hwnd_title = {} + def getAllWindowHandle(hwnd,use): + if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd): + hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)}) + + win32gui.EnumWindows(getAllWindowHandle,None) + new_dict = {v: k for k, v in hwnd_title.items()} + return new_dict[self.win_name] + @staticmethod + def getButtonInfo(): + hwnd_title = {} + + def getAllWindowHandle(hwnd, use): + if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd): + hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)}) + + win32gui.EnumWindows(getAllWindowHandle, None) + return hwnd_title + def setWindows(self,hwdn,win_size=[1920, 1080],win_index=[0,0]): + """设置窗口大小尺寸并置顶""" + win32gui.SetWindowPos(hwdn, win32con.HWND_TOPMOST, win_index[0], win_index[1], win_size[0], win_size[1], win32con.SWP_NOSIZE| win32con.SWP_SHOWWINDOW) + + def getWindowsRGB(self,hwnd): + """游戏窗口截图""" + hwndDC = win32gui.GetWindowDC(hwnd) + # 根据窗口句柄获取窗口的设备上下文DC(Device Context) + mfcDC = win32ui.CreateDCFromHandle(hwndDC) + # 根据窗口的DC获取mfcDC + saveDC = mfcDC.CreateCompatibleDC() + # mfcDC创建可兼容的DC + saveBitMap = win32ui.CreateBitmap() + # 创建bigmap准备保存图片 + rctA = win32gui.GetWindowRect(hwnd) + Screen_w = rctA[2] - rctA[0] # 游戏界面宽度 + Screen_h = rctA[3] - rctA[1] # 游戏界面高度 + + # 获取图片大小 + # 截取从左上角(0,0)长宽为(w,h)的图片 + saveBitMap.CreateCompatibleBitmap(mfcDC, Screen_w, Screen_h) + # 为bitmap开辟空间 + saveDC.SelectObject(saveBitMap) + # 高度saveDC,将截图保存到saveBitmap中 + saveDC.BitBlt((0, 0), (Screen_w, Screen_h), mfcDC, (0, 0), win32con.SRCCOPY) + + signedIntsArray = saveBitMap.GetBitmapBits(True) + img = np.frombuffer(signedIntsArray, dtype="uint8") + img.shape = (Screen_h, Screen_w, 4) + # bit图转mat图 + win32gui.DeleteObject(saveBitMap.GetHandle()) + mfcDC.DeleteDC() + saveDC.DeleteDC() + # 释放内存 + return img, (Screen_w,Screen_h) # 转为RGB图返回 + + def getRegion(self,img,Screen): + Screen_w,Screen_h = Screen[0],Screen[1]-self.win_more + Screen_cx = Screen_w // 2 # 游戏界面中心x + Screen_cy = Screen_h // 2 # 游戏界面中心y + Screen_c = [Screen_cx, Screen_cy] # 游戏界面中心坐标 + x0 = Screen_w // 3 # 游戏界面检测框左上角x0 + y0 = Screen_h // 3 # 游戏界面检测框左上角y0 + x1 = 2*x0 # 游戏界面检测框右下角x1 + y1 = 2*y0 # 游戏界面检测框右下角y1 + # 选取roi = img[y0,y1,x0,x1]窗口 + roi = img[int(y0):int(y1),int(x0):int(x1)] + return roi,Screen_c,[x0,y0+self.win_more,x1,y1+self.win_more] + + +