Spaces:
Running
on
L40S
Running
on
L40S
Commit
·
9f37699
0
Parent(s):
SAM3 Video Segmentation - Clean deployment
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +4 -0
- .gitignore +54 -0
- LICENSE +2 -0
- README.md +100 -0
- app.py.encrypted +0 -0
- app_loader.py +89 -0
- assets/bpe_simple_vocab_16e6.txt.gz +3 -0
- assets/dog.gif +3 -0
- assets/images/groceries.jpg +3 -0
- assets/images/test_image.jpg +3 -0
- assets/images/truck.jpg +3 -0
- assets/model_diagram.png +3 -0
- assets/player.gif +3 -0
- assets/sa_co_dataset.jpg +3 -0
- assets/saco_gold_annotation.png +3 -0
- assets/veval/toy_gt_and_pred/toy_saco_veval_sav_test_eval_res.json +3 -0
- assets/veval/toy_gt_and_pred/toy_saco_veval_sav_test_gt.json +3 -0
- assets/veval/toy_gt_and_pred/toy_saco_veval_sav_test_pred.json +3 -0
- assets/videos/0001/0.jpg +3 -0
- assets/videos/0001/1.jpg +3 -0
- assets/videos/0001/10.jpg +3 -0
- assets/videos/0001/100.jpg +3 -0
- assets/videos/0001/101.jpg +3 -0
- assets/videos/0001/102.jpg +3 -0
- assets/videos/0001/103.jpg +3 -0
- assets/videos/0001/104.jpg +3 -0
- assets/videos/0001/105.jpg +3 -0
- assets/videos/0001/106.jpg +3 -0
- assets/videos/0001/107.jpg +3 -0
- assets/videos/0001/108.jpg +3 -0
- assets/videos/0001/109.jpg +3 -0
- assets/videos/0001/11.jpg +3 -0
- assets/videos/0001/110.jpg +3 -0
- assets/videos/0001/111.jpg +3 -0
- assets/videos/0001/112.jpg +3 -0
- assets/videos/0001/113.jpg +3 -0
- assets/videos/0001/114.jpg +3 -0
- assets/videos/0001/115.jpg +3 -0
- assets/videos/0001/116.jpg +3 -0
- assets/videos/0001/117.jpg +3 -0
- assets/videos/0001/118.jpg +3 -0
- assets/videos/0001/119.jpg +3 -0
- assets/videos/0001/12.jpg +3 -0
- assets/videos/0001/120.jpg +3 -0
- assets/videos/0001/121.jpg +3 -0
- assets/videos/0001/122.jpg +3 -0
- assets/videos/0001/123.jpg +3 -0
- assets/videos/0001/124.jpg +3 -0
- assets/videos/0001/125.jpg +3 -0
- assets/videos/0001/126.jpg +3 -0
.gitattributes
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
|
| 3 |
+
sam3/perflib/tests/assets/masks.tiff filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
assets/** filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
.DS_Store
|
| 3 |
+
example_sam3_project_local/
|
| 4 |
+
*.py[cod]
|
| 5 |
+
*$py.class
|
| 6 |
+
*.so
|
| 7 |
+
.Python
|
| 8 |
+
env/
|
| 9 |
+
venv/
|
| 10 |
+
ENV/
|
| 11 |
+
*.egg-info/
|
| 12 |
+
.eggs/
|
| 13 |
+
dist/
|
| 14 |
+
build/
|
| 15 |
+
|
| 16 |
+
# Source code protection
|
| 17 |
+
# app.py is tracked in GitHub (private), but excluded in HuggingFace deployments
|
| 18 |
+
# Use 'push-hf.sh' script to deploy to HuggingFace without app.py
|
| 19 |
+
encrypt_code.py
|
| 20 |
+
setup_hooks.sh
|
| 21 |
+
cookie_util.py
|
| 22 |
+
|
| 23 |
+
# Gradio
|
| 24 |
+
flagged/
|
| 25 |
+
gradio_cached_examples/
|
| 26 |
+
|
| 27 |
+
# Outputs
|
| 28 |
+
*.mp4
|
| 29 |
+
!bedroom.mp4
|
| 30 |
+
!football.mp4
|
| 31 |
+
!openfield_5mice_3s.mp4
|
| 32 |
+
!openfield_5mice_5min.mp4
|
| 33 |
+
!openfield_5mice_6min.mp4
|
| 34 |
+
!openfield_5mice_7min.mp4
|
| 35 |
+
!examples/*.mp4
|
| 36 |
+
|
| 37 |
+
# Model checkpoints
|
| 38 |
+
*.pth
|
| 39 |
+
*.ckpt
|
| 40 |
+
checkpoints/
|
| 41 |
+
|
| 42 |
+
# Logs
|
| 43 |
+
*.log
|
| 44 |
+
|
| 45 |
+
# IDE/editor
|
| 46 |
+
.vscode/
|
| 47 |
+
.idea/
|
| 48 |
+
*.swp
|
| 49 |
+
*.swo
|
| 50 |
+
|
| 51 |
+
# OS
|
| 52 |
+
.DS_Store
|
| 53 |
+
Thumbs.db
|
| 54 |
+
.env
|
LICENSE
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Unauthorized use of this code is prohibited.\n
|
| 2 |
+
For inquiries regarding permissions, please send email to : bellmake@naver.com
|
README.md
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: SAM3 Video Segmentation and Tracking with Text Prompt
|
| 3 |
+
emoji: 🎬
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.9.1
|
| 8 |
+
app_file: app_loader.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# 🎬 SAM3 Video Segmentation and Tracking with Text Prompt
|
| 13 |
+
|
| 14 |
+
A HuggingFace Spaces app that automatically segments and tracks objects in videos using text prompts.
|
| 15 |
+
|
| 16 |
+
## ✨ Key Features
|
| 17 |
+
|
| 18 |
+
- **Text-driven segmentation**: Describe the target object in natural language
|
| 19 |
+
- **Multiple objects**: Segment several objects at once by separating them with commas
|
| 20 |
+
- **Temporal consistency**: Tracks objects across frames for stable masks
|
| 21 |
+
- **Distinct colors**: Assigns unique colors for each object
|
| 22 |
+
- **Live progress**: Watch processing updates in real time
|
| 23 |
+
- **High-quality output**: Produces MP4 encoded with H.264 for broad compatibility
|
| 24 |
+
|
| 25 |
+
## 🚀 How to Use
|
| 26 |
+
|
| 27 |
+
1. **Upload a video**: Provide an MP4 file
|
| 28 |
+
2. **Enter prompts**: Type the objects to segment
|
| 29 |
+
- Single object: `bed`, `person`, `car`, `dog`, etc.
|
| 30 |
+
- Multiple objects: `bed, pillow, person` (comma-separated)
|
| 31 |
+
3. **Run**: Click the "🚀 Run Segmentation" button
|
| 32 |
+
4. **Review results**: Preview and download the segmented video
|
| 33 |
+
|
| 34 |
+
## 📋 Examples
|
| 35 |
+
|
| 36 |
+
### Single-object segmentation
|
| 37 |
+
```
|
| 38 |
+
Prompt: bed
|
| 39 |
+
Result: Only the bed is segmented and highlighted
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
### Multi-object segmentation
|
| 43 |
+
```
|
| 44 |
+
Prompt: bed, pillow, person
|
| 45 |
+
Result: Bed, pillow, and person are segmented with unique colors
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
## 🛠️ Tech Stack
|
| 49 |
+
|
| 50 |
+
- **Model**: SAM3 (Segment Anything Model 3)
|
| 51 |
+
- **Framework**: Gradio
|
| 52 |
+
- **Video processing**: OpenCV, FFmpeg
|
| 53 |
+
- **Deep learning**: PyTorch
|
| 54 |
+
|
| 55 |
+
## 📊 Performance & Limitations
|
| 56 |
+
|
| 57 |
+
- **Processing time**: Scales with video length and resolution
|
| 58 |
+
- **Multiple objects**: More objects increase runtime (roughly N objects ≈ N× time)
|
| 59 |
+
- **GPU requirement**: GPU needed to run SAM3 efficiently
|
| 60 |
+
- **Memory**: High-resolution videos require more memory
|
| 61 |
+
|
| 62 |
+
## 🔧 Run Locally
|
| 63 |
+
|
| 64 |
+
```bash
|
| 65 |
+
# Clone the space
|
| 66 |
+
git clone https://huggingface.co/spaces/bellmake/SAM3-video-segmentation-tracking
|
| 67 |
+
cd SAM3-video-segmentation-tracking
|
| 68 |
+
|
| 69 |
+
# Install dependencies
|
| 70 |
+
pip install -r requirements.txt
|
| 71 |
+
|
| 72 |
+
# Start the app
|
| 73 |
+
python app.py
|
| 74 |
+
```
|
| 75 |
+
|
| 76 |
+
## 📁 Project Structure
|
| 77 |
+
|
| 78 |
+
```
|
| 79 |
+
.
|
| 80 |
+
├── app.py # Main Gradio app
|
| 81 |
+
├── requirements.txt # Python dependencies
|
| 82 |
+
├── packages.txt # System packages (FFmpeg)
|
| 83 |
+
├── bedroom.mp4 # Sample video
|
| 84 |
+
├── example_sam3_project_local/ # Local SAM3 project
|
| 85 |
+
└── README.md # This file
|
| 86 |
+
```
|
| 87 |
+
|
| 88 |
+
## 🤝 Contributing
|
| 89 |
+
|
| 90 |
+
Issues and pull requests are welcome!
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
## 📄 License
|
| 94 |
+
|
| 95 |
+
Unauthorized use of this code is prohibited.\n
|
| 96 |
+
For inquiries regarding permissions, please send email to : bellmake@naver.com
|
| 97 |
+
|
| 98 |
+
---
|
| 99 |
+
|
| 100 |
+
**Powered by SAM3 & HuggingFace Spaces**
|
app.py.encrypted
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app_loader.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Encrypted Code Loader - Hugging Face Space Entry Point
|
| 4 |
+
|
| 5 |
+
This file decrypts and executes the encrypted app.py.
|
| 6 |
+
DECRYPT_KEY must be stored in Hugging Face Space Secrets.
|
| 7 |
+
"""
|
| 8 |
+
|
| 9 |
+
import os
|
| 10 |
+
import sys
|
| 11 |
+
from cryptography.fernet import Fernet
|
| 12 |
+
|
| 13 |
+
# List of files to decrypt
|
| 14 |
+
ENCRYPTED_FILES = {
|
| 15 |
+
"app.py.encrypted": "app.py",
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def decrypt_file(encrypted_path, decrypted_path, key):
|
| 20 |
+
"""Decrypts an encrypted file."""
|
| 21 |
+
cipher = Fernet(key)
|
| 22 |
+
|
| 23 |
+
# Read encrypted file
|
| 24 |
+
with open(encrypted_path, 'rb') as f:
|
| 25 |
+
encrypted_data = f.read()
|
| 26 |
+
|
| 27 |
+
# Decrypt
|
| 28 |
+
try:
|
| 29 |
+
decrypted_data = cipher.decrypt(encrypted_data)
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"❌ Decryption failed for {encrypted_path}: {e}")
|
| 32 |
+
print(" Please check that DECRYPT_KEY is correctly set in Space Secrets.")
|
| 33 |
+
sys.exit(1)
|
| 34 |
+
|
| 35 |
+
# Save decrypted file to temporary location (current directory in this case)
|
| 36 |
+
with open(decrypted_path, 'wb') as f:
|
| 37 |
+
f.write(decrypted_data)
|
| 38 |
+
|
| 39 |
+
print(f"✓ Decrypted: {encrypted_path} → {decrypted_path} ({len(decrypted_data):,} bytes)")
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def main():
|
| 43 |
+
print("=" * 60)
|
| 44 |
+
print("🔓 Decrypting source code...")
|
| 45 |
+
print("=" * 60)
|
| 46 |
+
|
| 47 |
+
# Get decryption key from Secrets
|
| 48 |
+
key_str = os.getenv("DECRYPT_KEY")
|
| 49 |
+
if not key_str:
|
| 50 |
+
print("❌ ERROR: DECRYPT_KEY not found in environment variables!")
|
| 51 |
+
print(" Please set DECRYPT_KEY in Hugging Face Space Secrets.")
|
| 52 |
+
print(" Go to: Settings → Variables and secrets → Add secret")
|
| 53 |
+
sys.exit(1)
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
key = key_str.encode('utf-8')
|
| 57 |
+
cipher = Fernet(key) # Validate key
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f"❌ Invalid DECRYPT_KEY: {e}")
|
| 60 |
+
sys.exit(1)
|
| 61 |
+
|
| 62 |
+
print(f"✓ DECRYPT_KEY loaded from secrets")
|
| 63 |
+
print()
|
| 64 |
+
|
| 65 |
+
# Decrypt files
|
| 66 |
+
for encrypted_file, decrypted_file in ENCRYPTED_FILES.items():
|
| 67 |
+
if not os.path.exists(encrypted_file):
|
| 68 |
+
print(f"⚠️ Warning: {encrypted_file} not found, skipping...")
|
| 69 |
+
continue
|
| 70 |
+
decrypt_file(encrypted_file, decrypted_file, key)
|
| 71 |
+
|
| 72 |
+
print()
|
| 73 |
+
print("=" * 60)
|
| 74 |
+
print("✅ Decryption complete! Starting application...")
|
| 75 |
+
print("=" * 60)
|
| 76 |
+
print()
|
| 77 |
+
|
| 78 |
+
# Execute decrypted app.py
|
| 79 |
+
# app.py automatically launches the Gradio app upon import
|
| 80 |
+
import app
|
| 81 |
+
|
| 82 |
+
# Explicitly launch if a demo object exists
|
| 83 |
+
if hasattr(app, 'demo'):
|
| 84 |
+
print("✓ Launching Gradio app...")
|
| 85 |
+
app.demo.launch()
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
main()
|
assets/bpe_simple_vocab_16e6.txt.gz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:924691ac288e54409236115652ad4aa250f48203de50a9e4722a6ecd48d6804a
|
| 3 |
+
size 1356917
|
assets/dog.gif
ADDED
|
|
Git LFS Details
|
assets/images/groceries.jpg
ADDED
|
|
Git LFS Details
|
assets/images/test_image.jpg
ADDED
|
|
Git LFS Details
|
assets/images/truck.jpg
ADDED
|
|
Git LFS Details
|
assets/model_diagram.png
ADDED
|
|
Git LFS Details
|
assets/player.gif
ADDED
|
|
Git LFS Details
|
assets/sa_co_dataset.jpg
ADDED
|
|
Git LFS Details
|
assets/saco_gold_annotation.png
ADDED
|
|
Git LFS Details
|
assets/veval/toy_gt_and_pred/toy_saco_veval_sav_test_eval_res.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d491596003e0dd7742705ea17ab84375e468d3bca3464b831d9d504e550ff8e8
|
| 3 |
+
size 12166
|
assets/veval/toy_gt_and_pred/toy_saco_veval_sav_test_gt.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4a1a964427d4e4225f883ed3d08ffa1d3613c45ac35dcc9426f2a25031c92a31
|
| 3 |
+
size 3398949
|
assets/veval/toy_gt_and_pred/toy_saco_veval_sav_test_pred.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c593f97bb4701a8c34df38bfebaf9091c6ba4f056860a789c38877be6403652e
|
| 3 |
+
size 542939
|
assets/videos/0001/0.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/1.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/10.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/100.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/101.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/102.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/103.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/104.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/105.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/106.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/107.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/108.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/109.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/11.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/110.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/111.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/112.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/113.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/114.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/115.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/116.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/117.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/118.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/119.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/12.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/120.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/121.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/122.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/123.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/124.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/125.jpg
ADDED
|
|
Git LFS Details
|
assets/videos/0001/126.jpg
ADDED
|
|
Git LFS Details
|