Skip to content

base

sleap.gui.overlays.base

Base classes for overlays.

Overlays are used for showing additional visuals on top of a video frame (i.e., a QtVideoPlayer object). Overlay objects are created in the main GUI app, which then automatically calls the add_to_scene for each loaded overlay after drawing a frame (i.e., when user navigates to a new frame or something changes so that current frame must be redrawn).

Classes:

Name Description
BaseOverlay

Abstract base class for overlays.

BaseOverlay

Bases: ABC

Abstract base class for overlays.

Most overlays need rely on the Labels from which to get data and need the QtVideoPlayer to which a QGraphicsItem will be added, so these attributes are included in the base class.

Parameters:

Name Type Description Default
labels

the Labels from which to get data

required
player

the QtVideoPlayer to which a QGraphicsObject item will be added

required
items

stores all QGraphicsItems currently added to the player from this overlay

required

Methods:

Name Description
add_to_scene

Add items to scene.

redraw

Remove all items from the scene before adding new items to the scene.

remove_from_scene

Remove all items added to scene by this overlay.

Source code in sleap/gui/overlays/base.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
@attr.s(auto_attribs=True)
class BaseOverlay(abc.ABC):
    """Abstract base class for overlays.

    Most overlays need rely on the `Labels` from which to get data and need the
    `QtVideoPlayer` to which a `QGraphicsItem` will be added, so these
    attributes are included in the base class.

    Args:
        labels: the `Labels` from which to get data
        player: the `QtVideoPlayer` to which a `QGraphicsObject` item will be added
        items: stores all `QGraphicsItem`s currently added to the player from this
            overlay
    """

    labels: Labels | None = None
    player: QtVideoPlayer | None = None
    items: list[QGraphicsItem] | None = None

    @abc.abstractmethod
    def add_to_scene(self, video: Video, frame_idx: int):
        """Add items to scene.

        To use the `remove_from_scene` and `redraw` methods, keep track of a list of
        `QGraphicsItem`s added in this function.
        """
        # Start your method with:
        self.items = []

        # As items are added to the `QtVideoPlayer`, keep track of these items:
        item = self.player.scene.addItem(...)
        self.items.append(item)

    def remove_from_scene(self):
        """Remove all items added to scene by this overlay.

        This method does not need to be called when changing the plot to a new frame.
        """
        if self.items is None:
            return
        for item in self.items:
            try:
                self.player.scene.removeItem(item)

            except RuntimeError as e:
                # Internal C++ object (PySide2.QtWidgets.QGraphicsPathItem) already
                # deleted.
                logger.debug(e)

        # Stop tracking the items after they been removed from the scene
        self.items = []

    def redraw(self, video, frame_idx, *args, **kwargs):
        """Remove all items from the scene before adding new items to the scene.

        This method does not need to be called when changing the plot to a new frame.
        """
        self.remove_from_scene(*args, **kwargs)
        self.add_to_scene(video, frame_idx, *args, **kwargs)

add_to_scene(video, frame_idx) abstractmethod

Add items to scene.

To use the remove_from_scene and redraw methods, keep track of a list of QGraphicsItems added in this function.

Source code in sleap/gui/overlays/base.py
44
45
46
47
48
49
50
51
52
53
54
55
56
@abc.abstractmethod
def add_to_scene(self, video: Video, frame_idx: int):
    """Add items to scene.

    To use the `remove_from_scene` and `redraw` methods, keep track of a list of
    `QGraphicsItem`s added in this function.
    """
    # Start your method with:
    self.items = []

    # As items are added to the `QtVideoPlayer`, keep track of these items:
    item = self.player.scene.addItem(...)
    self.items.append(item)

redraw(video, frame_idx, *args, **kwargs)

Remove all items from the scene before adding new items to the scene.

This method does not need to be called when changing the plot to a new frame.

Source code in sleap/gui/overlays/base.py
77
78
79
80
81
82
83
def redraw(self, video, frame_idx, *args, **kwargs):
    """Remove all items from the scene before adding new items to the scene.

    This method does not need to be called when changing the plot to a new frame.
    """
    self.remove_from_scene(*args, **kwargs)
    self.add_to_scene(video, frame_idx, *args, **kwargs)

remove_from_scene()

Remove all items added to scene by this overlay.

This method does not need to be called when changing the plot to a new frame.

Source code in sleap/gui/overlays/base.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def remove_from_scene(self):
    """Remove all items added to scene by this overlay.

    This method does not need to be called when changing the plot to a new frame.
    """
    if self.items is None:
        return
    for item in self.items:
        try:
            self.player.scene.removeItem(item)

        except RuntimeError as e:
            # Internal C++ object (PySide2.QtWidgets.QGraphicsPathItem) already
            # deleted.
            logger.debug(e)

    # Stop tracking the items after they been removed from the scene
    self.items = []