Skip to content

Commit

Permalink
Fixup pynput import issues in headless tests
Browse files Browse the repository at this point in the history
  • Loading branch information
plstcharles committed Apr 20, 2019
1 parent a598b57 commit 3c29932
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def test_annotate_data(annot_config, mocker):
_ = mocker.patch("cv2.namedWindow")
_ = mocker.patch("cv2.waitKey")
_ = mocker.patch("cv2.imshow")
_ = mocker.patch("pynput.keyboard.Listener")
_ = mocker.patch("thelper.gui.utils.create_key_listener")
fake_config = copy.deepcopy(annot_config)
del fake_config["name"]
with pytest.raises(AssertionError):
Expand Down
1 change: 1 addition & 0 deletions thelper/gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
from thelper.gui.annotators import Annotator # noqa: F401
from thelper.gui.annotators import ImageSegmentAnnotator # noqa: F401
from thelper.gui.utils import create_annotator # noqa: F401
from thelper.gui.utils import create_key_listener # noqa: F401

logger = logging.getLogger("thelper.gui")
3 changes: 1 addition & 2 deletions thelper/gui/annotators.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,7 @@ def __init__(self, session_name, config, save_dir, datasets):
self.image, self.mask = self.load(self.curr_sample_idx)
self.image_display, self.image_display_base, self.mask_display = self.refresh_layers()
self.gui_display = None
import pynput.keyboard
self.listener = pynput.keyboard.Listener(on_press=ImageSegmentAnnotator.on_press)
self.listener = thelper.gui.create_key_listener(ImageSegmentAnnotator.on_press)
self.listener.start()
cv.setMouseCallback(self.window_name, ImageSegmentAnnotator.on_mouse)

Expand Down
6 changes: 6 additions & 0 deletions thelper/gui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
logger = logging.getLogger(__name__)


def create_key_listener(callback):
"""Returns a key press listener based on pynput.keyboard (used for mocking)."""
import pynput.keyboard
return pynput.keyboard.Listener(on_press=callback)


def create_annotator(session_name, save_dir, config, datasets):
"""Instantiates a GUI annotation tool based on the type contained in the config dictionary.
Expand Down

0 comments on commit 3c29932

Please sign in to comment.