From d85396e5ea4f628923c7e4fdfb814c8809cdf274 Mon Sep 17 00:00:00 2001 From: pmj110119 <929041691@qq.com> Date: Fri, 15 Oct 2021 16:29:44 +0800 Subject: [PATCH 1/2] 1.Avoid timeout when pip install. 2.Reset bug version. --- requirements.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.py b/requirements.py index 4290587..0d09a01 100644 --- a/requirements.py +++ b/requirements.py @@ -27,4 +27,4 @@ for line in pkgs.split('\n'): if len(line)>0: - pip.main(['install', *line.split()]) + pip.main(['install', '--default-timeout=100', *line.split()]) From 30ad8f9cce91f2234a0cb411d266516a6e7d7512 Mon Sep 17 00:00:00 2001 From: pmj110119 <929041691@qq.com> Date: Fri, 15 Oct 2021 16:32:23 +0800 Subject: [PATCH 2/2] 1.Avoid timeout when pip install. 2.Reset bug version. --- README.md | 1 + config.yaml | 6 ++++ utils/utils.py | 89 ++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 83 insertions(+), 13 deletions(-) create mode 100644 config.yaml diff --git a/README.md b/README.md index d93a222..e339d18 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ pip install -U pip python requirements.py --cuda [cuda版本] #例如安装的CUDA11.x python requirements.py --cuda 110 +python requirements.py --cuda 110 --proxy http://127.0.0.1:1080 # use proxy to speed up ``` 可能会有Time out之类的报错,多试几遍,github太卡。 diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..547be0b --- /dev/null +++ b/config.yaml @@ -0,0 +1,6 @@ +--- + windows: + monitor_width: 1920 + monitor_height: 1080 + game: + window_name: "原神" \ No newline at end of file diff --git a/utils/utils.py b/utils/utils.py index 4798fa2..97a1283 100644 --- a/utils/utils.py +++ b/utils/utils.py @@ -3,43 +3,106 @@ import cv2 import pyautogui import numpy as np -import win32api, win32con +import win32api, win32con, win32gui, win32ui +from pathlib import Path +import yaml + +CONFIG_PATH = Path(__file__).parent.parent.joinpath("config.yaml") +assert CONFIG_PATH.is_file() + + +with open(CONFIG_PATH, encoding='utf-8') as f: + result = yaml.safe_load(f) + DEFAULT_MONITOR_WIDTH = result.get("windows").get("monitor_width") + DEFAULT_MONITOR_HEIGHT = result.get("windows").get("monitor_height") + WINDOW_NAME = result.get("game").get("window_name") + + +# def cap(region=None): +# img = pyautogui.screenshot(region=region) if region else pyautogui.screenshot() +# return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) + def cap(region=None): - img = pyautogui.screenshot(region=region) if region else pyautogui.screenshot() + if region is not None: + left, top, w, h = region + # w = x2 - left + 1 + # h = y2 - top + 1 + else: + w = DEFAULT_MONITOR_WIDTH # set this + h = DEFAULT_MONITOR_HEIGHT # set this + left = 0 + top = 0 + + hwnd = win32gui.FindWindow(None, WINDOW_NAME) + # hwnd = win32gui.GetDesktopWindow() + wDC = win32gui.GetWindowDC(hwnd) + dcObj = win32ui.CreateDCFromHandle(wDC) + cDC = dcObj.CreateCompatibleDC() + dataBitMap = win32ui.CreateBitmap() + + dataBitMap.CreateCompatibleBitmap(dcObj, w, h) + + cDC.SelectObject(dataBitMap) + cDC.BitBlt((0, 0), (w, h), dcObj, (left, top), win32con.SRCCOPY) + # dataBitMap.SaveBitmapFile(cDC, bmpfilenamename) + signedIntsArray = dataBitMap.GetBitmapBits(True) + img = np.fromstring(signedIntsArray, dtype="uint8") + img.shape = (h, w, 4) + + # Free Resources + dcObj.DeleteDC() + cDC.DeleteDC() + win32gui.ReleaseDC(hwnd, wDC) + win32gui.DeleteObject(dataBitMap.GetHandle()) + return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) + def mouse_down(x, y): win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0) + def mouse_move(dx, dy): win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, dx, dy, 0, 0) + def mouse_up(x, y): win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0) + def mouse_click(x, y): mouse_down(x, y) mouse_up(x, y) + def match_img(img, target, type=cv2.TM_CCOEFF): h, w = target.shape[:2] res = cv2.matchTemplate(img, target, type) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) - return (*max_loc, max_loc[0] + w, max_loc[1] + h, max_loc[0] + w // 2, max_loc[1] + h // 2) + return ( + *max_loc, + max_loc[0] + w, + max_loc[1] + h, + max_loc[0] + w // 2, + max_loc[1] + h // 2, + ) + def list_add(li, num): if isinstance(num, int) or isinstance(num, float): - return [x+num for x in li] + return [x + num for x in li] elif isinstance(num, list) or isinstance(num, tuple): - return [x+y for x,y in zip(li,num)] + return [x + y for x, y in zip(li, num)] + def psnr(img1, img2): - mse = np.mean( (img1/255. - img2/255.) ** 2 ) - if mse < 1.0e-10: - return 100 - PIXEL_MAX = 1 - return 20 * np.log10(PIXEL_MAX / np.sqrt(mse)) - -def distance(x1,y1,x2,y2): - return np.sqrt(np.square(x1-x2)+np.square(y1-y2)) \ No newline at end of file + mse = np.mean((img1 / 255.0 - img2 / 255.0) ** 2) + if mse < 1.0e-10: + return 100 + PIXEL_MAX = 1 + return 20 * np.log10(PIXEL_MAX / np.sqrt(mse)) + + +def distance(x1, y1, x2, y2): + return np.sqrt(np.square(x1 - x2) + np.square(y1 - y2))