Skip to content

Commit

Permalink
Add more checks for video seeking/reading failure (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
talmo authored Nov 1, 2024
1 parent 780deb0 commit 50757c2
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion sleap_io/io/video_reading.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,16 +397,21 @@ def _read_frame(self, frame_idx: int) -> np.ndarray:
This does not apply grayscale conversion. It is recommended to use the
`get_frame` method of the `VideoBackend` class instead.
"""
failed = False
if self.plugin == "opencv":
if self.reader.get(cv2.CAP_PROP_POS_FRAMES) != frame_idx:
self.reader.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)
_, img = self.reader.read()
success, img = self.reader.read()
elif self.plugin == "pyav" or self.plugin == "FFMPEG":
if self.keep_open:
img = self.reader.read(index=frame_idx)
else:
with iio.imopen(self.filename, "r", plugin=self.plugin) as reader:
img = reader.read(index=frame_idx)

success = (not failed) and (img is not None)
if not success:
raise IndexError(f"Failed to read frame index {frame_idx}.")
return img

def _read_frames(self, frame_inds: list) -> np.ndarray:
Expand Down

0 comments on commit 50757c2

Please sign in to comment.