Skip to content

Commit d354b94

Browse files
authored
fix: performance, windows cursor flicker
* CAPTURES_DIR -> CAPTURE_DIR_PATH; add capture/__main__.py * save as mp4 * mss.windows.CAPTUREBLT = 0 * return PIL.image in utils.take_screenshot()
1 parent 060309b commit d354b94

File tree

5 files changed

+40
-23
lines changed

5 files changed

+40
-23
lines changed

openadapt/capture/__main__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""Entrypoint for video capture module.
2+
3+
Usage:
4+
5+
python -m openadapt.capture
6+
"""
7+
8+
from . import test
9+
10+
if __name__ == "__main__":
11+
test()

openadapt/capture/_windows.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,17 @@ def start(self, audio: bool = True) -> None:
4848

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

5658
# Start audio recording
5759
if audio:
5860
self.audio_out = os.path.join(
59-
config.CAPTURES_DIR,
61+
config.CAPTURE_DIR_PATH,
6062
datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + ".wav",
6163
)
6264
self.audio_stream = self.audio.open(

openadapt/record.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from collections import namedtuple
1010
from functools import partial, wraps
1111
from typing import Any, Callable, Union
12+
import io
1213
import multiprocessing
1314
import os
1415
import queue
@@ -24,7 +25,6 @@
2425
from pympler import tracker
2526
from tqdm import tqdm
2627
import fire
27-
import mss.tools
2828
import psutil
2929

3030
from openadapt import config, utils, video, window
@@ -291,9 +291,11 @@ def write_screen_event(
291291
perf_q: A queue for collecting performance data.
292292
"""
293293
assert event.type == "screen", event
294-
screenshot = event.data
294+
image = event.data
295295
if config.RECORD_IMAGES:
296-
png_data = mss.tools.to_png(screenshot.rgb, screenshot.size)
296+
with io.BytesIO() as output:
297+
image.save(output, format="PNG")
298+
png_data = output.getvalue()
297299
event_data = {"png_data": png_data}
298300
else:
299301
event_data = {}

openadapt/utils.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626
import numpy as np
2727
import orjson
2828

29+
30+
if sys.platform == "win32":
31+
import mss.windows
32+
33+
# fix cursor flicker on windows; see:
34+
# https://github.com/BoboTiG/python-mss/issues/179#issuecomment-673292002
35+
mss.windows.CAPTUREBLT = 0
36+
37+
2938
from openadapt import common, config
3039
from openadapt.db import db
3140
from openadapt.logging import filter_log_messages
@@ -678,16 +687,17 @@ def evenly_spaced(arr: list, N: list) -> list:
678687
return [val for idx, val in enumerate(arr) if idx in idxs]
679688

680689

681-
def take_screenshot() -> mss.base.ScreenShot:
690+
def take_screenshot() -> Image.Image:
682691
"""Take a screenshot.
683692
684693
Returns:
685-
mss.base.ScreenShot: The screenshot.
694+
PIL.Image: The screenshot image.
686695
"""
687696
# monitor 0 is all in one
688697
monitor = SCT.monitors[0]
689698
sct_img = SCT.grab(monitor)
690-
return sct_img
699+
image = Image.frombytes("RGB", sct_img.size, sct_img.bgra, "raw", "BGRX")
700+
return image
691701

692702

693703
def get_strategy_class_by_name() -> dict:

poetry.lock

Lines changed: 6 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)