Skip to content

Commit

Permalink
pyhook free
Browse files Browse the repository at this point in the history
  • Loading branch information
IrisRainbowNeko committed Sep 22, 2021
1 parent 0de1a1d commit 8992360
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 50 deletions.
31 changes: 2 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,7 @@
conda create -n ysfish python=3.6
conda activate ysfish
```
安装<font color=#66CCFF>**python3.7或以下**</font>版本,否则pyHook要自己编译。

## pyHook安装
安装PyUserInput前需要安装<font color=#66CCFF>**pyHook**</font>适用于win10的版本可以在 [这里](https://www.lfd.uci.edu/~gohlke/pythonlibs/) 找到。

适用于python3.6的文件为:\
64位:[pyHook‑1.5.1‑cp36‑cp36m‑win_amd64.whl](https://download.lfd.uci.edu/pythonlibs/y2rycu7g/cp36/pyHook-1.5.1-cp36-cp36m-win_amd64.whl) \
32位:[pyHook‑1.5.1‑cp36‑cp36m‑win32.whl](https://download.lfd.uci.edu/pythonlibs/y2rycu7g/cp36/pyHook-1.5.1-cp36-cp36m-win32.whl)

下载文件后将命令行切换到文件所在目录,执行:
```shell
pip install [文件名].whl
```
例如在64位的python3.6下安装:
```shell
pip install pyHook-1.5.1-cp36-cp36m-win_amd64.whl
```
推荐安装<font color=#66CCFF>**python3.7或以下**</font>版本。

## 下载工程代码
使用git下载,[git安装教程](https://www.cnblogs.com/xiaoliu66/p/9404963.html):
Expand All @@ -46,7 +30,7 @@ cd genshin_auto_fish
```
执行以下命令安装依赖:
```shell
pip install -U pip
python -m pip install -U pip
python requirements.py
```
如果要使用显卡进行加速需要 [安装CUDA和cudnn](https://zhuanlan.zhihu.com/p/94220564?utm_source=wechat_session&ivk_sa=1024320u) 安装后无视上面的命令用下面这条安装gpu版:
Expand All @@ -68,17 +52,6 @@ python setup.py develop
下载预训练[权重](https://github.com/7eu7d7/genshin_auto_fish/releases/tag/weights) (.pth文件),
下载后将权重文件放在 <font color=#66CCFF>**工程目录/weights**</font> 下

## 可能的问题
如果后续运行出现<font color=red>ModuleNotFoundError: No module named 'windows'</font>\
[anaconda安装路径]/envs/ysfish/Lib/site-packages/pymouse/\_\_init\_\_.py中的
```python
from windows import PyMouse, PyMouseEvent
```
改为
```python
from .windows import PyMouse, PyMouseEvent
```

# YOLOX训练工作流程
YOLOX部分因为打标签太累所以用半监督学习。标注少量样本后训练模型生成其余样本伪标签再人工修正,不断迭代提高精度。
样本量较少所以使用迁移学习,在COCO预训练的模型上进行fine-tuning.
Expand Down
25 changes: 9 additions & 16 deletions fisher/environment.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import numpy as np
from utils import *
import cv2
from pymouse import *
import pyautogui
import win32api, win32con
import time
from copy import deepcopy
from collections import Counter
Expand All @@ -23,6 +21,7 @@ def __init__(self, predictor, show_det=True):
self.dist_dict={'hua jiang':130, 'ji yu':80, 'die yu':80, 'jia long':80, 'pao yu':80}
self.food_rgn=[580,400,740,220]
self.last_fish_type='hua jiang'
#self.last_fish_type='die yu' # 钓雷鸣仙
self.show_det=show_det
os.makedirs('img_tmp/', exist_ok=True)

Expand All @@ -32,22 +31,18 @@ def get_fish_types(self, n=12, rate=0.6):
for i in range(n):
obj_list = self.predictor.image_det(cap())
if obj_list is None:
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, 70 * fx(i), 0, 0, 0)
mouse_move(70 * fx(i), 0)
time.sleep(0.1)
continue
cls_list = set([x[0] for x in obj_list])
counter.update(cls_list)
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, 70 * fx(i), 0, 0, 0)
mouse_move(70 * fx(i), 0)
time.sleep(0.2)
# pyautogui.moveRel(50, 0, duration=0.5)
# for u in range(1,51):
# mosue.move(sx + u, sy)
# time.sleep(0.003)
fish_list = [k for k, v in dict(counter).items() if v / n >= rate]
return fish_list

def throw_rod(self, fish_type):
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0)
mouse_down(960, 540)
time.sleep(1)

def move_func(dist):
Expand All @@ -64,7 +59,7 @@ def move_func(dist):

rod_info = sorted(list(filter(lambda x: x[0] == 'rod', obj_list)), key=lambda x: x[1], reverse=True)
if len(rod_info)<=0:
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, np.random.randint(-50,50), np.random.randint(-50,50), 0, 0)
mouse_move(np.random.randint(-50,50), np.random.randint(-50,50))
time.sleep(0.1)
continue
rod_info=rod_info[0]
Expand All @@ -86,19 +81,18 @@ def move_func(dist):

dx = int(move_func(x_dist))
dy = int(move_func(((fish_info[2][3]) + fish_info[2][1]) / 2 - rod_info[2][3]))
#win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, fish_info[2][2] - rod_info[2][2] + 50, (fish_info[2][3] + fish_info[2][1]) / 2 - rod_info[2][3], 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, dx, dy, 0, 0)
mouse_move(dx, dy)
except Exception as e:
traceback.print_exc()
#time.sleep(0.3)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0)
mouse_up(960, 540)

def select_food(self, fish_type):
pyautogui.press('f')
time.sleep(1)
pyautogui.click(1650, 790, button=pyautogui.SECONDARY)
time.sleep(0.5)
bbox_food = match_img(cap(self.food_rgn), self.food_imgs[self.ff_dict[fish_type]], type=cv2.TM_CCOEFF_NORMED)
bbox_food = match_img(cap(self.food_rgn), self.food_imgs[self.ff_dict[fish_type]], type=cv2.TM_CCORR_NORMED)
pyautogui.click(bbox_food[4]+self.food_rgn[0], bbox_food[5]+self.food_rgn[1])
time.sleep(0.1)
pyautogui.click(1183, 756)
Expand All @@ -113,7 +107,6 @@ def do_fish(self, fish_init=True):

class Fishing:
def __init__(self, delay=0.1, max_step=100, show_det=True, predictor=None):
self.mosue = PyMouse()
self.t_l = cv2.imread('./imgs/target_left.png')
self.t_r = cv2.imread('./imgs/target_right.png')
self.t_n = cv2.imread('./imgs/target_now.png')
Expand Down Expand Up @@ -141,7 +134,7 @@ def reset(self):
return self.get_state()

def drag(self):
self.mosue.click(1630,995)
mouse_click(1630,995)

def do_action(self, action):
if action==1:
Expand Down
2 changes: 1 addition & 1 deletion fishing.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def start_fishing(predictor, agent, bite_timeout=20):
state, reward, done = env.step(action)
if done:
break
time.sleep(1)
time.sleep(3)

#python fishing.py image -f yolox/exp/yolox_tiny_fish.py -c weights/best_tiny3.pth --conf 0.25 --nms 0.45 --tsize 640 --device gpu
if __name__ == "__main__":
Expand Down
8 changes: 4 additions & 4 deletions requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
loguru
matplotlib
tabulate
PyUserInput
tqdm
pywin32
PyAutoGUI
Expand All @@ -21,10 +20,11 @@
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
--no-deps torchvision==0.8.1+{"cpu" if args.cuda is None else "cu" + args.cuda} -f https://download.pytorch.org/whl/torch_stable.html
thop
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'):
pip.main(['install', *line.split()])
if len(line)>0:
pip.main(['install', *line.split()])
14 changes: 14 additions & 0 deletions utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import cv2
import pyautogui
import numpy as np
import win32api, win32con

def cap(region=None):
img = pyautogui.screenshot(region=region) if region else pyautogui.screenshot()
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)
Expand Down

0 comments on commit 8992360

Please sign in to comment.