|
| 1 | +# Moondream Plugin |
| 2 | + |
| 3 | +This plugin provides Moondream 3 detection capabilities for vision-agents, enabling real-time zero-shot object detection on video streams. Choose between cloud-hosted or local processing depending on your needs. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +uv add vision-agents-plugins-moondream |
| 9 | +``` |
| 10 | + |
| 11 | +## Choosing the Right Processor |
| 12 | + |
| 13 | +### CloudDetectionProcessor (Recommended for Most Users) |
| 14 | +- **Use when:** You want a simple setup with no infrastructure management |
| 15 | +- **Pros:** No model download, no GPU required, automatic updates |
| 16 | +- **Cons:** Requires API key, 2 RPS rate limit by default (can be increased) |
| 17 | +- **Best for:** Development, testing, low-to-medium volume applications |
| 18 | + |
| 19 | +### LocalDetectionProcessor (For Advanced Users) |
| 20 | +- **Use when:** You need higher throughput, have your own GPU infrastructure, or want to avoid rate limits |
| 21 | +- **Pros:** No rate limits, no API costs, full control over hardware |
| 22 | +- **Cons:** Requires GPU for best performance, model download on first use, infrastructure management |
| 23 | +- **Best for:** Production deployments, high-volume applications, Digital Ocean Gradient AI GPUs, or custom infrastructure |
| 24 | + |
| 25 | +## Quick Start |
| 26 | + |
| 27 | +### Using CloudDetectionProcessor (Hosted) |
| 28 | + |
| 29 | +The `CloudDetectionProcessor` uses Moondream's hosted API. By default it has a 2 RPS (requests per second) rate limit and requires an API key. The rate limit can be adjusted by contacting the Moondream team to request a higher limit. |
| 30 | + |
| 31 | +```python |
| 32 | +from vision_agents.plugins import moondream |
| 33 | +from vision_agents.core import Agent |
| 34 | + |
| 35 | +# Create a cloud processor with detection |
| 36 | +processor = moondream.CloudDetectionProcessor( |
| 37 | + api_key="your-api-key", # or set MOONDREAM_API_KEY env var |
| 38 | + detect_objects="person", # or ["person", "car", "dog"] for multiple |
| 39 | + fps=30 |
| 40 | +) |
| 41 | + |
| 42 | +# Use in an agent |
| 43 | +agent = Agent( |
| 44 | + processors=[processor], |
| 45 | + llm=your_llm, |
| 46 | + # ... other components |
| 47 | +) |
| 48 | +``` |
| 49 | + |
| 50 | +### Using LocalDetectionProcessor (On-Device) |
| 51 | + |
| 52 | +If you are running on your own infrastructure or using a service like Digital Ocean's Gradient AI GPUs, you can use the `LocalDetectionProcessor` which downloads the model from HuggingFace and runs on device. By default it will use CUDA for best performance. Performance will vary depending on your specific hardware configuration. |
| 53 | + |
| 54 | +**Note:** The moondream3-preview model is gated and requires HuggingFace authentication: |
| 55 | +- Request access at https://huggingface.co/moondream/moondream3-preview |
| 56 | +- Set `HF_TOKEN` environment variable: `export HF_TOKEN=your_token_here` |
| 57 | +- Or run: `huggingface-cli login` |
| 58 | + |
| 59 | +```python |
| 60 | +from vision_agents.plugins import moondream |
| 61 | +from vision_agents.core import Agent |
| 62 | + |
| 63 | +# Create a local processor (no API key needed) |
| 64 | +processor = moondream.LocalDetectionProcessor( |
| 65 | + detect_objects=["person", "car", "dog"], |
| 66 | + conf_threshold=0.3, |
| 67 | + device="cuda", # Auto-detects CUDA, MPS, or CPU |
| 68 | + fps=30 |
| 69 | +) |
| 70 | + |
| 71 | +# Use in an agent |
| 72 | +agent = Agent( |
| 73 | + processors=[processor], |
| 74 | + llm=your_llm, |
| 75 | + # ... other components |
| 76 | +) |
| 77 | +``` |
| 78 | + |
| 79 | +### Detect Multiple Objects |
| 80 | + |
| 81 | +```python |
| 82 | +# Detect multiple object types with zero-shot detection |
| 83 | +processor = moondream.CloudDetectionProcessor( |
| 84 | + api_key="your-api-key", |
| 85 | + detect_objects=["person", "car", "dog", "basketball"], |
| 86 | + conf_threshold=0.3 |
| 87 | +) |
| 88 | + |
| 89 | +# Access results for LLM |
| 90 | +state = processor.state() |
| 91 | +print(state["detections_summary"]) # "Detected: 2 persons, 1 car" |
| 92 | +print(state["detections_count"]) # Total number of detections |
| 93 | +print(state["last_image"]) # PIL Image for vision models |
| 94 | +``` |
| 95 | + |
| 96 | +## Configuration |
| 97 | + |
| 98 | +### CloudDetectionProcessor Parameters |
| 99 | + |
| 100 | +- `api_key`: str - API key for Moondream Cloud API. If not provided, will attempt to read from `MOONDREAM_API_KEY` environment variable. |
| 101 | +- `detect_objects`: str | List[str] - Object(s) to detect using zero-shot detection. Can be any object name like "person", "car", "basketball". Default: `"person"` |
| 102 | +- `conf_threshold`: float - Confidence threshold for detections (default: 0.3) |
| 103 | +- `fps`: int - Frame processing rate (default: 30) |
| 104 | +- `interval`: int - Processing interval in seconds (default: 0) |
| 105 | +- `max_workers`: int - Thread pool size for CPU-intensive operations (default: 10) |
| 106 | + |
| 107 | +**Rate Limits:** By default, the Moondream Cloud API has a 2rps (requests per second) rate limit. Contact the Moondream team to request a higher limit. |
| 108 | + |
| 109 | +### LocalDetectionProcessor Parameters |
| 110 | + |
| 111 | +- `detect_objects`: str | List[str] - Object(s) to detect using zero-shot detection. Can be any object name like "person", "car", "basketball". Default: `"person"` |
| 112 | +- `conf_threshold`: float - Confidence threshold for detections (default: 0.3) |
| 113 | +- `fps`: int - Frame processing rate (default: 30) |
| 114 | +- `interval`: int - Processing interval in seconds (default: 0) |
| 115 | +- `max_workers`: int - Thread pool size for CPU-intensive operations (default: 10) |
| 116 | +- `device`: str - Device to run inference on ('cuda', 'mps', or 'cpu'). Auto-detects CUDA, then MPS (Apple Silicon), then defaults to CPU. Default: `None` (auto-detect) |
| 117 | +- `model_name`: str - Hugging Face model identifier (default: "moondream/moondream3-preview") |
| 118 | +- `options`: AgentOptions - Model directory configuration. If not provided, uses default which defaults to tempfile.gettempdir() |
| 119 | + |
| 120 | +**Performance:** Performance will vary depending on your hardware configuration. CUDA is recommended for best performance on NVIDIA GPUs. The model will be downloaded from HuggingFace on first use. |
| 121 | + |
| 122 | +## Video Publishing |
| 123 | + |
| 124 | +The processor publishes annotated video frames with bounding boxes drawn on detected objects: |
| 125 | + |
| 126 | +```python |
| 127 | +processor = moondream.CloudDetectionProcessor( |
| 128 | + api_key="your-api-key", |
| 129 | + detect_objects=["person", "car"] |
| 130 | +) |
| 131 | + |
| 132 | +# The track will show: |
| 133 | +# - Green bounding boxes around detected objects |
| 134 | +# - Labels with confidence scores |
| 135 | +# - Real-time annotation overlay |
| 136 | +``` |
| 137 | + |
| 138 | +## Testing |
| 139 | + |
| 140 | +The plugin includes comprehensive tests: |
| 141 | + |
| 142 | +```bash |
| 143 | +# Run all tests |
| 144 | +pytest plugins/moondream/tests/ -v |
| 145 | + |
| 146 | +# Run specific test categories |
| 147 | +pytest plugins/moondream/tests/ -k "inference" -v |
| 148 | +pytest plugins/moondream/tests/ -k "annotation" -v |
| 149 | +pytest plugins/moondream/tests/ -k "state" -v |
| 150 | +``` |
| 151 | + |
| 152 | +## Dependencies |
| 153 | + |
| 154 | +### Required |
| 155 | +- `vision-agents` - Core framework |
| 156 | +- `moondream` - Moondream SDK for cloud API (CloudDetectionProcessor only) |
| 157 | +- `numpy>=2.0.0` - Array operations |
| 158 | +- `pillow>=10.0.0` - Image processing |
| 159 | +- `opencv-python>=4.8.0` - Video annotation |
| 160 | +- `aiortc` - WebRTC support |
| 161 | + |
| 162 | +### LocalDetectionProcessor Additional Dependencies |
| 163 | +- `torch` - PyTorch for model inference |
| 164 | +- `transformers` - HuggingFace transformers library for model loading |
| 165 | + |
| 166 | +## Links |
| 167 | + |
| 168 | +- [Moondream Documentation](https://docs.moondream.ai/) |
| 169 | +- [Vision Agents Documentation](https://visionagents.ai/) |
| 170 | +- [GitHub Repository](https://github.com/GetStream/Vision-Agents) |
| 171 | + |
| 172 | + |
0 commit comments