-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathauto_aim_prov2.py
171 lines (144 loc) · 5.45 KB
/
auto_aim_prov2.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
import signal
import threading
import warnings
from multiprocessing import Pipe, Process
import cv2
import numpy as np
import pynput
import torch
import win32con
import win32gui
from auto_scripts.grabscreen import grab_screen
from auto_scripts.configs import *
# 创建一个命名窗口
from auto_scripts.get_model import load_model_infos
# loadConfig
from auto_scripts.mouse.mouse import mouse_down, mouse_up
from auto_scripts.mouse_controller import lock
# 消除警告信息
from utils.augmentations import letterbox
from utils.general import non_max_suppression, scale_coords, xyxy2xywh
from utils.plots import Annotator, colors
warnings.filterwarnings('ignore')
# loadModel
model, device, half = load_model_infos()
# 获取模型其他参
stride = int(model.stride.max()) # model stride
names = model.module.names if hasattr(model, 'module') else model.names # get class names
# 锁开关
LOCK_MOUSE = False
# 鼠标控制
mouse = pynput.mouse.Controller()
# 点击监听
def on_click(x, y, button, pressed):
global LOCK_MOUSE
if pressed and button == button.x2:
LOCK_MOUSE = not LOCK_MOUSE
print('LOCK_MOUSE', LOCK_MOUSE)
def img_init(p1):
print('进程 img_init 启动 ...')
while True:
# 获取指定位置
img0 = grab_screen(region=(0, 0, GAME_X, GAME_Y))
# 将图片缩小指定大小
img0 = cv2.resize(img0, (SCREEN_WIDTH, SCREEN_HEIGHT))
# Padded resize
img = letterbox(img0, IMGSZ, stride=stride)[0]
# Convert
img = img.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB
img = np.ascontiguousarray(img)
img = torch.from_numpy(img).to(device)
img = img.half() if half else img.float()
# 归一化处理
img = img / 255.
if len(img.shape) == 3:
img = img[None] # expand for batch dim
pred = model(img, augment=False, visualize=False)[0]
# NMS
pred = non_max_suppression(pred, CONF_THRES, IOU_THRES, None, False, max_det=1000)
aims = []
for i, det in enumerate(pred):
s = ''
s += '%gx%g ' % img.shape[2:] # print string
gn = torch.tensor(img0.shape)[[1, 0, 1, 0]] # normalization gain whwh
# 设置方框绘制
annotator = Annotator(img0, line_width=LINE_THICKNESS, example=str(names))
if len(det):
# Rescale boxes from img_size to im0 size
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img0.shape).round()
# Write results
for *xyxy, conf, cls in reversed(det):
# 获取类别索引
c = int(cls) # integer class
# bbox 中的坐标
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
line = (c, *xywh) # label format
aims.append(line)
# 图片绘制
label = SHOW_LABEL and names[c] or None
annotator.box_label(xyxy, label, color=colors(c, True))
p1.send((img0, aims))
def img_show(c1, p2):
print('进程 img_show 启动 ...')
show_up = False
show_tips = True
signal.signal(signal.SIGINT, lambda x, y: sys.exit(0))
while True:
# 展示窗口
img0, aims = c1.recv()
p2.send(aims)
if show_tips:
print('传输坐标中 ...')
if SHOW_IMG:
cv2.namedWindow(SCREEN_NAME, cv2.WINDOW_NORMAL)
# 重设窗口大小
cv2.resizeWindow(SCREEN_NAME, RESIZE_X, RESIZE_Y)
cv2.imshow(SCREEN_NAME, img0)
if not show_up:
hwnd = win32gui.FindWindow(None, SCREEN_NAME)
win32gui.ShowWindow(hwnd, win32con.SW_SHOWNORMAL)
win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0, 0, 0, 0,
win32con.SWP_NOMOVE | win32con.SWP_NOACTIVATE | win32con.SWP_NOOWNERZORDER |
win32con.SWP_SHOWWINDOW | win32con.SWP_NOSIZE)
show_up = not show_up
k = cv2.waitKey(1)
if k % 256 == 27:
cv2.destroyAllWindows()
p2.send('exit')
exit('结束 img_show 进程中 ...')
if show_tips:
show_tips = False
def get_bbox(c2):
global LOCK_MOUSE
print('进程 get_bbox 启动 ...')
# ...or, in a non-blocking fashion:
listener = pynput.mouse.Listener(on_click=on_click)
listener.start()
while True:
aims = c2.recv()
if isinstance(aims, str):
exit('结束 get_bbox 进程中 ...')
else:
if aims and LOCK_MOUSE:
p = threading.Thread(target=lock, args=(aims, mouse, GAME_X, GAME_Y), kwargs={'logitech': True, 'model_type': None})
p.start()
p.join()
if __name__ == '__main__':
# 父进程创建Queue,并传给各个子进程:
p1, c1 = Pipe()
p2, c2 = Pipe()
reader1 = Process(target=get_bbox, args=(c2,))
reader2 = Process(target=img_show, args=(c1, p2))
writer = Process(target=img_init, args=(p1,))
# 启动子进程 reader,读取:
reader1.start()
reader2.start()
# 启动子进程 writer,写入:
writer.start()
# 等待 reader 结束:
reader1.join()
reader2.join()
# 等待 writer 结束:
writer.terminate()
writer.join()
exit('结束 img_init 进程中 ...')