-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23.data_loader_visualizer.py
More file actions
341 lines (293 loc) · 11.3 KB
/
23.data_loader_visualizer.py
File metadata and controls
341 lines (293 loc) · 11.3 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
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
import cv2
from data_loader import DataLoader
import os
import numpy as np
from typing import List, Dict, Tuple, Optional
import json
class RecordingVisualizer:
def __init__(self, video_path: str, jsonl_path: str):
self.cap = cv2.VideoCapture(video_path)
self.frame_events = self.load_jsonl(jsonl_path)
self.total_frames = len(self.frame_events)
if self.total_frames == 0:
raise ValueError("No events found in JSONL file")
# Verify video file is valid
if not self.cap.isOpened():
raise ValueError(f"Could not open video file: {video_path}")
# Get frame dimensions
self.frame_width = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
self.frame_height = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
@staticmethod
def load_jsonl(jsonl_path: str) -> List[Dict]:
"""Load JSONL file into list of events"""
events = []
with open(jsonl_path, 'r') as f:
for line in f:
events.append(json.loads(line.strip()))
return events
def parse_mouse_position(self, mouse_pos: Optional[List[int]]) -> Optional[Tuple[int, int]]:
"""Safely parse mouse position data"""
if not mouse_pos or not isinstance(mouse_pos, (list, tuple)) or len(mouse_pos) != 2:
return None
try:
x, y = int(mouse_pos[0]), int(mouse_pos[1])
return (x, y)
except (ValueError, TypeError):
return None
def is_mouse_in_frame(self, mouse_pos: Optional[Tuple[int, int]]) -> bool:
"""Check if mouse coordinates are within frame bounds"""
if not mouse_pos:
return False
x, y = mouse_pos
return (0 <= x < self.frame_width) and (0 <= y < self.frame_height)
def get_edge_intersection(self, mouse_pos: Tuple[int, int]) -> Tuple[int, int]:
"""Get the point where a line from the center to mouse_pos intersects frame edge"""
x, y = mouse_pos
center_x = self.frame_width // 2
center_y = self.frame_height // 2
# Vector from center to mouse position
dx = x - center_x
dy = y - center_y
# Find intersection with frame edges
if abs(dx) > abs(dy):
# Intersects with left or right edge
if dx > 0:
edge_x = self.frame_width - 1
else:
edge_x = 0
# Scale y accordingly
if dx != 0:
edge_y = int(center_y + dy * (edge_x - center_x) / dx)
edge_y = min(max(edge_y, 0), self.frame_height - 1)
else:
edge_y = center_y
else:
# Intersects with top or bottom edge
if dy > 0:
edge_y = self.frame_height - 1
else:
edge_y = 0
# Scale x accordingly
if dy != 0:
edge_x = int(center_x + dx * (edge_y - center_y) / dy)
edge_x = min(max(edge_x, 0), self.frame_width - 1)
else:
edge_x = center_x
return edge_x, edge_y
def draw_offscreen_indicator(self, frame: np.ndarray, mouse_pos: Tuple[int, int]) -> np.ndarray:
"""Draw an arrow indicating off-screen mouse position"""
if not mouse_pos:
return frame
x, y = mouse_pos
# Get intersection point with frame edge
edge_x, edge_y = self.get_edge_intersection(mouse_pos)
# Draw arrow from edge pointing towards off-screen mouse
arrow_length = 20
center_x = self.frame_width // 2
center_y = self.frame_height // 2
# Calculate arrow direction
dx = x - center_x
dy = y - center_y
magnitude = np.sqrt(dx*dx + dy*dy)
if magnitude > 0:
dx = dx / magnitude
dy = dy / magnitude
# Draw arrow
start_point = (edge_x - int(dx * arrow_length), edge_y - int(dy * arrow_length))
end_point = (edge_x, edge_y)
cv2.arrowedLine(
frame,
start_point,
end_point,
(0, 255, 0),
2,
tipLength=0.3
)
# Add distance indicator
distance = int(np.sqrt((x - edge_x)**2 + (y - edge_y)**2))
cv2.putText(
frame,
f"{distance}px",
(edge_x + 5, edge_y + 5),
cv2.FONT_HERSHEY_SIMPLEX,
0.4,
(0, 255, 0),
1
)
return frame
def draw_mouse_status(self, frame: np.ndarray,
raw_mouse_pos: Optional[List[int]],
mouse_buttons: List[str],
mouse_scroll: Optional[Tuple[int, int]],
y_start: int = 80) -> np.ndarray:
"""Draw mouse events on frame"""
# Mouse position
cv2.putText(
frame,
"Mouse:",
(10, y_start),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(255, 255, 255),
1
)
# Parse mouse position safely
mouse_pos = self.parse_mouse_position(raw_mouse_pos)
if mouse_pos:
x, y = mouse_pos
pos_text = f"Position: ({x}, {y})"
if self.is_mouse_in_frame(mouse_pos):
# Draw crosshair for in-frame mouse position
cv2.drawMarker(
frame,
(x, y),
(0, 255, 0),
markerType=cv2.MARKER_CROSS,
markerSize=15,
thickness=1
)
else:
# Draw off-screen indicator
frame = self.draw_offscreen_indicator(frame, mouse_pos)
pos_text += " (Off-screen)"
else:
pos_text = "Position: None"
cv2.putText(
frame,
pos_text,
(10, y_start + 20),
cv2.FONT_HERSHEY_SIMPLEX,
0.4,
(255, 255, 255),
1
)
# Mouse buttons
button_text = ", ".join(mouse_buttons) if mouse_buttons else "None"
cv2.putText(
frame,
f"Buttons: {button_text}",
(10, y_start + 40),
cv2.FONT_HERSHEY_SIMPLEX,
0.4,
(255, 255, 255),
1
)
# Scroll info
if mouse_scroll:
scroll_text = f"Scroll: dx={mouse_scroll[0]}, dy={mouse_scroll[1]}"
else:
scroll_text = "Scroll: None"
cv2.putText(
frame,
scroll_text,
(10, y_start + 60),
cv2.FONT_HERSHEY_SIMPLEX,
0.4,
(255, 255, 255),
1
)
return frame
def draw_keyboard_status(self, frame: np.ndarray, keys: List[str], y_start: int = 30) -> np.ndarray:
"""Draw keyboard events on frame"""
cv2.putText(
frame,
"Keyboard:",
(10, y_start),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(255, 255, 255),
1
)
# Display active keys
key_text = ", ".join(keys) if keys else "None"
cv2.putText(
frame,
f"Active Keys: {key_text}",
(10, y_start + 20),
cv2.FONT_HERSHEY_SIMPLEX,
0.4,
(255, 255, 255),
1
)
return frame
def draw_frame_info(self, frame: np.ndarray, frame_number: int, timestamp: float) -> np.ndarray:
"""Draw frame information"""
cv2.putText(
frame,
f"Frame: {frame_number} | Time: {timestamp:.2f}s",
(10, 20),
cv2.FONT_HERSHEY_SIMPLEX,
0.5,
(255, 255, 255),
1
)
return frame
def visualize(self):
"""Display frames with overlaid input information"""
frame_idx = 0
while frame_idx < self.total_frames:
ret, frame = self.cap.read()
if not ret:
print("Error reading frame")
break
event = self.frame_events[frame_idx]
# Draw information layers
frame = self.draw_frame_info(frame, event['frame_number'], event['timestamp'])
frame = self.draw_keyboard_status(frame, event['keyboard_keys'])
frame = self.draw_mouse_status(
frame,
event['mouse_position'],
event['mouse_buttons'],
event['mouse_scroll']
)
# Display the frame
cv2.imshow('Recording Playback', frame)
# Handle keyboard input
key = cv2.waitKey(0) # Wait for key press
if key == ord('q'): # Quit
break
elif key == ord('a'): # Previous frame
frame_idx = max(0, frame_idx - 1)
self.cap.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)
elif key == ord('d'): # Next frame
frame_idx = min(self.total_frames - 1, frame_idx + 1)
elif key == ord('s'): # Play/pause
while True:
ret, frame = self.cap.read()
if not ret or frame_idx >= self.total_frames - 1:
break
frame_idx += 1
event = self.frame_events[frame_idx]
frame = self.draw_frame_info(frame, event['frame_number'], event['timestamp'])
frame = self.draw_keyboard_status(frame, event['keyboard_keys'])
frame = self.draw_mouse_status(
frame,
event['mouse_position'],
event['mouse_buttons'],
event['mouse_scroll']
)
cv2.imshow('Recording Playback', frame)
if cv2.waitKey(33) == ord('s'): # ~30 FPS playback
break
self.cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
# Example usage:
dataset_dir = './recordings/' # Replace with your dataset directory
batch_size = 2
n_workers = 2
n_epochs = 1
data_loader = DataLoader(dataset_dir, n_workers, batch_size, n_epochs)
for i, (frames, actions, episode_ids) in enumerate(data_loader):
print(f"Batch {i+1}:")
print(f" - Frames (first frame in batch): shape={frames[0].shape}, type={frames[0].dtype}")
print(f" - Actions (first action in batch): {actions[0]}")
print(f" - Episode IDs: {episode_ids}")
# Display video with annotation
video_path = os.path.join(dataset_dir, f"recording_20241222_132253_recording.mp4")
jsonl_path = os.path.join(dataset_dir, f"recording_20241222_132253_events.jsonl")
# load data for display
recorder_visualizer = RecordingVisualizer(video_path, jsonl_path)
recorder_visualizer.visualize()
if i >= 2:
break