1
- import numpy as np
2
- from grabscreen import grab_screen
3
- import cv2
1
+ import random
4
2
import time
5
- import directkeys
3
+
4
+ import cv2
5
+ import numpy as np
6
6
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
8
11
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
12
13
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 )
17
19
18
20
19
21
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
53
55
weights = r'resource\best.pt' # yolo5 模型存放的位置
54
56
device = torch .device ("cuda:0" ) if torch .cuda .is_available () else torch .device ("cpu" )
55
57
model = attempt_load (weights , map_location = device ) # load FP32 model
56
- dir (model )
57
58
window_size = (0 , 0 , 1280 , 800 ) # 截屏的位置
58
59
img_size = 640 # 输入到yolo5中的模型尺寸
59
60
paused = False
@@ -78,10 +79,19 @@ def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=False, scal
78
79
next_door_time = - 20
79
80
fs = 1 # 每四帧处理一次
80
81
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 )
85
95
86
96
87
97
# 捕捉画面+目标检测+玩游戏
@@ -91,29 +101,26 @@ def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=False, scal
91
101
img0 = grab_screen (window_size )
92
102
frame += 1
93
103
if frame % fs == 0 :
94
- # img0 = cv2.imread("test/shiwu.jpg")
95
104
96
105
img = cv2 .cvtColor (img0 , cv2 .COLOR_BGRA2BGR )
97
106
98
107
# Padded resize
99
108
# img = letterbox(img0, new_shape=img_size)[0]
100
109
101
-
102
110
# Convert
103
111
img = img [:, :, ::- 1 ].transpose (2 , 0 , 1 ) # BGR to RGB
104
112
img = np .ascontiguousarray (img )
105
113
106
114
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
109
117
110
-
111
- pred = model (img .to (torch .float32 ), augment = False )[0 ]
118
+ pred = model (img , augment = False )[0 ]
112
119
113
120
# Apply NMS
114
121
det = non_max_suppression (pred , conf_thres , iou_thres , classes = classes , agnostic = agnostic_nms )
115
122
gn = torch .tensor (img0 .shape )[[1 , 0 , 1 , 0 ]]
116
- det = det [0 ] # 所有的检测到的目标
123
+ det = det [0 ] # 所有的检测到的目标wqewq
117
124
118
125
if det is not None and len (det ):
119
126
# 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
135
142
# with open(txt_path + '.txt', 'a') as f:
136
143
# f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format
137
144
138
- #转换xywh形式,方便计算距离
145
+ # 转换xywh形式,方便计算距离
139
146
xywh = (xyxy2xywh (torch .tensor (xyxy ).view (1 , 4 ))).view (- 1 ).tolist ()
140
147
cls = int (cls )
141
148
img_object .append (xywh )
@@ -185,30 +192,31 @@ def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=False, scal
185
192
monster_box = box
186
193
monster_index = idx
187
194
min_distance = dis
195
+ # 处于攻击距离
188
196
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])]
212
220
print ("释放技能攻击" )
213
221
if not action_cache :
214
222
pass
@@ -220,12 +228,15 @@ def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=False, scal
220
228
ReleaseKey (direct_dic [action_cache ])
221
229
action_cache = None
222
230
# break
231
+ # 怪物在英雄右上 , 左上 左下 右下
223
232
elif monster_box [1 ] - hero_xywh [1 ] < 0 and monster_box [0 ] - hero_xywh [0 ] > 0 :
233
+ # y方向 小于攻击距离
224
234
if abs (monster_box [1 ] - hero_xywh [1 ]) < thy :
225
235
action_cache = move (direct = "RIGHT" , material = True , action_cache = action_cache ,
226
236
press_delay = press_delay ,
227
237
release_delay = release_delay )
228
238
# break
239
+ #
229
240
elif hero_xywh [1 ] - monster_box [1 ] < monster_box [0 ] - hero_xywh [0 ]:
230
241
action_cache = move (direct = "RIGHT_UP" , material = True , action_cache = action_cache ,
231
242
press_delay = press_delay ,
0 commit comments