-
-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathvisualize.py
More file actions
424 lines (362 loc) · 12.5 KB
/
Copy pathvisualize.py
File metadata and controls
424 lines (362 loc) · 12.5 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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
"""Implements visualization utilities for OpenAdapt."""
from pprint import pformat
from threading import Timer
import html
import os
import string
from bokeh.io import output_file, show
from bokeh.layouts import layout, row
from bokeh.models.widgets import Div
from loguru import logger
from openadapt.build_utils import redirect_stdout_stderr
with redirect_stdout_stderr():
from tqdm import tqdm
import fire
from openadapt import video
from openadapt.config import RECORDING_DIR_PATH, config
from openadapt.db import crud
from openadapt.events import get_events
from openadapt.models import Recording
from openadapt.plotting import display_event
from openadapt.utils import (
EMPTY,
compute_diff,
configure_logging,
evenly_spaced,
get_posthog_instance,
image2utf8,
row2dict,
rows2dicts,
)
SCRUB = config.SCRUB_ENABLED
posthog = get_posthog_instance()
LOG_LEVEL = "INFO"
MAX_EVENTS = None
MAX_TABLE_CHILDREN = 5
MAX_TABLE_STR_LEN = 1024
PROCESS_EVENTS = True
IMG_WIDTH_PCT = 60
def recursive_len(lst: list, key: str) -> int:
"""Calculate the recursive length of a list based on a key.
Args:
lst (list): The list to calculate the length of.
key: The key to access the sublists.
Returns:
int: The recursive length of the list.
"""
try:
_len = len(lst)
except TypeError:
return 0
for obj in lst:
try:
_len += recursive_len(obj.get(key), key)
except AttributeError:
continue
return _len
def format_key(key: str, value: list) -> str:
"""Format a key and value for display.
Args:
key: The key to format.
value: The value associated with the key.
Returns:
str: The formatted key and value.
"""
if isinstance(value, list):
return f"{key} ({len(value)}; {recursive_len(value, key)})"
else:
return key
def indicate_missing(some: list, every: list, indicator: str) -> list:
"""Indicate missing elements in a list.
Args:
some (list): The list with potentially missing elements.
every (list): The reference list with all elements.
indicator (str): The indicator to use for missing elements.
Returns:
list: The list with indicators for missing elements.
"""
rval = []
some_idx = 0
every_idx = 0
while some_idx < len(some):
skipped = False
while some[some_idx] != every[every_idx]:
every_idx += 1
skipped = True
if skipped:
rval.append(indicator)
rval.append(some[some_idx])
some_idx += 1
every_idx += 1
return rval
def dict2html(
obj: dict,
max_children: int = MAX_TABLE_CHILDREN,
max_len: int = MAX_TABLE_STR_LEN,
) -> str:
"""Convert a dictionary to an HTML representation.
Args:
obj (dict): The dictionary to convert.
max_children (int): The maximum number of child elements to display in a table.
max_len (int): The maximum length of a string value in the HTML representation.
Returns:
str: The HTML representation of the dictionary.
"""
if isinstance(obj, list):
children = [dict2html(value, max_children) for value in obj]
if max_children is not None and len(children) > max_children:
all_children = children
children = evenly_spaced(children, max_children)
children = indicate_missing(children, all_children, "...")
html_str = "\n".join(children)
elif isinstance(obj, dict):
rows_html = "\n".join([f"""
<tr>
<th>{format_key(key, value)}</th>
<td>{dict2html(value, max_children)}</td>
</tr>
""" for key, value in obj.items() if value not in EMPTY])
html_str = f"<table>{rows_html}</table>"
else:
html_str = html.escape(str(obj))
if len(html_str) > max_len:
n = max_len // 2
head = html_str[:n]
tail = html_str[-n:]
snipped = html_str[n:-n]
middle = f"<br/>...<i>(snipped {len(snipped):,})...</i><br/>"
html_str = head + middle + tail
return html_str
@logger.catch
def main(
recording: Recording = None,
recording_id: int = None,
diff_video: bool = False,
cleanup: bool = True,
) -> bool:
"""Visualize a recording.
Args:
recording (Recording, optional): The recording to visualize.
recording_id (int, optional): The ID of the recording to visualize.
diff_video (bool): Whether to diff Screenshots against video frames.
cleanup (bool): Whether to remove the HTML file after it is displayed.
Returns:
bool: True if visualization was successful, None otherwise.
"""
configure_logging(logger, LOG_LEVEL)
assert not all([recording, recording_id]), "Only one may be specified."
posthog.capture(
event="visualize.started", properties={"recording_id": recording_id}
)
session = crud.get_new_session(read_only=True)
if recording_id:
recording = crud.get_recording_by_id(session, recording_id)
elif recording is None:
recording = crud.get_latest_recording(session)
if recording is None:
logger.error("No recording found")
return False
if SCRUB:
from openadapt.privacy.providers.presidio import PresidioScrubbingProvider
scrub = PresidioScrubbingProvider()
scrub.scrub_text(recording.task_description)
logger.info(f"{recording=}")
logger.info(f"{diff_video=}")
session = crud.get_new_session(read_only=True)
audio_info = row2dict(crud.get_audio_info(session, recording))
if audio_info:
del audio_info["flac_data"]
# TODO XXX: display audio_info
if diff_video:
assert recording.config[
"RECORD_VIDEO"
], "Can't diff video against images because video was not saved."
assert recording.config[
"RECORD_IMAGES"
], "Can't diff video against images because images were not saved."
meta = {}
action_events = get_events(session, recording, process=PROCESS_EVENTS, meta=meta)
event_dicts = rows2dicts(action_events)
if SCRUB:
event_dicts = scrub.scrub_list_dicts(event_dicts)
logger.info(f"event_dicts=\n{pformat(event_dicts)}")
recording_dict = row2dict(recording)
if SCRUB:
recording_dict = scrub.scrub_dict(recording_dict)
CSS = string.Template("""
table {
outline: 1px solid black;
}
table th {
vertical-align: top;
}
.screenshot img {
display: none;
width: ${IMG_WIDTH_PCT}vw;
}
.screenshot img:nth-child(1) {
display: block;
}
.screenshot:hover img:nth-child(1) {
display: none;
}
.screenshot:hover img:nth-child(2) {
display: block;
}
.screenshot:hover img:nth-child(3) {
display: none;
}
.screenshot:active img:nth-child(1) {
display: none;
}
.screenshot:active img:nth-child(2) {
display: none;
}
.screenshot:active img:nth-child(3) {
display: block;
}
""").substitute(
IMG_WIDTH_PCT=IMG_WIDTH_PCT,
)
rows = [
row(
Div(
text=f"<style>{CSS}</style>",
),
),
row(
Div(
text=f"{dict2html(recording_dict)}",
),
),
row(
Div(
text=f"{dict2html(meta)}",
width_policy="max",
),
),
]
logger.info(f"{len(action_events)=}")
if diff_video:
video_file_path = video.get_video_file_path(recording.timestamp)
timestamps = [
action_event.screenshot.timestamp - recording.video_start_time
for action_event in action_events
]
frames = video.extract_frames(video_file_path, timestamps)
num_events = (
min(MAX_EVENTS, len(action_events))
if MAX_EVENTS is not None
else len(action_events)
)
with redirect_stdout_stderr():
with tqdm(
total=num_events,
desc="Preparing HTML",
unit="event",
colour="green",
dynamic_ncols=True,
) as progress:
for idx, action_event in enumerate(action_events):
if idx == MAX_EVENTS:
break
try:
image = display_event(action_event)
except TypeError as exc:
# https://github.com/moses-palmer/pynput/issues/481
logger.warning(exc)
continue
if image:
if diff_video:
frame_image = frames[idx]
diff_image = compute_diff(
frame_image, action_event.screenshot.image
)
# TODO: rename
diff = frame_image
mask = diff_image
else:
diff = display_event(action_event, diff=True)
mask = action_event.screenshot.diff_mask
if SCRUB:
image = scrub.scrub_image(image)
diff = scrub.scrub_image(diff)
mask = scrub.scrub_image(mask)
image_utf8 = image2utf8(image)
diff_utf8 = image2utf8(diff)
mask_utf8 = image2utf8(mask)
width, height = image.size
else:
# TODO: display a placeholder image
image_utf8 = ""
diff_utf8 = ""
mask_utf8 = ""
width = 0
height = 1
action_event_dict = row2dict(action_event)
window_event_dict = row2dict(action_event.window_event)
if SCRUB:
action_event_dict = scrub.scrub_dict(action_event_dict)
window_event_dict = scrub.scrub_dict(window_event_dict)
rows.append(
[
row(
Div(
text=f"""
<div class="screenshot">
<img
src="{image_utf8}"
style="
aspect-ratio: {width}/{height};
"
>
<img
src="{diff_utf8}"
style="
aspect-ratio: {width}/{height};
"
>
<img
src="{mask_utf8}"
style="
aspect-ratio: {width}/{height};
"
>
</div>
<table>
{dict2html(window_event_dict , None)}
</table>
""",
),
Div(text=f"""
<table>
{dict2html(action_event_dict)}
</table>
"""),
),
]
)
progress.update()
progress.close()
title = f"recording-{recording.id}"
fname_out = RECORDING_DIR_PATH / f"recording-{recording.id}.html"
logger.info(f"{fname_out=}")
os.makedirs(RECORDING_DIR_PATH, exist_ok=True)
output_file(fname_out, title=title)
result = show( # noqa: F841
layout(
rows,
)
)
def _cleanup() -> None:
os.remove(fname_out)
removed = not os.path.exists(fname_out)
logger.info(f"{removed=}")
if cleanup:
Timer(1, _cleanup).start()
posthog.capture(
event="visualize.completed", properties={"recording_id": recording.id}
)
return True
if __name__ == "__main__":
fire.Fire(main)