forked from samyamaryal/chAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecord.py
More file actions
302 lines (251 loc) · 9.77 KB
/
record.py
File metadata and controls
302 lines (251 loc) · 9.77 KB
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
import cv2
import os
import json
import time
from datetime import datetime
import threading
import mss
import numpy as np
from pynput import keyboard, mouse
# Configuration
output_base = "frames"
webcam_folder = os.path.join(output_base, "webcam")
screen_folder = os.path.join(output_base, "screen")
log_file = "./frames/combined_log.json"
# Setup
os.makedirs(webcam_folder, exist_ok=True)
os.makedirs(screen_folder, exist_ok=True)
# Global recording state
recording_threads = []
recording_listeners = []
is_recording = False
shared_data = {}
data_lock = threading.Lock()
def start_recording():
"""Start recording webcam, screen, keyboard, and mouse events"""
global is_recording, recording_threads, recording_listeners, shared_data
print("Starting recording process...")
if is_recording:
print("Already recording")
return False, "Already recording"
# Initialize shared data
with data_lock:
shared_data = {
"log_data": {
"webcam": [],
"screen": [],
"keyboard": [],
"mouse": []
},
"running": True
}
print("Shared data initialized")
# Webcam capture function
def capture_webcam():
print("Starting webcam capture...")
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Could not open webcam.")
return
frame_count = 0
last_time = time.time()
try:
while shared_data["running"]:
ret, frame = cap.read()
if not ret:
print("Error: Could not read webcam frame")
break
now = time.time()
fps = 1 / (now - last_time) if frame_count > 0 else 0.0
last_time = now
filename = f"webcam_{frame_count:06d}.jpg"
path = os.path.join(webcam_folder, filename)
# Ensure webcam directory exists
os.makedirs(webcam_folder, exist_ok=True)
success = cv2.imwrite(path, frame)
if success:
print(f"Saved webcam frame: {filename}")
else:
print(f"Failed to save webcam frame: {filename}")
timestamp = datetime.now().isoformat()
with data_lock:
shared_data["log_data"]["webcam"].append({
"frame": frame_count,
"time": timestamp,
"file": filename,
"fps": round(fps, 2)
})
frame_count += 1
time.sleep(1 / 30)
except Exception as e:
print(f"Error in webcam capture: {e}")
finally:
cap.release()
print(f"Webcam capture stopped. Total frames: {frame_count}")
# Screen capture function
def capture_screen():
print("Starting screen capture...")
sct = mss.mss()
monitor = sct.monitors[1]
frame_count = 0
last_time = time.time()
try:
while shared_data["running"]:
img = sct.grab(monitor)
frame = np.array(img)
frame = cv2.cvtColor(frame, cv2.COLOR_BGRA2BGR)
now = time.time()
fps = 1 / (now - last_time) if frame_count > 0 else 0.0
last_time = now
filename = f"screen_{frame_count:06d}.jpg"
path = os.path.join(screen_folder, filename)
# Ensure screen directory exists
os.makedirs(screen_folder, exist_ok=True)
success = cv2.imwrite(path, frame)
if success:
print(f"Saved screen frame: {filename}")
else:
print(f"Failed to save screen frame: {filename}")
timestamp = datetime.now().isoformat()
with data_lock:
shared_data["log_data"]["screen"].append({
"frame": frame_count,
"time": timestamp,
"file": filename,
"fps": round(fps, 2)
})
frame_count += 1
time.sleep(1 / 60)
except Exception as e:
print(f"Error in screen capture: {e}")
finally:
sct.close()
print(f"Screen capture stopped. Total frames: {frame_count}")
# Keyboard event handlers
def on_key_press(key):
try:
with data_lock:
shared_data["log_data"]["keyboard"].append({
"event": "press",
"key": str(key.char),
"time": datetime.now().isoformat()
})
except AttributeError:
with data_lock:
shared_data["log_data"]["keyboard"].append({
"event": "press",
"key": str(key),
"time": datetime.now().isoformat()
})
def on_key_release(key):
with data_lock:
shared_data["log_data"]["keyboard"].append({
"event": "release",
"key": str(key),
"time": datetime.now().isoformat()
})
# Mouse click event handler only
def on_click(x, y, button, pressed):
with data_lock:
shared_data["log_data"]["mouse"].append({
"event": "click",
"button": str(button),
"pressed": pressed,
"position": [x, y],
"time": datetime.now().isoformat()
})
# Create threads and listeners
webcam_thread = threading.Thread(target=capture_webcam)
screen_thread = threading.Thread(target=capture_screen)
keyboard_listener = keyboard.Listener(on_press=on_key_press, on_release=on_key_release)
mouse_listener = mouse.Listener(on_click=on_click)
# Store references for cleanup
recording_threads = [webcam_thread, screen_thread]
recording_listeners = [keyboard_listener, mouse_listener]
print("Threads and listeners created")
# Start recording
try:
print("Starting threads...")
webcam_thread.start()
screen_thread.start()
keyboard_listener.start()
mouse_listener.start()
is_recording = True
print("Recording started successfully")
return True, "Recording started successfully"
except Exception as e:
print(f"Failed to start recording: {e}")
# Cleanup on error
with data_lock:
shared_data["running"] = False
for thread in recording_threads:
if thread.is_alive():
thread.join(timeout=1)
for listener in recording_listeners:
listener.stop()
recording_threads = []
recording_listeners = []
return False, f"Failed to start recording: {str(e)}"
def stop_recording():
"""Stop recording and save log data"""
global is_recording, recording_threads, recording_listeners, shared_data
print("Stopping recording...")
if not is_recording:
print("Not currently recording")
return False, "Not currently recording"
try:
# Stop recording
with data_lock:
shared_data["running"] = False
is_recording = False
print("Recording flag set to False")
# Stop threads
print("Stopping threads...")
for thread in recording_threads:
if thread.is_alive():
thread.join(timeout=2)
# Stop listeners
print("Stopping listeners...")
for listener in recording_listeners:
listener.stop()
# Save log data
with data_lock:
log_data = shared_data["log_data"].copy()
print(f"Log data summary:")
print(f"- Webcam entries: {len(log_data.get('webcam', []))}")
print(f"- Screen entries: {len(log_data.get('screen', []))}")
print(f"- Keyboard entries: {len(log_data.get('keyboard', []))}")
print(f"- Mouse entries: {len(log_data.get('mouse', []))}")
# Ensure frames directory exists
os.makedirs(output_base, exist_ok=True)
# Save log data with error handling
try:
with open(log_file, "w") as f:
json.dump(log_data, f, indent=4)
print(f"Log file saved to: {log_file}")
except Exception as e:
print(f"Warning: Could not save log file: {e}")
# Continue even if log save fails
# Clear references
recording_threads = []
recording_listeners = []
print("Recording stopped successfully")
return True, "Recording stopped and log saved"
except Exception as e:
print(f"Failed to stop recording: {e}")
return False, f"Failed to stop recording: {str(e)}"
def is_recording_active():
"""Check if recording is currently active"""
return is_recording
# For standalone execution
if __name__ == "__main__":
print("Starting recording... Press Ctrl+C to stop.")
success, message = start_recording()
print(message)
try:
while is_recording:
time.sleep(1)
except KeyboardInterrupt:
print("Stopping recording...")
success, message = stop_recording()
print(message)