Skip to content

Commit

Permalink
Add button to toggle grayscale
Browse files Browse the repository at this point in the history
  • Loading branch information
roomrys committed Jun 11, 2022
1 parent 2962d64 commit 5d71030
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
5 changes: 4 additions & 1 deletion sleap/gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,7 @@ def _add_button(to, label, action, key=None):
videos_layout.addWidget(self.videosTable)

hb = QHBoxLayout()
_add_button(hb, "Toggle Grayscale", self.commands.toggleGrayscale)
_add_button(hb, "Show Video", self.videosTable.activateSelected)
_add_button(hb, "Add Videos", self.commands.addVideo)
_add_button(hb, "Remove Video", self.commands.removeVideo)
Expand Down Expand Up @@ -1172,9 +1173,10 @@ def overlay_state_connect(overlay, state_key, overlay_attribute=None):
def _update_gui_state(self):
"""Enable/disable gui items based on current state."""
has_selected_instance = self.state["instance"] is not None
has_selected_video = self.state["selected_video"] is not None
has_selected_node = self.state["selected_node"] is not None
has_selected_edge = self.state["selected_edge"] is not None
has_selected_video = self.state["selected_video"] is not None
has_video = self.state["video"] is not None

has_frame_range = bool(self.state["has_frame_range"])
has_unsaved_changes = bool(self.state["has_changes"])
Expand Down Expand Up @@ -1224,6 +1226,7 @@ def _update_gui_state(self):
self._buttons["add edge"].setEnabled(has_nodes_selected)
self._buttons["delete edge"].setEnabled(has_selected_edge)
self._buttons["delete node"].setEnabled(has_selected_node)
self._buttons["toggle grayscale"].setEnabled(has_video)
self._buttons["show video"].setEnabled(has_selected_video)
self._buttons["remove video"].setEnabled(has_selected_video)
self._buttons["delete instance"].setEnabled(has_selected_instance)
Expand Down
28 changes: 28 additions & 0 deletions sleap/gui/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@ def gotoVideoAndFrame(self, video: Video, frame_idx: int):

# Editing Commands

def toggleGrayscale(self):
"""Toggles grayscale setting for current video."""
self.execute(ToggleGrayscale)

def addVideo(self):
"""Shows gui for adding videos to project."""
self.execute(AddVideo)
Expand Down Expand Up @@ -1442,6 +1446,30 @@ class EditCommand(AppCommand):
does_edits = True


class ToggleGrayscale(EditCommand):
topics = [UpdateTopic.video, UpdateTopic.frame]

@staticmethod
def do_action(context: CommandContext, params: dict):
"""Reset the video backend."""
video: Video = context.state["video"]
try:
grayscale = video.backend.grayscale
video.backend.reset(grayscale=(not grayscale))
except:
print(
f"This video type {type(video.backend)} does not support grayscale yet."
)

@staticmethod
def ask(context: CommandContext, params: dict) -> bool:
"""Check that video can be reset."""
# Check that current video is set
if context.state["video"] is None:
return False
return True


class AddVideo(EditCommand):
topics = [UpdateTopic.video]

Expand Down
18 changes: 16 additions & 2 deletions sleap/io/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,23 @@ def dtype(self):
"""See :class:`Video`."""
return self.test_frame.dtype

def reset(self):
def reset(self, filename: str = None, grayscale: bool = None, bgr: bool = None):
"""Reloads the video."""
self._reader_ = None
if filename is not None:
self.filename = filename
self._test_frame_ = None # Test frame does not depend on num channels

if grayscale is not None:
self.grayscale = grayscale
self._detect_grayscale = False
else:
self._detect_grayscale = True

if bgr is not None:
self.bgr = bgr

if (filename is not None) or (grayscale is not None):
self._reader_ = None # Reader depends on both filename and grayscale

def get_frame(self, idx: int, grayscale: bool = None) -> np.ndarray:
"""See :class:`Video`."""
Expand Down

0 comments on commit 5d71030

Please sign in to comment.