Skip to content

Commit

Permalink
fix: performance, windows cursor flicker
Browse files Browse the repository at this point in the history
* CAPTURES_DIR -> CAPTURE_DIR_PATH; add capture/__main__.py

* save as mp4

* mss.windows.CAPTUREBLT = 0

* return PIL.image in utils.take_screenshot()
  • Loading branch information
abrichr authored Apr 22, 2024
1 parent 060309b commit d354b94
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 23 deletions.
11 changes: 11 additions & 0 deletions openadapt/capture/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""Entrypoint for video capture module.
Usage:
python -m openadapt.capture
"""

from . import test

if __name__ == "__main__":
test()
8 changes: 5 additions & 3 deletions openadapt/capture/_windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ def start(self, audio: bool = True) -> None:

# Start video recording
self.video_out = os.path.join(
config.CAPTURES_DIR,
datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + ".mov",
config.CAPTURE_DIR_PATH,
datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + ".mp4",
)
if not os.path.exists(config.CAPTURE_DIR_PATH):
os.mkdir(config.CAPTURE_DIR_PATH)
screen_recorder.start_video_recording(self.video_out, 30, 8000000, True)

# Start audio recording
if audio:
self.audio_out = os.path.join(
config.CAPTURES_DIR,
config.CAPTURE_DIR_PATH,
datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + ".wav",
)
self.audio_stream = self.audio.open(
Expand Down
8 changes: 5 additions & 3 deletions openadapt/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from collections import namedtuple
from functools import partial, wraps
from typing import Any, Callable, Union
import io
import multiprocessing
import os
import queue
Expand All @@ -24,7 +25,6 @@
from pympler import tracker
from tqdm import tqdm
import fire
import mss.tools
import psutil

from openadapt import config, utils, video, window
Expand Down Expand Up @@ -291,9 +291,11 @@ def write_screen_event(
perf_q: A queue for collecting performance data.
"""
assert event.type == "screen", event
screenshot = event.data
image = event.data
if config.RECORD_IMAGES:
png_data = mss.tools.to_png(screenshot.rgb, screenshot.size)
with io.BytesIO() as output:
image.save(output, format="PNG")
png_data = output.getvalue()
event_data = {"png_data": png_data}
else:
event_data = {}
Expand Down
16 changes: 13 additions & 3 deletions openadapt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@
import numpy as np
import orjson


if sys.platform == "win32":
import mss.windows

# fix cursor flicker on windows; see:
# https://github.com/BoboTiG/python-mss/issues/179#issuecomment-673292002
mss.windows.CAPTUREBLT = 0


from openadapt import common, config
from openadapt.db import db
from openadapt.logging import filter_log_messages
Expand Down Expand Up @@ -678,16 +687,17 @@ def evenly_spaced(arr: list, N: list) -> list:
return [val for idx, val in enumerate(arr) if idx in idxs]


def take_screenshot() -> mss.base.ScreenShot:
def take_screenshot() -> Image.Image:
"""Take a screenshot.
Returns:
mss.base.ScreenShot: The screenshot.
PIL.Image: The screenshot image.
"""
# monitor 0 is all in one
monitor = SCT.monitors[0]
sct_img = SCT.grab(monitor)
return sct_img
image = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
return image


def get_strategy_class_by_name() -> dict:
Expand Down
20 changes: 6 additions & 14 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d354b94

Please sign in to comment.