Skip to content

Commit b21a760

Browse files
committed
feat: todo 驱动/硬件级的键鼠模拟 参考按键精灵
1 parent 91d89fb commit b21a760

33 files changed

+1110
-102
lines changed

best.pt

13.8 MB
Binary file not shown.

datasets_utils.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import cv2 as cv
21
import os
32
import shutil
43

direction_move.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import time
2-
from directkeys import PressKey, ReleaseKey, key_down, key_up
2+
3+
from directkeys import PressKey, ReleaseKey
34

45
direct_dic = {"UP": 0xC8, "DOWN": 0xD0, "LEFT": 0xCB, "RIGHT": 0xCD}
56

directkeys.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# coding=utf-8
2-
import win32con
32
import win32api
4-
import time
3+
import win32con
54

65
key_map = {
76
"0": 49, "1": 50, "2": 51, "3": 52, "4": 53, "5": 54, "6": 55, "7": 56, "8": 57, "9": 58,

getkeys.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import win32api as wapi
2-
import time
32

43
dict = {"A": 0, "S": 1,"D": 2, "F": 3, "G": 4, "H": 5, "Q": 6, "W": 7, "E": 8, "R": 9, "T": 10, "Y": 11, "up": 12,
54
"down": 13, "left": 14, "right":15, "ctrl": 16, "alt": 17, "Z":18, "X":19, "C": 20, "esc": 21, "f2": 22,

grabscreen.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
55
@author: analoganddigital ( GitHub )
66
"""
7-
import cv2
87
import numpy as np
9-
import win32gui, win32ui, win32con, win32api
8+
import win32api
9+
import win32con
10+
import win32gui
11+
import win32ui
12+
1013

1114
def grab_screen(region=None):
1215

image_grab.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
@author: analoganddigital ( GitHub )
66
"""
77

8-
import numpy as np
9-
from PIL import ImageGrab
10-
import cv2
8+
import os
119
import time
12-
import directkeys
13-
import grabscreen
10+
11+
import cv2
12+
1413
import getkeys
15-
import os
14+
import grabscreen
1615

1716
wait_time = 5
1817
L_t = 3

main2.py

+59-48
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
import numpy as np
2-
from grabscreen import grab_screen
3-
import cv2
1+
import random
42
import time
5-
import directkeys
3+
4+
import cv2
5+
import numpy as np
66
import torch
7-
from directkeys import PressKey, ReleaseKey, key_down, key_up
7+
8+
import directkeys
9+
from direction_move import move
10+
from directkeys import ReleaseKey
811
from getkeys import key_check
9-
from utils.general import (
10-
check_img_size, non_max_suppression, apply_classifier, scale_coords,
11-
xyxy2xywh, xywh2xyxy, strip_optimizer, set_logging, plot_one_box)
12+
from grabscreen import grab_screen
1213
from models.experimental import attempt_load
13-
from direction_move import move
14-
from small_recgonize import current_door, next_door
15-
from skill_recgnize import skill_rec
16-
import random
14+
# from skill_recgnize import skill_rec
15+
from small_recgonize import current_door
16+
from utils.general import (
17+
non_max_suppression, scale_coords,
18+
xyxy2xywh)
1719

1820

1921
def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=False, scaleFill=False, scaleup=True):
@@ -53,7 +55,6 @@ def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=False, scal
5355
weights = r'resource\best.pt' # yolo5 模型存放的位置
5456
device = torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")
5557
model = attempt_load(weights, map_location=device) # load FP32 model
56-
dir(model)
5758
window_size = (0, 0, 1280, 800) # 截屏的位置
5859
img_size = 640 # 输入到yolo5中的模型尺寸
5960
paused = False
@@ -78,10 +79,19 @@ def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=False, scal
7879
next_door_time = -20
7980
fs = 1 # 每四帧处理一次
8081

81-
# # 倒计时
82-
# for i in list(range(5))[::-1]:
83-
# print(i + 1)
84-
# time.sleep(1)
82+
83+
def plot_one_box(x, img, color=None, label=None, line_thickness=None):
84+
# Plots one bounding box on image img
85+
tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness
86+
color = color or [random.randint(0, 255) for _ in range(3)]
87+
c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
88+
cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
89+
if label:
90+
tf = max(tl - 1, 1) # font thickness
91+
t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
92+
c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
93+
cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA) # filled
94+
cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)
8595

8696

8797
# 捕捉画面+目标检测+玩游戏
@@ -91,29 +101,26 @@ def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=False, scal
91101
img0 = grab_screen(window_size)
92102
frame += 1
93103
if frame % fs == 0:
94-
# img0 = cv2.imread("test/shiwu.jpg")
95104

96105
img = cv2.cvtColor(img0, cv2.COLOR_BGRA2BGR)
97106

98107
# Padded resize
99108
# img = letterbox(img0, new_shape=img_size)[0]
100109

101-
102110
# Convert
103111
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB
104112
img = np.ascontiguousarray(img)
105113

106114
img = torch.from_numpy(img).to(device).unsqueeze(dim=0)
107-
# img = img.half() if half else img.float() # uint8 to fp16/32
108-
# img /= 255.0 # 0 - 255 to 0.0 - 1.0
115+
img = img.half() if half else img.float() # uint8 to fp16/32
116+
img /= 255.0 # 0 - 255 to 0.0 - 1.0
109117

110-
111-
pred = model(img.to(torch.float32), augment=False)[0]
118+
pred = model(img, augment=False)[0]
112119

113120
# Apply NMS
114121
det = non_max_suppression(pred, conf_thres, iou_thres, classes=classes, agnostic=agnostic_nms)
115122
gn = torch.tensor(img0.shape)[[1, 0, 1, 0]]
116-
det = det[0] # 所有的检测到的目标
123+
det = det[0] # 所有的检测到的目标wqewq
117124

118125
if det is not None and len(det):
119126
# Rescale boxes from img_size to im0 size
@@ -135,7 +142,7 @@ def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=False, scal
135142
# with open(txt_path + '.txt', 'a') as f:
136143
# f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format
137144

138-
#转换xywh形式,方便计算距离
145+
# 转换xywh形式,方便计算距离
139146
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4))).view(-1).tolist()
140147
cls = int(cls)
141148
img_object.append(xywh)
@@ -185,30 +192,31 @@ def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=False, scal
185192
monster_box = box
186193
monster_index = idx
187194
min_distance = dis
195+
# 处于攻击距离
188196
if abs(hero_xywh[0] - monster_box[0]) < attx and abs(hero_xywh[1] - monster_box[1]) < atty:
189-
if "BOSS" in cls_object:
190-
directkeys.key_press("R")
191-
directkeys.key_press("Q")
192-
# time.sleep(0.5)
193-
skill_name = skill_char[int(np.random.randint(len(skill_char), size=1)[0])]
194-
while True:
195-
if skill_rec(skill_name, img0):
196-
directkeys.key_press(skill_name)
197-
directkeys.key_press(skill_name)
198-
directkeys.key_press(skill_name)
199-
break
200-
else:
201-
skill_name = skill_char[int(np.random.randint(len(skill_char), size=1)[0])]
202-
else:
203-
skill_name = skill_char[int(np.random.randint(len(skill_char), size=1)[0])]
204-
while True:
205-
if skill_rec(skill_name, img0):
206-
directkeys.key_press(skill_name)
207-
directkeys.key_press(skill_name)
208-
directkeys.key_press(skill_name)
209-
break
210-
else:
211-
skill_name = skill_char[int(np.random.randint(len(skill_char), size=1)[0])]
197+
directkeys.key_press("A")
198+
# if "BOSS" in cls_object:
199+
# directkeys.key_press("A")
200+
#
201+
# skill_name = skill_char[int(np.random.randint(len(skill_char), size=1)[0])]
202+
# while True:
203+
# if skill_rec(skill_name, img0):
204+
# directkeys.key_press(skill_name)
205+
# directkeys.key_press(skill_name)
206+
# directkeys.key_press(skill_name)
207+
# break
208+
# else:
209+
# skill_name = skill_char[int(np.random.randint(len(skill_char), size=1)[0])]
210+
# else:
211+
# skill_name = skill_char[int(np.random.randint(len(skill_char), size=1)[0])]
212+
# while True:
213+
# if skill_rec(skill_name, img0):
214+
# directkeys.key_press(skill_name)
215+
# directkeys.key_press(skill_name)
216+
# directkeys.key_press(skill_name)
217+
# break
218+
# else:
219+
# skill_name = skill_char[int(np.random.randint(len(skill_char), size=1)[0])]
212220
print("释放技能攻击")
213221
if not action_cache:
214222
pass
@@ -220,12 +228,15 @@ def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=False, scal
220228
ReleaseKey(direct_dic[action_cache])
221229
action_cache = None
222230
# break
231+
# 怪物在英雄右上 , 左上 左下 右下
223232
elif monster_box[1] - hero_xywh[1] < 0 and monster_box[0] - hero_xywh[0] > 0:
233+
# y方向 小于攻击距离
224234
if abs(monster_box[1] - hero_xywh[1]) < thy:
225235
action_cache = move(direct="RIGHT", material=True, action_cache=action_cache,
226236
press_delay=press_delay,
227237
release_delay=release_delay)
228238
# break
239+
#
229240
elif hero_xywh[1] - monster_box[1] < monster_box[0] - hero_xywh[0]:
230241
action_cache = move(direct="RIGHT_UP", material=True, action_cache=action_cache,
231242
press_delay=press_delay,

models/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# This file contains modules common to various models
22
import math
3-
import warnings
43

54
import torch
65
import torch.nn as nn
76
from torch.nn import functional as F
7+
88
from utils.general import non_max_suppression
99

1010

models/experimental.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import numpy as np
44
import torch
55
import torch.nn as nn
6+
from utils.google_utils import attempt_download
67

78
from models.common import Conv, DWConv
8-
from utils.google_utils import attempt_download
99

1010

1111
class CrossConv(nn.Module):

opencv/basic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import numpy as np
21
import cv2
2+
import numpy as np
33

44
# -----------蓝色通道值--------------
55
blue = np.zeros((300, 300, 3), dtype=np.uint8)

resource/best.pt

-55.8 MB
Binary file not shown.

skill_recgnize.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import cv2 as cv
2-
import numpy as np
2+
33

44
def score(img):
55
counter = 0

test/DNF.jpg

324 KB
Loading

test/DNF.png

-217 KB
Binary file not shown.

test/best.pt

13.8 MB
Binary file not shown.

test/cv2_test.py

-9
This file was deleted.

0 commit comments

Comments
 (0)