Object tracking using Grounding DINO for text-based detection and SAMURAI (SAM2 with motion-aware memory) for video segmentation.
- Text-based detection: Describe what you want to track in natural language
- Motion-aware tracking: SAMURAI uses Kalman filtering for robust tracking through occlusions
- Batch processing: Process entire video directories automatically
- Periodic re-detection: Catches new objects entering the scene
- NVIDIA GPU with CUDA support (recommended)
- Anaconda or Miniconda
conda create -n samurai_tracker python=3.10 -y
conda activate samurai_trackerpip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121pip install opencv-python matplotlib tqdm transformers huggingface_hub supervision hydra-core iopath logurugit clone https://github.com/yangchris11/samurai.git
cd samurai/sam2
pip install -e .
cd ../..Linux/Mac:
cd samurai/sam2/checkpoints
./download_ckpts.sh
cd ../../..Windows (PowerShell):
cd samurai/sam2/checkpoints
Invoke-WebRequest -Uri "https://dl.fbaipublicfiles.com/segment_anything_2/092824/sam2.1_hiera_base_plus.pt" -OutFile "sam2.1_hiera_base_plus.pt"
cd ../../..Note: Only
sam2.1_hiera_base_plus.ptis required. The Linux script downloads all sizes but you only need this one.
python samurai_tracker.py --input_dir path/to/frames --text_prompt "fish"python batch_samurai_videos.py \
--input_root /path/to/videos \
--output_root /path/to/output \
--text_prompt "turtle"| Argument | Description | Default |
|---|---|---|
--input_dir |
Directory containing input frames | Required |
--text_prompt |
Text description of object(s) to track | Required |
--output_dir |
Directory to save outputs | output |
--models_dir |
Directory to cache model weights | models |
--box_threshold |
Detection confidence threshold | 0.30 |
--text_threshold |
Text matching threshold | 0.25 |
--max_image_size |
Maximum image dimension | 1280 |
--redetect_interval |
Re-run detection every N frames | 30 |
--device |
Device to use (cuda/cpu) | Auto-detect |
--no-samurai |
Disable SAMURAI, use vanilla SAM2 | Enabled |
By default, SAMURAI mode is enabled which uses Kalman filtering for motion-aware tracking. This is better for tracking through occlusions but tracks one object at a time when multiple are detected.
Use --no-samurai to disable SAMURAI mode and use vanilla SAM2:
- Faster when tracking multiple objects simultaneously
- No motion prediction (may lose objects through occlusions)
# SAMURAI mode (default) - better tracking, slower with multiple objects
python samurai_tracker.py --input_dir frames/ --text_prompt "fish"
# Vanilla SAM2 - faster multi-object, no motion prediction
python samurai_tracker.py --no-samurai --input_dir frames/ --text_prompt "fish"- Use periods to separate multiple objects:
"fish. turtle. dolphin" - Try variations if detection fails:
"fish"vs"a fish" - Lower
--box_thresholdto0.15if objects are missed
output/
├── visualizations/ # Annotated frames with masks
│ ├── vis_000000.jpg
│ └── ...
├── tracked_output.mp4 # Final output video
└── tracking_metadata.json # Object IDs per detection interval
CUDA Out of Memory: Reduce --max_image_size to 720 or use --device cpu
No Objects Detected: Lower --box_threshold to 0.15-0.20
Import Errors: Ensure environment is active and SAMURAI installed:
conda activate samurai_tracker
cd samurai/sam2 && pip install -e .- Grounding DINO - IDEA Research
- SAMURAI - University of Washington
- SAM2 - Meta AI