Fix SAM3 io_utils to handle extensionless video files from OIL
Summary: Fix SAM3 io_utils.py to handle extensionless video files from OIL (Object Image Library). Previously, the code would immediately raise NotImplementedError when encountering files without recognized video extensions. This change attempts to load such files as videos using the video loader, only raising an exception if the actual decoding fails. This enables SAM3 to process OIL-stored video paths that don't include file extensions. Differential Revision: D99228861 fbshipit-source-id: c93d089ad4b7eac9e0eb629d7a71b021604db410
This commit is contained in:
committed by
meta-codesync[bot]
parent
bfbed072a0
commit
44ef224799
+17
-1
@@ -159,7 +159,23 @@ def load_video_frames(
|
|||||||
video_loader_type=video_loader_type,
|
video_loader_type=video_loader_type,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError("Only video files and image folders are supported")
|
# No recognized extension (e.g., extensionless OIL paths) — attempt video loading.
|
||||||
|
# Only raise if the loader itself fails to decode frames.
|
||||||
|
try:
|
||||||
|
return load_video_frames_from_video_file(
|
||||||
|
video_path=video_path,
|
||||||
|
image_size=image_size,
|
||||||
|
offload_video_to_cpu=offload_video_to_cpu,
|
||||||
|
img_mean=img_mean,
|
||||||
|
img_std=img_std,
|
||||||
|
async_loading_frames=async_loading_frames,
|
||||||
|
video_loader_type=video_loader_type,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
raise NotImplementedError(
|
||||||
|
f"Only video files and image folders are supported; "
|
||||||
|
f"failed to load '{video_path}' as video: {e}"
|
||||||
|
) from e
|
||||||
|
|
||||||
|
|
||||||
def load_video_frames_from_image_folder(
|
def load_video_frames_from_image_folder(
|
||||||
|
|||||||
@@ -0,0 +1,109 @@
|
|||||||
|
# Copyright (c) Meta Platforms, Inc. and affiliates. All Rights Reserved
|
||||||
|
|
||||||
|
"""Tests for io_utils extensionless video file handling (D99228861)."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
from sam3.model.io_utils import load_video_frames
|
||||||
|
|
||||||
|
|
||||||
|
class TestLoadVideoFramesRouting(unittest.TestCase):
|
||||||
|
"""Test that load_video_frames routes paths correctly based on extension."""
|
||||||
|
|
||||||
|
@patch("sam3.model.io_utils.load_video_frames_from_video_file")
|
||||||
|
def test_mp4_extension_routes_to_video_loader(self, mock_load_video):
|
||||||
|
"""Paths with .mp4 extension should route to load_video_frames_from_video_file."""
|
||||||
|
mock_load_video.return_value = ("frames", 480, 640)
|
||||||
|
result = load_video_frames(
|
||||||
|
video_path="/tmp/test_video.mp4",
|
||||||
|
image_size=256,
|
||||||
|
offload_video_to_cpu=True,
|
||||||
|
)
|
||||||
|
mock_load_video.assert_called_once()
|
||||||
|
self.assertEqual(result, ("frames", 480, 640))
|
||||||
|
|
||||||
|
@patch("sam3.model.io_utils.load_video_frames_from_video_file")
|
||||||
|
def test_mov_extension_routes_to_video_loader(self, mock_load_video):
|
||||||
|
"""Paths with .mov extension should route to load_video_frames_from_video_file."""
|
||||||
|
mock_load_video.return_value = ("frames", 480, 640)
|
||||||
|
load_video_frames(
|
||||||
|
video_path="/tmp/test_video.mov",
|
||||||
|
image_size=256,
|
||||||
|
offload_video_to_cpu=True,
|
||||||
|
)
|
||||||
|
mock_load_video.assert_called_once()
|
||||||
|
|
||||||
|
@patch("sam3.model.io_utils.load_video_frames_from_video_file")
|
||||||
|
def test_extensionless_oil_path_routes_to_video_loader(self, mock_load_video):
|
||||||
|
"""Extensionless OIL paths should attempt video loading (D99228861 fix)."""
|
||||||
|
mock_load_video.return_value = ("frames", 480, 640)
|
||||||
|
result = load_video_frames(
|
||||||
|
video_path="oil://fb_permanent/abc123def456",
|
||||||
|
image_size=256,
|
||||||
|
offload_video_to_cpu=True,
|
||||||
|
)
|
||||||
|
mock_load_video.assert_called_once()
|
||||||
|
self.assertEqual(result, ("frames", 480, 640))
|
||||||
|
|
||||||
|
@patch("sam3.model.io_utils.load_video_frames_from_video_file")
|
||||||
|
def test_extensionless_bare_hash_routes_to_video_loader(self, mock_load_video):
|
||||||
|
"""Bare hash paths without extension should attempt video loading."""
|
||||||
|
mock_load_video.return_value = ("frames", 480, 640)
|
||||||
|
result = load_video_frames(
|
||||||
|
video_path="/data/videos/abc123def456",
|
||||||
|
image_size=256,
|
||||||
|
offload_video_to_cpu=True,
|
||||||
|
)
|
||||||
|
mock_load_video.assert_called_once()
|
||||||
|
self.assertEqual(result, ("frames", 480, 640))
|
||||||
|
|
||||||
|
@patch("sam3.model.io_utils.load_video_frames_from_video_file")
|
||||||
|
def test_extensionless_path_raises_on_decode_failure(self, mock_load_video):
|
||||||
|
"""Extensionless path that fails to decode should raise NotImplementedError."""
|
||||||
|
mock_load_video.side_effect = RuntimeError("Could not decode video")
|
||||||
|
with self.assertRaises(NotImplementedError) as ctx:
|
||||||
|
load_video_frames(
|
||||||
|
video_path="oil://fb_permanent/corrupted_file",
|
||||||
|
image_size=256,
|
||||||
|
offload_video_to_cpu=True,
|
||||||
|
)
|
||||||
|
self.assertIn("failed to load", str(ctx.exception))
|
||||||
|
self.assertIn("oil://fb_permanent/corrupted_file", str(ctx.exception))
|
||||||
|
|
||||||
|
@patch("sam3.model.io_utils.load_video_frames_from_image_folder")
|
||||||
|
def test_directory_routes_to_image_folder_loader(self, mock_load_folder):
|
||||||
|
"""Directory paths should route to load_video_frames_from_image_folder."""
|
||||||
|
mock_load_folder.return_value = ("frames", 480, 640)
|
||||||
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
|
load_video_frames(
|
||||||
|
video_path=tmpdir,
|
||||||
|
image_size=256,
|
||||||
|
offload_video_to_cpu=True,
|
||||||
|
)
|
||||||
|
mock_load_folder.assert_called_once()
|
||||||
|
|
||||||
|
def test_dummy_video_pattern(self):
|
||||||
|
"""<load-dummy-video-N> pattern should return dummy frames."""
|
||||||
|
frames, h, w = load_video_frames(
|
||||||
|
video_path="<load-dummy-video-5>",
|
||||||
|
image_size=64,
|
||||||
|
offload_video_to_cpu=True,
|
||||||
|
)
|
||||||
|
self.assertEqual(frames.shape[0], 5) # 5 frames
|
||||||
|
self.assertEqual(h, 480)
|
||||||
|
self.assertEqual(w, 640)
|
||||||
|
|
||||||
|
@patch("sam3.model.io_utils.load_video_frames_from_video_file")
|
||||||
|
def test_unknown_extension_routes_to_video_loader(self, mock_load_video):
|
||||||
|
"""Paths with unrecognized extensions should attempt video loading."""
|
||||||
|
mock_load_video.return_value = ("frames", 480, 640)
|
||||||
|
result = load_video_frames(
|
||||||
|
video_path="/tmp/video.xyz",
|
||||||
|
image_size=256,
|
||||||
|
offload_video_to_cpu=True,
|
||||||
|
)
|
||||||
|
mock_load_video.assert_called_once()
|
||||||
|
self.assertEqual(result, ("frames", 480, 640))
|
||||||
Reference in New Issue
Block a user