diff --git a/sam3/train/data/sam3_image_dataset.py b/sam3/train/data/sam3_image_dataset.py index 97efb1d1..7d4c8a98 100644 --- a/sam3/train/data/sam3_image_dataset.py +++ b/sam3/train/data/sam3_image_dataset.py @@ -15,9 +15,13 @@ import torch import torch.utils.data import torchvision -from decord import cpu, VideoReader from iopath.common.file_io import g_pathmgr +# Lazy import of decord to avoid conflicts with OpenCV +# Decord will only be imported when actually needed in _load_images +# This prevents decord from loading when just importing sam3, which would +# cause cv2.imshow() to segfault due to library conflicts + from PIL import Image as PILImage from PIL.Image import DecompressionBombError @@ -202,6 +206,14 @@ def _load_images( try: if ".mp4" in path and path[-4:] == ".mp4": # Going to load a video frame + # Lazy import decord only when needed to avoid conflicts with OpenCV + try: + from decord import cpu, VideoReader + except ImportError: + raise ImportError( + "decord is required for video loading but is not installed. " + "Install it with: pip install decord or pip install -e '.[notebooks]'" + ) video_path, frame = path.split("@") video = VideoReader(video_path, ctx=cpu(0)) # Convert to PIL image