-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
301 lines (240 loc) · 8.16 KB
/
app.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
import sys
import pyautogui
import sqlalchemy
import threading
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, DateTime, create_engine, MetaData, Table, asc
from sqlalchemy.orm import sessionmaker
import time
import json
from pynput import mouse, keyboard
from PIL import Image, ImageDraw
import cv2
import os
print("Click 'ESC' to end the recording.")
recording = []
count = 0
Base = declarative_base()
is_abort = False
class Event(Base):
__tablename__ = 'events'
id = Column(Integer, primary_key=True, autoincrement=True)
timestamp = Column(DateTime, default=sqlalchemy.func.now())
json_data_str = Column(String)
def draw_pointer(source_img_path, x, y, button, key, action):
with Image.open(source_img_path) as img:
draw = ImageDraw.Draw(img)
rect_height = 20
rect_width = 600
color = 'orange'
if x > 0 and y > 0:
if str(button) == 'Button.right':
color = 'blue'
elif str(button) == 'Button.left':
color = 'green'
pointer_size = 24
x0 = x - pointer_size // 2
y0 = y - pointer_size // 2
x1 = x0 + pointer_size
y1 = y0 + pointer_size
draw.ellipse([(x0, y0), (x1, y1)], fill=color)
rect_x0 = 0
rect_y0 = 0
rect_x1 = rect_x0 + rect_width
rect_y1 = rect_y0 + rect_height
draw.rectangle([(rect_x0, rect_y0), (rect_x1, rect_y1)], fill="#666600")
text = f"x={x}, y={y}, key={key}, action={action}"
text_color = "white"
draw.text((5, 5), text, fill=text_color)
img.save(source_img_path)
def create_db_session(engine):
meta = MetaData()
events_table = Table('events', meta, Column('id', Integer, primary_key=True, autoincrement=True),
Column('timestamp', DateTime, default=sqlalchemy.func.now()), Column('json_data_str', String))
meta.create_all(engine)
Session = sessionmaker(bind=engine)
return Session()
def save_event(session, data):
if 'key' in data['action']:
action = 'type'
elif 'button' in data['action']:
action = 'click'
else:
action = 'move'
screen_shot = f'c:/windows/temp/{action}_{(time.time())}.png'
data['screen_shot'] = screen_shot
json_data = json.dumps(data)
event = Event(json_data_str=json_data)
session.add(event)
session.commit()
t = threading.Thread(target=trigger_screenshot, args=(str(screen_shot),))
t.start()
def on_press(key):
global is_abort
try:
json_object = {
'action': 'pressed_key',
'key': key.char,
'_time': time.time(),
}
if key == keyboard.Key.esc:
print("Keyboard recording ended.")
is_abort = True
return False
except AttributeError:
if key == keyboard.Key.esc:
print("Recording ended.")
is_abort = True
return False
json_object = {
'action': 'pressed_key',
'key': str(key),
'_time': time.time(),
}
save_event(global_session, json_object)
def on_release(key):
try:
json_object = {
'action': 'released_key',
'key': key.char,
'_time': time.time(),
'screen_shot': ''
}
except AttributeError:
json_object = {
'action': 'released_key',
'key': str(key),
'_time': time.time(),
}
save_event(global_session, json_object)
def on_move(x, y):
if len(recording) >= 1:
if (recording[-1]['action'] == "pressed" and \
recording[-1]['button'] == 'Button.left') or \
(recording[-1]['action'] == "moved" and \
time.time() - recording[-1]['_time'] > 0.02):
json_object = {
'action': 'moved',
'x': x,
'y': y,
'_time': time.time(),
}
save_event(global_session, json_object)
if is_abort: return False
def trigger_screenshot(filename):
screenshot = pyautogui.screenshot()
screenshot.save(filename)
def on_click(x, y, button, pressed):
screen_shot = ''
if pressed:
json_object = {
'action': 'button clicked' if pressed else 'button unclicked',
'button': str(button),
'x': x,
'y': y,
'_time': time.time(),
}
save_event(global_session, json_object)
if is_abort: return False
def on_scroll(x, y, dx, dy):
json_object = {
'action': 'scroll',
'vertical_direction': int(dy),
'horizontal_direction': int(dx),
'x': x,
'y': y,
'_time': time.time(),
}
if is_abort: return False
def start_recording(session):
global global_session
global_session = session
keyboard_listener = keyboard.Listener(
on_press=on_press,
on_release=on_release)
mouse_listener = mouse.Listener(
on_click=on_click,
on_scroll=on_scroll,
on_move=on_move)
keyboard_listener.start()
mouse_listener.start()
keyboard_listener.join()
mouse_listener.join()
def create_video(screenshots, output_filename='c://windows/temp/output_video.mp4', fps=30, frame_duration=1):
try:
first_image = cv2.imread(screenshots[0])
if first_image is None:
print("Error: The first file cannot be loaded..")
return
height, width, _ = first_image.shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_filename, fourcc, fps, (width, height))
for screenshot in screenshots:
img = cv2.imread(screenshot)
if img is None:
print(f"Error: {screenshot} cannot be loaded, ignoring it.")
continue
for _ in range(fps * frame_duration):
out.write(img)
out.release()
print(f"Video generated: {output_filename}")
except Exception as e:
print("Error creating video:", e)
def compile_data(engine):
try:
session = create_db_session(engine)
events = session.query(Event).order_by(asc(Event.timestamp)).all()
screenshots = []
for event in events:
data = json.loads(event.json_data_str)
if 'screen_shot' in data and data['screen_shot'] != '' and os.path.exists(data['screen_shot']):
print('processing: ', data['screen_shot'])
key = ''
button = ''
action = ''
x = 0
y = 0
if 'key' in data['action']:
key = data['key']
action = 'type'
elif 'button' in data['action']:
button = data['button']
x = data['x']
y = data['y']
action = 'click'
else:
x = data['x']
y = data['y']
action = 'move'
draw_pointer(data['screen_shot'], x, y, button, key, action)
screenshots.append(data['screen_shot'])
print("Creating video...")
create_video(screenshots)
for screenshot in screenshots:
os.remove(screenshot)
print(screenshot, 'deleted')
except Exception as e:
print("Error compiling data:", e)
finally:
if session:
session.close()
def main():
if len(sys.argv) != 2:
print("Use: python record.py [record|compile]")
return
db_path = "c://windows/temp/scicrop-user-stats.db"
engine = create_engine(f'sqlite:///{db_path}', echo=True)
if sys.argv[1] == "record":
if os.path.exists(db_path):
os.remove(db_path)
print(f"File {db_path} deleted.")
else:
print(f"File {db_path} not found.")
session = create_db_session(engine)
start_recording(session)
elif sys.argv[1] == "compile":
compile_data(engine)
else:
print("Command not recognized. Use 'record' or 'compile'.")
if __name__ == "__main__":
main()