-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.py
404 lines (373 loc) · 17.8 KB
/
main.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import sys
import json
import time
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5 import QtCore, QtGui
from pynput import mouse
from pynput import keyboard
import win32api
import win32con
import win32gui
import win32com.client
import pyautogui
import threading
import numpy as np
from predict import Network
import random
import tkinter as tk
from tkinter import filedialog
from PIL import ImageTk
import pickle
from mousepath import wind_mouse
x_size = win32api.GetSystemMetrics(0)
y_size = win32api.GetSystemMetrics(1)
shell = win32com.client.Dispatch("WScript.Shell")
# Constants
DEBUG = False
NETWORK_INPUT = (400,300)
RIGHT_CLICK = 0
LEFT_CLICK = 1
SAVE_DIR = 'scripts\\'
PICKLE_SAVE = 'training.data'
EXTRA_SLEEP = 0.5
# For window and main loop
class ScreenOverlay(QMainWindow):
def __init__(self, config):
super().__init__()
# Config
self.config = config
self.mode = 'idle'
self.current_save_path = 'none'
# Capturing setup
self.last_click_time = None
self.new_recording()
# Screen setup
self.setWindowFlags(
QtCore.Qt.Window |
QtCore.Qt.FramelessWindowHint |
QtCore.Qt.WindowStaysOnTopHint |
QtCore.Qt.X11BypassWindowManagerHint
)
self.current_size = self.config['window_size']
self.left_corner = self.config['left_corner']
self.move(*self.left_corner)
self.right_corner = (self.left_corner[0] + self.current_size[0], self.left_corner[1] + self.current_size[1])
self.setMinimumSize(QtCore.QSize(self.current_size[0] + 1, self.current_size[1] + 1))
self.setAttribute(QtCore.Qt.WA_NoSystemBackground, True)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground, True)
# Network setup and thread init
self.mouse_predictor = Network(self.current_size, NETWORK_INPUT)
self.capture_on = True
keyboard_listener = keyboard.Listener(on_press=self.on_press, on_release=self.on_release)
keyboard_listener.start()
mouse_listener = mouse.Listener(on_click=self.on_click)
mouse_listener.start()
threading.Thread(target=self.background_thread, daemon=True).start()
find_window_then_resize_and_move(self.config['window_name'], self.left_corner, self.current_size)
def clear_last_capture(self):
self.last_click_time = None
self.last_click_location = None
self.last_screen_capture = None
def new_recording(self):
self.click_buffer = []
self.screen_cap_buffer = []
self.click_time_buffer = []
self.clear_last_capture()
def save_data(self):
with open(PICKLE_SAVE, 'wb') as savefile:
print('Saving data to', PICKLE_SAVE)
pickle.dump((self.click_buffer, self.screen_cap_buffer, self.click_time_buffer), savefile)
def load_data(self):
with open(PICKLE_SAVE, 'rb') as savefile:
print('Loading data from', PICKLE_SAVE)
clicks, screen_caps, click_times = pickle.load(savefile)
self.click_buffer += clicks
self.screen_cap_buffer += screen_caps
self.click_time_buffer += click_times
if DEBUG:
print(self.click_buffer)
print(self.click_time_buffer)
def record_moment(self, click, screen_cap):
start_time = time.time()
if self.last_click_time != None:
self.click_buffer.append(self.last_click_location)
self.screen_cap_buffer.append(self.last_screen_capture)
self.click_time_buffer.append(start_time - self.last_click_time)
self.last_click_time = start_time
self.last_click_location = click
self.last_screen_capture = screen_cap
def on_click(self, x, y, button, pressed):
if DEBUG:
print('{0} at {1}'.format('Pressed' if pressed else 'Released', (x, y)))
if self.in_capture_window(x, y):
if self.mode == 'record' or self.mode == 'record-control':
if pressed and (button == mouse.Button.left):# or button == mouse.Button.right):
click = self.full_to_window((x, y))#, RIGHT_CLICK if button == mouse.Button.right else LEFT_CLICK
cap = pyautogui.screenshot(region=(*self.left_corner, self.current_size[0], self.current_size[1]))
processed_capture = cap.resize(NETWORK_INPUT)
self.record_moment(click, processed_capture)
if not self.capture_on:
# Stop listener
return False
def on_press(self, key):
if DEBUG:
try:
print('alphanumeric key {0} pressed'.format(key.char))
except AttributeError:
print('special key {0} pressed'.format(key))
def on_release(self, key):
try:
key = '{0}'.format(key.char)
except AttributeError:
key = '{0}'.format(key)
key = key.split('.')[1]
if DEBUG:
print(key, 'released')
if key == self.config['close']:
print('Closing application')
self.capture_on = False
QApplication.quit()
return False
elif key == self.config['record']:
print('Record mode: ON')
self.mode = 'record'
self.clear_last_capture()
elif key == self.config['control']:
print('Control mode: ON')
self.mode = 'control'
elif key == self.config['idle']:
print('Recording/Controlling: OFF')
print('Idle: ON')
self.mode = 'idle'
elif key == self.config['record-control']:
print('Recording AND Controlling: ON')
self.mode = 'record-control'
self.clear_last_capture()
elif key == self.config['train']:
print('Starting training')
self.mode = 'training'
if len(self.screen_cap_buffer) == 0:
print('No data to train on')
else:
self.mouse_predictor.train(self.screen_cap_buffer, self.click_buffer, self.click_time_buffer)
elif key == self.config['load/save_network']:
if self.mode != 'idle':
print('You must be in idle mode to load or save a network, press', self.config['idle'], 'then try again')
else:
print('Would you like to load or save a network? (l or s)')
load_or_save = input('>')
while load_or_save != 'l' and load_or_save != 's':
print('Not l or s')
load_or_save = input('>')
if load_or_save == 'l':
root = tk.Tk()
root.withdraw()
file_path = filedialog.askdirectory(initialdir=SAVE_DIR, title='Select network folder')
self.mouse_predictor.load(file_path)
else:
if self.current_save_path == 'none':
print('Saving network - please input network name')
network_name = input('>')
while network_name == '':
network_name = input('>')
self.current_save_path = SAVE_DIR + network_name
else:
print('Saving network - file already exist at ' + self.current_save_path + ', would you like to overwrite it? (y,n)')
stdinput = input('>')
while stdinput != 'y' and stdinput != 'n':
print('Unknown response, type y or n')
stdinput = input('>')
if stdinput == 'y':
pass
else:
print('Please input new network name')
network_name = input('>')
while network_name == '':
network_name = input('>')
self.current_save_path = SAVE_DIR + network_name
print('Saving to', self.current_save_path)
self.mouse_predictor.save(self.current_save_path)
elif key == self.config['load/save_data']:
if self.mode != 'idle':
print('You must be in idle mode to load or save training data, press', self.config['idle'], 'then try again')
else:
print('Would you like to load or save training data? (l or s)')
load_or_save = input('>')
while load_or_save != 'l' and load_or_save != 's':
print('Not l or s')
load_or_save = input('>')
if load_or_save == 'l':
self.load_data()
else:
self.save_data()
elif key == self.config['new_network']:
if self.mode != 'idle':
print('You must be in idle mode to create a new network, press', self.config['idle'], 'then try again')
else:
print('Creating new network, wiping loaded weights')
self.current_save_path = 'none'
self.mouse_predictor.new_network()
elif key == self.config['clear_recording']:
if self.mode != 'idle':
print('You must be in idle mode to clear training data, press', self.config['idle'], 'then try again')
else:
print('Clearing data')
self.new_recording()
elif key == self.config['data_edit']:
print('Starting data edit mode')
print('Click in location to reset prediction for training')
print('Exit window or hit S to move to next record')
print('Hit D to delete record')
print('Hit SPACE to exit data edit mode')
print('Hit [ to decrease time until next click')
print('Hit ] to increase time until next click')
self.mode = 'data_edit'
self.update()
self.exit_edit_mode = False
self.index = 0
while self.index < len(self.screen_cap_buffer):
image = self.screen_cap_buffer[self.index]
root = tk.Tk()
root.geometry('%dx%d+%d+%d' % (NETWORK_INPUT[0] + 1, NETWORK_INPUT[1] + 1, self.left_corner[0], self.left_corner[1]))
canvas = tk.Canvas(root, width = NETWORK_INPUT[0], height = NETWORK_INPUT[1])
canvas.pack()
img = ImageTk.PhotoImage(image)
canvas.create_image((NETWORK_INPUT[0] // 2), (NETWORK_INPUT[1] // 2), image=img)
canvas.create_oval((self.click_buffer[self.index][0] / self.current_size[0]) * NETWORK_INPUT[0] - 3, (self.click_buffer[self.index][1] / self.current_size[1]) * NETWORK_INPUT[1] - 3, \
(self.click_buffer[self.index][0] / self.current_size[0]) * NETWORK_INPUT[0] + 3, (self.click_buffer[self.index][1] / self.current_size[1]) * NETWORK_INPUT[1] + 3)
def key_press(e):
if e.char == 's':
print('Moving to next record')
root.destroy()
if e.char == ' ':
print('Exiting data edit mode')
self.exit_edit_mode = True
root.destroy()
if e.char == 'd':
print('Deleting record')
del self.screen_cap_buffer[self.index]
del self.click_buffer[self.index]
del self.click_time_buffer[self.index]
self.index -= 1
root.destroy()
if e.char == '[':
self.click_time_buffer[self.index] -= 1
print('New time until next click', self.click_time_buffer[self.index])
if e.char == ']':
self.click_time_buffer[self.index] += 1
print('New time until next click', self.click_time_buffer[self.index])
def new_coords(e):
scaled_x = (e.x / NETWORK_INPUT[0]) * self.current_size[0]
scaled_y = (e.y / NETWORK_INPUT[1]) * self.current_size[1]
print('Click assigned to', (scaled_x, scaled_y))
self.click_buffer[self.index] = (scaled_x, scaled_y)
self.index -= 1
root.destroy()
root.bind('<Key>', key_press)
root.bind('<Button 1>', new_coords)
shell.SendKeys('%')
win32gui.SetForegroundWindow(root.winfo_id())
print('Time until next click:', self.click_time_buffer[self.index])
root.mainloop()
if self.exit_edit_mode:
break
self.index += 1
print('Idle mode: ON')
self.mode = 'idle'
elif key == self.config['help']:
self.print_help()
self.update()
def background_thread(self):
while self.capture_on:
if self.mode == 'control' or self.mode == 'record-control':
cap = pyautogui.screenshot(region=(*self.left_corner, *self.current_size))
processed_capture = cap.resize(NETWORK_INPUT)
location, sleep_time = self.mouse_predictor.predict(processed_capture)
print('Predicted location:', location)
print('Predicted sleep time:', sleep_time)
location_scaled = location[0] + self.left_corner[0], location[1] + self.left_corner[1]
# If windmouse is off, auto move mouse to predicted location then back to original location
if self.config['windmouse']:
wind_mouse(*pyautogui.position(), *location_scaled, move_mouse=self.move_mouse)
time.sleep((random.random() / 10) + 0.1)
pyautogui.click()
else:
current_pos = pyautogui.position()
self.move_mouse(*location_scaled)
time.sleep((random.random() / 10) + 0.02)
pyautogui.click(*location_scaled)
self.move_mouse(*current_pos, False)
time.sleep(sleep_time + EXTRA_SLEEP + random.random())
else:
time.sleep(0.1)
def move_mouse(self, x, y, box_movement = True):
if box_movement:
x_dest, y_dest = self.box_movement(x, y)
else:
x_dest, y_dest = x, y
x_conv = int(65535 * x_dest / x_size)
y_conv = int(65535 * y_dest / y_size)
win32api.mouse_event(win32con.MOUSEEVENTF_MOVE | win32con.MOUSEEVENTF_ABSOLUTE, x_conv, y_conv)
# Prevents predicted mouse movements from going outside the application area
def box_movement(self, x, y):
new_x = min(max(self.left_corner[0], x), self.right_corner[0])
new_y = min(max(self.left_corner[1], y), self.right_corner[1])
return (new_x, new_y)
# Returns true if inside capture window
def in_capture_window(self, x, y):
if x < self.left_corner[0] or x > self.right_corner[0]:
return False
if y < self.left_corner[1] or y > self.right_corner[1]:
return False
return True
# Converts from full screen position to window relative position
def full_to_window(self, position):
p0 = min(max(position[0] - self.left_corner[0], 0), self.current_size[0] - 1)
p1 = min(max(position[1] - self.left_corner[1], 0), self.current_size[1] - 1)
return (p0, p1)
# Paints application boarders
def paintEvent(self, e):
painter = QtGui.QPainter(self)
if self.mode == 'data_edit':
return
elif self.mode == 'record':
painter.setPen(QtCore.Qt.green)
elif self.mode == 'control' or self.mode == 'record-control':
painter.setPen(QtCore.Qt.yellow)
else:
painter.setPen(QtCore.Qt.red)
painter.drawLine(0,0,0,self.current_size[1])
painter.drawLine(0,0,self.current_size[0],0)
painter.drawLine(self.current_size[0],0,self.current_size[0],self.current_size[1])
painter.drawLine(0,self.current_size[1],self.current_size[0],self.current_size[1])
def print_help(self):
print('Keybinds can be set in config.txt')
print(self.config['close'], '- close program')
print(self.config['idle'], '- idle mode (does nothing)')
print(self.config['record'], '- record mode')
print(self.config['control'], '- control mode')
print(self.config['record-control'], '- controls but also records clicks for training')
print(self.config['train'], '- trains network on recorded data')
print(self.config['load/save_network'], '- load or save current network')
print(self.config['load/save_data'], '- load or save training data')
print(self.config['new_network'], '- create new network with random weights')
print(self.config['clear_recording'], '- deletes all training data')
print(self.config['data_edit'], '- opens an interface to edit recorded data')
print(self.config['help'], '- prints this message')
def find_window_then_resize_and_move(window_name, position, size):
hwnd = win32gui.FindWindow(None, window_name)
if hwnd == 0:
print('Window with name', window_name, 'not found for auto moving/resizing')
print('Window name can be set in config.txt')
return None
win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, *position, *size, win32con.SWP_SHOWWINDOW)
print('Window with name', window_name, 'moved and resized successfully')
if __name__ == "__main__":
conf_file = open('config.txt','r')
config = json.loads(conf_file.read())
app = QApplication([config])
mainWin = ScreenOverlay(config)
mainWin.show()
mainWin.print_help()
app.exec_()
print('done')