Skip to content

Commit

Permalink
scrubbing in diff images
Browse files Browse the repository at this point in the history
  • Loading branch information
Krish Patel committed Jun 1, 2023
1 parent 318c344 commit 37968e5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
20 changes: 12 additions & 8 deletions openadapt/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
import fire
import mss.tools

from openadapt import config, crud, utils, window
from openadapt import config, crud, scrub, utils, window


EVENT_TYPES = ("screen", "action", "window")
LOG_LEVEL = "INFO"
SCRUB_LOGGING = True
PROC_WRITE_BY_EVENT_TYPE = {
"screen": True,
"action": True,
Expand Down Expand Up @@ -67,7 +68,7 @@ def process_events(
terminate_event: An event to signal the termination of the process.
"""

utils.configure_logging(logger, LOG_LEVEL)
utils.configure_logging(logger, LOG_LEVEL, SCRUB_LOGGING)
utils.set_start_time(recording_timestamp)
logger.info(f"starting")

Expand Down Expand Up @@ -206,7 +207,7 @@ def write_events(
terminate_event: An event to signal the termination of the process.
"""

utils.configure_logging(logger, LOG_LEVEL)
utils.configure_logging(logger, LOG_LEVEL, SCRUB_LOGGING)
utils.set_start_time(recording_timestamp)
logger.info(f"{event_type=} starting")
signal.signal(signal.SIGINT, signal.SIG_IGN)
Expand Down Expand Up @@ -344,7 +345,7 @@ def read_screen_events(
recording_timestamp: The timestamp of the recording.
"""

utils.configure_logging(logger, LOG_LEVEL)
utils.configure_logging(logger, LOG_LEVEL, SCRUB_LOGGING)
utils.set_start_time(recording_timestamp)
logger.info(f"starting")
while not terminate_event.is_set():
Expand All @@ -370,7 +371,7 @@ def read_window_events(
recording_timestamp: The timestamp of the recording.
"""

utils.configure_logging(logger, LOG_LEVEL)
utils.configure_logging(logger, LOG_LEVEL, SCRUB_LOGGING)
utils.set_start_time(recording_timestamp)
logger.info(f"starting")
prev_window_data = {}
Expand Down Expand Up @@ -410,7 +411,7 @@ def performance_stats_writer (
terminate_event: An event to signal the termination of the process.
"""

utils.configure_logging(logger, LOG_LEVEL)
utils.configure_logging(logger, LOG_LEVEL, SCRUB_LOGGING)
utils.set_start_time(recording_timestamp)
logger.info("performance stats writer starting")
signal.signal(signal.SIGINT, signal.SIG_IGN)
Expand Down Expand Up @@ -516,9 +517,12 @@ def record(
task_description: a text description of the task that will be recorded
"""

utils.configure_logging(logger, LOG_LEVEL)
utils.configure_logging(logger, LOG_LEVEL, SCRUB_LOGGING)

logger.info(f"{task_description=}")
if SCRUB_LOGGING:
logger.info(f"{scrub.scrub_text(task_description)=}")
else:
logger.info(f"{task_description=}")

recording = create_recording(task_description)
recording_timestamp = recording.timestamp
Expand Down
7 changes: 5 additions & 2 deletions openadapt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@
import mss.base
import numpy as np

from openadapt import common, config
from openadapt import common, config, scrub


EMPTY = (None, [], {}, "")


def configure_logging(logger, log_level):
def configure_logging(logger, log_level, scrub_logging):
log_level_override = os.getenv("LOG_LEVEL")
log_level = log_level_override or log_level
scrub_logging = os.getenv("SCRUB_LOGGING") or scrub_logging
logger.remove()
logger.add(sys.stderr, level=log_level)
logger.debug(f"{log_level=}")
logger.debug(f"{scrub_logging=}")


def row2dict(row, follow=True):
Expand Down Expand Up @@ -327,6 +329,7 @@ def display_event(
x = recording.monitor_width * width_ratio / 2
y = recording.monitor_height * height_ratio / 2
text = action_event.text
text = scrub.scrub_text(text, is_separated=True)
image = draw_text(x, y, text, image, outline=True)
else:
raise Exception("unhandled {action_event.name=}")
Expand Down
16 changes: 11 additions & 5 deletions openadapt/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
MAX_EVENTS = None
MAX_TABLE_CHILDREN = 5
PROCESS_EVENTS = True
SCRUB_LOGGING = True
IMG_WIDTH_PCT = 60
CSS = string.Template("""
table {
Expand Down Expand Up @@ -134,14 +135,18 @@ def dict2html(obj, max_children=MAX_TABLE_CHILDREN):


def main():
configure_logging(logger, LOG_LEVEL)
configure_logging(logger, LOG_LEVEL, SCRUB_LOGGING)

recording = get_latest_recording()
if SCRUB_LOGGING:
recording.task_description = scrub.scrub_text(recording.task_description)
logger.debug(f"{recording=}")

meta = {}
action_events = get_events(recording, process=PROCESS_EVENTS, meta=meta)
event_dicts = scrub.scrub_list_dicts(rows2dicts(action_events))
event_dicts = rows2dicts(action_events)
if SCRUB_LOGGING:
event_dicts = scrub.scrub_list_dicts(event_dicts)
logger.info(f"event_dicts=\n{pformat(event_dicts)}")

rows = [
Expand All @@ -167,11 +172,12 @@ def main():
if idx == MAX_EVENTS:
break
image = display_event(action_event)
image = scrub.scrub_image(image)
diff = display_event(action_event, diff=True)
diff = scrub.scrub_image(diff)
mask = action_event.screenshot.diff_mask
mask = scrub.scrub_image(mask)
if SCRUB_LOGGING:
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)
Expand Down

0 comments on commit 37968e5

Please sign in to comment.