From f6e51f59500a87c576c2df2323ce56b9fd7a12de Mon Sep 17 00:00:00 2001 From: Haoran Wang Date: Tue, 24 Feb 2026 10:28:32 -0800 Subject: [PATCH] Add empty-frames guard to cv2 video loader MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: ## Context When cv2.VideoCapture opens a video but cannot decode any frames (e.g., due to an unsupported codec), the frames list is empty and np.stack crashes with "need at least one array to stack" — an unhelpful error message that obscures the real problem. ## This Diff - Add explicit check for empty frames list after cap.release() and before np.stack - Raise RuntimeError with descriptive message about possible causes (corrupted file, empty video, or unsupported codec) Differential Revision: D94142839 fbshipit-source-id: 89a973d067d83439039734712d18727b06d0706d --- sam3/model/io_utils.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sam3/model/io_utils.py b/sam3/model/io_utils.py index 067f125..9d64c9c 100644 --- a/sam3/model/io_utils.py +++ b/sam3/model/io_utils.py @@ -300,6 +300,12 @@ def load_video_frames_from_video_file_using_cv2( cap.release() pbar.close() + if len(frames) == 0: + raise RuntimeError( + f"No frames could be decoded from video: {video_path}. " + f"The file may be corrupted, empty, or encoded with an unsupported codec." + ) + # Convert to tensor frames_np = np.stack(frames, axis=0).astype(np.float32) # (T, H, W, C) video_tensor = torch.from_numpy(frames_np).permute(0, 3, 1, 2) # (T, C, H, W)