Skip to content

Commit

Permalink
Add "dummy" option to VideoLoader.
Browse files Browse the repository at this point in the history
Returns dummy frames if set; used when we don't need frame images
in the prediction pipeline.
  • Loading branch information
ntabris committed Jan 22, 2020
1 parent 8604601 commit 8c90746
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions sleap/nn/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ class VideoLoader:
dataset: str = None
input_format: str = None
grayscale: bool = False
dummy: bool = False
chunk_size: int = 32
prefetch_chunks: int = 1
frame_inds: Optional[List[int]] = None
Expand Down Expand Up @@ -367,8 +368,12 @@ def _load_video(self, filename) -> "Video":
)

def load_frames(self, frame_inds):
local_vid = self._load_video(self.video.filename)
imgs = local_vid[np.array(frame_inds).astype("int64")]
if self.dummy:
dummy_shape = (len(frame_inds), *self._shape[1:])
imgs = np.zeros(dummy_shape, dtype="int8")
else:
local_vid = self._load_video(self.video.filename)
imgs = local_vid[np.array(frame_inds).astype("int64")]
return imgs

def tf_load_frames(self, frame_inds):
Expand Down
9 changes: 9 additions & 0 deletions tests/nn/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from sleap.nn.utils import VideoLoader
import numpy as np


def test_grayscale_video():
Expand All @@ -7,3 +8,11 @@ def test_grayscale_video():

vid = VideoLoader(filename="tests/data/videos/small_robot.mp4", grayscale=True)
assert vid.shape[-1] == 1


def test_dummy_video():
vid = VideoLoader(filename="tests/data/videos/small_robot.mp4", dummy=True)

x = vid.load_frames([1, 3, 5])
assert x.shape == (3, 320, 560, 3)
assert np.all(x == 0)

0 comments on commit 8c90746

Please sign in to comment.