Fix: Make decord import lazy to prevent cv2.imshow() segfault #273
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix: Make decord import lazy to prevent cv2.imshow() segfault
fixed #228
Problem
When importing the sam3 library, calling
cv2.imshow()throws a segmentation fault, even without calling any sam3 interface.Example that causes the issue:
Root Cause
The issue is in
sam3/train/data/sam3_image_dataset.pywhich has a module-level import ofdecord:When
sam3_image_dataset.pyis imported (which happens when importingsam3.model_builder), thedecordimport is executed immediately at module load time. This causes a conflict with OpenCV's initialization, leading to a segmentation fault whencv2.imshow()is called.Solution
Make the
decordimport lazy - only import it when actually needed (inside the function that uses it), not at module level.Changes
Removed module-level import (line 18):
from decord import cpu, VideoReader(at module level)Added lazy import inside
_load_images()method:.mp4filesCode Changes
Impact
Testing
The fix ensures:
cv2.imshow()works normally after importing sam3Related Issues
This fixes the issue reported where importing sam3 causes cv2.imshow() to segfault.