Skip to content

Commit

Permalink
Merge branch 'talmolab:main' into kloding/add_video_fps_data
Browse files Browse the repository at this point in the history
  • Loading branch information
keyaloding authored Jul 22, 2024
2 parents c8e8ab0 + 5b79a22 commit 8a1ef98
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
12 changes: 9 additions & 3 deletions sleap_io/io/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,13 @@ def num_frames(self) -> int:
@property
def img_shape(self) -> Tuple[int, int, int]:
"""Shape of a single frame in the video."""
height, width, channels = self.get_frame(0).shape
height, width, channels = self.read_test_frame().shape
if self.grayscale is None:
self.detect_grayscale()
if self.grayscale is False:
channels = 3
elif self.grayscale is True:
channels = 1
return int(height), int(width), int(channels)

@property
Expand Down Expand Up @@ -593,7 +599,7 @@ def read_test_frame(self) -> np.ndarray:
frame_idx = list(self.frame_map.keys())[0]
else:
frame_idx = 0
return self.read_frame(frame_idx)
return self._read_frame(frame_idx)

@property
def has_embedded_images(self) -> bool:
Expand Down Expand Up @@ -714,7 +720,7 @@ class ImageVideo(VideoBackend):
This backend supports reading videos stored as a list of images.
Attributes:
filename: Path to video files.
filename: Path to image files.
grayscale: Whether to force grayscale. If None, autodetect on first frame load.
"""

Expand Down
13 changes: 10 additions & 3 deletions sleap_io/model/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,16 @@ def grayscale(self) -> bool | None:
if shape is not None:
return shape[-1] == 1
else:
if "grayscale" in self.backend_metadata:
return self.backend_metadata["grayscale"]
return None
return self.backend_metadata.get("grayscale", None)

@grayscale.setter
def grayscale(self, value: bool):
"""Set the grayscale value and adjust the backend."""
if self.backend is not None:
self.backend.grayscale = value
self.backend._cached_shape = None

self.backend_metadata["grayscale"] = value

def __len__(self) -> int:
"""Return the length of the video as the number of frames."""
Expand Down
2 changes: 1 addition & 1 deletion sleap_io/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

# Define package version.
# This is read dynamically by setuptools in pyproject.toml to determine the release version.
__version__ = "0.1.5"
__version__ = "0.1.6"
20 changes: 20 additions & 0 deletions tests/model/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,23 @@ def test_video_replace_filename(
video.replace_filename(centered_pair_frame_paths)
assert type(video.backend) == ImageVideo
assert video.exists(check_all=True) is True


def test_grayscale(centered_pair_low_quality_path):
video = Video.from_filename(centered_pair_low_quality_path)
assert video.grayscale == True
assert video.shape[-1] == 1

video.grayscale = False
assert video.shape[-1] == 3

video.close()
video.open()
assert video.grayscale == False
assert video.shape[-1] == 3

video.grayscale = True
video.close()
video.open()
assert video.grayscale == True
assert video.shape[-1] == 1

0 comments on commit 8a1ef98

Please sign in to comment.