forked from GrayTempest-400/warthunder-cv2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyolov8FPSGame.py
354 lines (299 loc) · 12.7 KB
/
yolov8FPSGame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import ctypes
import multiprocessing
import time
from multiprocessing import Process
import cv2
import pynput
from pynput.mouse import Button
from pynput.keyboard import Key, Listener
from win32gui import FindWindow, SetWindowPos, GetWindowText, GetForegroundWindow
from win32con import HWND_TOPMOST, SWP_NOMOVE, SWP_NOSIZE
import winsound
from simple_pid import PID
import math
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image, ImageGrab
import time
from ultralytics import YOLO
import pyautogui
import math
from tool import detect,convert
import pydirectinput as direct
ads = 'ads'
pidc = 'pidc'
size = 'size'
stop = 'stop'
lock = 'lock'
show = 'show'
head = 'head'
left = 'left'
title = 'title'
debug = 'debug'
region = 'region'
center = 'center'
radius = 'radius'
weights = 'weights'
classes = 'classes'
confidence = 'confidence'
fov = 'fov'
horizontal = 'horizontal'
sensitive = 'sensitive'
sensitivity = 'sensitivity'
vertical = 'vertical'
init = {
fov: 110, # 游戏内的 FOV
horizontal: 16420, # 游戏内以鼠标灵敏度为1测得的水平旋转360°对应的鼠标移动距离, 多次测量验证. 经过测试该值与FOV无关. 移动像素理论上等于该值除以鼠标灵敏度Horizontal Vertical
vertical: 7710 * 2, # 垂直, 注意垂直只能测一半, 即180°范围, 所以结果需要翻倍
sensitivity: 2, # 当前游戏鼠标灵敏度
title: '', # 可在后台运行 print(GetWindowText(GetForegroundWindow())) 来检测前台游戏窗体标题
weights: 'yolov8n.pt',
classes: 0.0, # 要检测的标签的序号(标签序号从0开始), 多个时如右 [0, 1]
confidence: 0.3, # 置信度, 低于该值的认为是干扰
size: 400, # 截图的尺寸, 屏幕中心 size*size 大小
radius: 400, # 瞄准生效半径, 目标瞄点出现在以准星为圆心该值为半径的圆的范围内时才会锁定目标
ads: 1.2, # 移动倍数, 调整方式: 瞄准目标旁边并按住 Shift 键, 当准星移动到目标点的过程, 稳定精准快速不振荡时, 就找到了合适的 ADS 值
center: None, # 屏幕中心点
region: None, # 截图范围
stop: False, # 退出, End
lock: False, # 锁定, Shift, 按左键时不锁(否则扔雷时也会锁)
show: True, # 显示, Down
head: False, # 瞄头, Up
pidc: True, # 是否启用 PID Controller, 还未完善, Left
left: True, # 左键锁, Right, 按鼠标左键时锁
debug: False, # Debug 模式, 用来调试 PID 值
}
def Detector(img,aim_name):
model = YOLO(weights)
results = model(img, verbose=False)
for result in results:
boxes_xywh = list(result.boxes.xywh.cpu().numpy().tolist())
cls = result.boxes.cls.cpu().numpy().tolist()
conf = result.boxes.conf.cpu().numpy().tolist()
if cls == [aim_name]:
return cls,conf,boxes_xywh
def oc():
ac, _ = data[center]
return ac / math.tan((data[fov] / 2 * math.pi / 180))
def rx(x):
angle = math.atan(x / oc()) * 180 / math.pi
return int(angle * data[horizontal] / data[sensitivity] / 360)
def ry(y):
angle = math.atan(y / oc()) * 180 / math.pi
return int(angle * data[vertical] / data[sensitivity] / 360)
def game():
return init[title] == GetWindowText(GetForegroundWindow())
def mouse(data):
def down(x, y, button, pressed):
if not game():
return
if button == Button.left and data[left]:
data[lock] = pressed
with pynput.mouse.Listener(on_click=down) as m:
m.join()
def keyboard(data):
def press(key):
if not game():
return
if key == Key.shift:
data[lock] = True
def release(key):
if key == Key.end:
# 结束程序
data[stop] = True
winsound.Beep(400, 200)
return False
if not game():
return
if key == Key.shift:
data[lock] = False
elif key == Key.up:
data[head] = not data[head]
winsound.Beep(800 if data[head] else 400, 200)
elif key == Key.down:
data[show] = not data[show]
winsound.Beep(800 if data[show] else 400, 200)
elif key == Key.left:
data[pidc] = not data[pidc]
winsound.Beep(800 if data[pidc] else 400, 200)
elif key == Key.right:
data[left] = not data[left]
winsound.Beep(800 if data[left] else 400, 200)
elif key == Key.page_down:
data[debug] = not data[debug]
winsound.Beep(800 if data[debug] else 400, 200)
with Listener(on_release=release, on_press=press) as k:
k.join()
def loop(data):
from toolkit import Capturer, Timer
from tool import detect,convert
capturer = Capturer(data[title], data[region])
winsound.Beep(800, 200)
try:
import os
root = os.path.abspath(os.path.dirname(__file__))
driver = ctypes.CDLL(f'{root}/logitech.driver.dll')
ok = driver.device_open() == 1
if not ok:
print('初始化失败, 未安装罗技驱动')
except FileNotFoundError:
print('初始化失败, 缺少文件')
def move(x: int, y: int):
if (x == 0) & (y == 0):
return
driver.moveR(x, y, True)
def inner(point):
"""
判断该点是否在准星的瞄准范围内
"""
a, b = data[center]
x, y = point
return (x - a) ** 2 + (y - b) ** 2 < data[radius] ** 2
def follow(aims):
"""
从 targets 里选目标瞄点距离准星最近的
"""
if len(aims) == 0:
return None
# 瞄点调整
targets = []
for index, clazz, conf, sc, gc, sr, gr in aims:
if conf < data[confidence]: # 特意把置信度过滤放到这里(便于从图片中查看所有识别到的目标的置信度)
continue
_, _, _, height = sr
sx, sy = sc
gx, gy = gc
differ = (height // 7) if data[head] else (height // 3)
newSc = sx, sy - height // 2 + differ # 屏幕坐标系下各目标的瞄点坐标, 计算身体和头在方框中的大概位置来获得瞄点, 没有采用头标签的方式(感觉效果特别差)
newGc = gx, gy - height // 2 + differ
targets.append((index, clazz, conf, newSc, newGc, sr, gr))
if len(targets) == 0:
return None
# 找到目标
cx, cy = data[center]
index = 0
minimum = 0
for i, item in enumerate(targets):
index, clazz, conf, sc, gc, sr, gr = item
sx, sy = sc
distance = (sx - cx) ** 2 + (sy - cy) ** 2
if minimum == 0:
index = i
minimum = distance
else:
if distance < minimum:
index = i
minimum = distance
return targets[index]
text = 'Realtime Screen Capture Detect'
pidx = PID(2, 0, 0.02, setpoint=0)
times, targets, distances = [], [], [] # 用于绘图
# 主循环
while True:
try:
if data[stop]:
break
# 生产数据
t1 = time.perf_counter_ns()
image = Capturer.backup(data[region]) # 如果句柄截图是黑色, 不能正常使用, 可以使用本行的截图方法
t2 = time.perf_counter_ns()
final_aims = detect(image) # 目标检测, 得到截图坐标系内识别到的目标和标注好的图片(无需展示图片时img为none)
t3 = time.perf_counter_ns()
aims = convert(final_aims=final_aims, region=data[region]) # 将截图坐标系转换为屏幕坐标系
# print(f'{Timer.cost(t3 - t1)}, {Timer.cost(t2 - t1)}, {Timer.cost(t3 - t2)}')
# 找到目标
target = follow(aims)
# 移动准星
if data[lock] and target:
index, clazz, conf, sc, gc, sr, gr = target
if inner(sc):
cx, cy = data[center]
sx, sy = sc
x = sx - cx
y = sy - cy
box = (sr[0], sr[1] - 100, sr[2] + sr[0], sr[3] + sr[1])
print(box)
# 分别敌我
img1 = ImageGrab.grab(box)
img1.save('screenshot.png')
img1 = cv2.imread('screenshot.png')
hsv = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV)
# define range of blue color in HSV
lower_blue = np.array([54, 83, 179])
upper_blue = np.array([54, 83, 179])
# define range of green color in HSV
lower_green = np.array([34, 255, 0])
upper_green = np.array([34, 255, 0])
# Threshold the HSV image to get only blue colors
mask_blue = cv2.inRange(hsv, lower_blue, upper_blue)
# Threshold the HSV image to get only green colors
mask_green = cv2.inRange(hsv, lower_green, upper_green)
if data[pidc] and cv2.countNonZero(mask_green) == 0 and cv2.countNonZero(mask_blue) == 0:
if data[debug]: # 用于绘图
times.append(time.time())
targets.append(0)
distances.append(x)
px = -int(pidx(x))
move(px, y)
elif cv2.countNonZero(mask_green) == 0 and cv2.countNonZero(mask_blue) == 0:
ax = int(x * data[ads])
ay = int(y * data[ads])
move(ax, ay)
else: # 用于绘图
if data[debug] and len(times) != 0:
try:
plt.plot(times, targets, label='target')
plt.plot(times, distances, label='distance')
plt.legend() # 图例
plt.xlabel('time')
plt.ylabel('distance')
times.clear()
targets.clear()
distances.clear()
matplotlib.use('TkAgg') # TkAgg, module://backend_interagg
winsound.Beep(600, 200)
plt.show()
except:
pass
# 显示检测
if data[show] and image is not None:
# 记录耗时
cv2.putText(image, f'{Timer.cost(t3 - t1)}', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 1)
cv2.putText(image, f'{Timer.cost(t2 - t1)}', (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 1)
cv2.putText(image, f'{Timer.cost(t3 - t2)}', (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 1)
# 瞄点划线
if target:
index, clazz, conf, sc, gc, sr, gr = target
cv2.circle(image, gc, 2, (0, 0, 0), 2)
r = data[size] // 2
cv2.line(image, gc, (r, r), (255, 255, 0), 2)
# 展示图片
cv2.namedWindow(text, cv2.WINDOW_AUTOSIZE)
cv2.imshow(text, image)
SetWindowPos(FindWindow(None, text), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE)
cv2.waitKey(1)
if not data[show]:
cv2.destroyAllWindows()
except:
pass
if __name__ == '__main__':
multiprocessing.freeze_support()
manager = multiprocessing.Manager()
data = manager.dict()
data.update(init)
# 初始化数据
from toolkit import Monitor
data[center] = Monitor.resolution.center()
c1, c2 = data[center]
data[region] = c1 - data[size] // 2, c2 - data[size] // 2, data[size], data[size]
# 创建进程
pm = Process(target=mouse, args=(data,), name='Mouse')
pk = Process(target=keyboard, args=(data,), name='Keyboard')
pl = Process(target=loop, args=(data,), name='Loop')
# 启动进程
pm.start()
pk.start()
pl.start()
pk.join() # 不写 join 的话, 使用 dict 的地方就会报错 conn = self._tls.connection, AttributeError: 'ForkAwareLocal' object has no attribute 'connection'
pm.terminate() # 鼠标进程无法主动监听到终止信号, 所以需强制结束