Skip to content

Commit

Permalink
Merge pull request IrisRainbowNeko#101 from wasabiegg/master
Browse files Browse the repository at this point in the history
add proxy option to requirements installation, change capture screen from pyautogui to winapi
  • Loading branch information
IrisRainbowNeko authored Oct 9, 2021
2 parents 65b8796 + 6d62834 commit 8be30c3
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 41 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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太卡。

Expand Down
6 changes: 6 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
windows:
monitor_width: 1920
monitor_height: 1080
game:
window_name: "原神"
100 changes: 72 additions & 28 deletions requirements.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,74 @@
import pip
# import pip
import subprocess
import sys
import argparse

parser = argparse.ArgumentParser(description='install requirements')
parser.add_argument('--cuda', default=None, type=str)
args = parser.parse_args()

pkgs=f'''
cython
scikit-image
loguru
matplotlib
tabulate
tqdm
pywin32
PyAutoGUI
opencv_python
keyboard
Pillow
pymouse
numpy==1.19.5
torch==1.7.0+{"cpu" if args.cuda is None else "cu" + args.cuda} -f https://download.pytorch.org/whl/torch_stable.html
torchvision==0.8.1+{"cpu" if args.cuda is None else "cu" + args.cuda} --no-deps -f https://download.pytorch.org/whl/torch_stable.html
thop --no-deps
git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI
'''

for line in pkgs.split('\n'):
if len(line)>0:
pip.main(['install', *line.split()])
# use python type hints to make code more readable
from typing import List, Optional


def pip_install(proxy: Optional[str], args: List[str]) -> None:
if proxy is None:
# pip.main(["install", f"--proxy={proxy}", *args])
subprocess.run(
[sys.executable, "-m", "pip", "install", *args],
capture_output=False,
check=True,
)
else:
subprocess.run(
[sys.executable, "-m", "pip", "install", f"--proxy={proxy}", *args],
capture_output=False,
check=True,
)


def main():
parser = argparse.ArgumentParser(description="install requirements")
parser.add_argument("--cuda", default=None, type=str)
parser.add_argument(
"--proxy",
default=None,
type=str,
help="specify http proxy, [http://127.0.0.1:1080]",
)
args = parser.parse_args()

pkgs = f"""
cython
scikit-image
loguru
matplotlib
tabulate
tqdm
pywin32
PyAutoGUI
PyYAML>=5.3.1
opencv_python
keyboard
Pillow
pymouse
numpy==1.19.5
torch==1.7.0+{"cpu" if args.cuda is None else "cu" + args.cuda} -f https://download.pytorch.org/whl/torch_stable.html
torchvision==0.8.1+{"cpu" if args.cuda is None else "cu" + args.cuda} --no-deps -f https://download.pytorch.org/whl/torch_stable.html
thop --no-deps
git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI
"""

for line in pkgs.split("\n"):
# handle multiple space in an empty line
line = line.strip()

if len(line) > 0:
# use pip's internal APIs in this way is deprecated. This will fail in a future version of pip.
# The most reliable approach, and the one that is fully supported, is to run pip in a subprocess.
# ref: https://pip.pypa.io/en/latest/user_guide/#using-pip-from-your-program
# pip.main(['install', *line.split()])

pip_install(args.proxy, line.split())

print("\nsuccessfully installed requirements!")


if __name__ == "__main__":
main()
89 changes: 76 additions & 13 deletions utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
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))

0 comments on commit 8be30c3

Please sign in to comment.