Skip to main content

API Reference

Complete API documentation for the SAM3 detector filter.

FilterSAM3DetectorConfigDirect link to FilterSAM3DetectorConfig

Configuration class for the SAM3 detector filter.

Class DefinitionDirect link to Class Definition

class FilterSAM3DetectorConfig(FilterConfig):
"""Configuration for SAM3 object detection filter."""

Configuration ParametersDirect link to Configuration Parameters

ParameterTypeDefaultDescription
model_idstr"facebook/sam2-hiera-large"HuggingFace model ID or local path to model checkpoint
devicestr"cuda"Device to run inference on: "cuda", "cpu", or "mps"
text_promptstr | NoneNoneNatural language text prompt for detection (e.g., "person", "car")
exemplars_pathstr | NoneNonePath to directory containing exemplar images (jpg/png)
exemplar_embeddings_cachestr | NoneNonePath to cache file for exemplar embeddings (optional)
positive_boxeslist | NoneNoneReference boxes (positive): list of [x, y, w, h] in pixels
negative_boxeslist | NoneNoneReference boxes (negative): list of [x, y, w, h] in pixels
confidence_thresholdfloat0.5Minimum confidence score for detections (0.0-1.0)
mask_thresholdfloat0.5Threshold for mask binarization (0.0-1.0)
max_detectionsint100Maximum number of detections per frame
output_masksboolTrueWhether to output segmentation masks
output_boxesboolTrueWhether to output bounding boxes
output_scoresboolTrueWhether to output confidence scores
output_labelstr"sam3_detections"Key for storing results in frame.data['meta']
visualizeboolFalseWhether to draw detections on output frames
viz_topicstr""When set (e.g. "viz"), main topic gets original frame + meta; this topic gets drawn frame + same meta. Empty = legacy behavior (visualize draws on main).
debugboolFalseEnable debug logging

Environment VariablesDirect link to Environment Variables

All parameters can be set via environment variables with the FILTER_ prefix:

export FILTER_TEXT_PROMPT="person"
export FILTER_CONFIDENCE_THRESHOLD=0.7
export FILTER_DEVICE=cuda

FilterSAM3DetectorDirect link to FilterSAM3Detector

Main filter class for SAM3 object detection.

Class DefinitionDirect link to Class Definition

class FilterSAM3Detector(Filter):
"""
SAM3 object detection filter.

This filter performs open-set object detection using SAM3 (Segment Anything Model 3).
It supports two prompting modes:
- Text prompts: Natural language descriptions (e.g., "person", "car")
- Image exemplars: Few-shot learning with cropped example images
"""

MethodsDirect link to Methods

normalize_config(config: FilterConfig) -> FilterConfigDirect link to normalize_configconfig-filterconfig---filterconfig

Normalize and validate configuration parameters.

Parameters:

  • config (FilterConfig): Input configuration dictionary

Returns:

  • FilterConfig: Normalized and validated configuration

Raises:

  • ValueError: If configuration parameters are invalid

Note: This method is idempotent - calling it multiple times produces the same result.

setup(config: FilterSAM3DetectorConfig) -> NoneDirect link to setupconfig-filtersam3detectorconfig---none

Initialize the filter with the given configuration.

Parameters:

  • config (FilterSAM3DetectorConfig): Filter configuration

Actions:

  • Loads the SAM3 model from HuggingFace or local path
  • Loads and processes exemplar images if provided
  • Computes exemplar embeddings
  • Initializes device (CUDA/CPU/MPS)

Raises:

  • ImportError: If SAM3 dependencies are not available
  • RuntimeError: If model loading fails

process(frames: dict[str, Frame]) -> dict[str, Frame]Direct link to processframes-dictstr-frame---dictstr-frame

Process input frames and detect objects.

Parameters:

  • frames (dict[str, Frame]): Dictionary of input frames keyed by topic name

Returns:

  • dict[str, Frame]: Dictionary of output frames with detection results

Output Format: Detections are stored in frame.data['meta'][output_label]:

[
{
"box": [x1, y1, x2, y2], # Bounding box coordinates
"score": 0.95, # Confidence score (0.0-1.0)
"mask": [[...]] # Binary mask as 2D array (if output_masks=True)
},
...
]

shutdown() -> NoneDirect link to shutdown---none

Clean up resources when the filter is stopped.

Actions:

  • Releases model resources
  • Clears GPU memory cache
  • Closes file handles

run(config: dict[str, Any] | None = None, **kwargs) -> NoneDirect link to runconfig-dictstr-any--none--none-kwargs---none

Run the filter standalone until it exits.

Parameters:

  • config (dict | None): Configuration dictionary (if None, uses environment variables)
  • **kwargs: Additional arguments passed to Filter.run()

Usage:

from filter_sam3_detector import FilterSAM3Detector

# Run with default config from environment
FilterSAM3Detector.run()

# Run with explicit config
FilterSAM3Detector.run({
"text_prompt": "person",
"confidence_threshold": 0.7,
"sources": "tcp://127.0.0.1:5555",
"outputs": ["tcp://127.0.0.1:5556"],
})

Frame Data StructureDirect link to Frame Data Structure

Input FrameDirect link to Input Frame

Frames must have image data accessible via frame.rw_bgr.image:

frame.rw_bgr.image  # numpy array in BGR format (H, W, 3)
frame.has_image # bool: whether frame contains image data

Output FrameDirect link to Output Frame

Detections are added to frame metadata:

frame.data['meta'][output_label] = [
{
"box": [x1, y1, x2, y2],
"score": 0.95,
"mask": [[...]] # Optional, if output_masks=True
}
]

Error HandlingDirect link to Error Handling

The filter handles errors gracefully:

  • Missing model: Forwards frames unchanged with warning
  • No prompts: Forwards frames unchanged with warning
  • Processing errors: Logs error and forwards frame unchanged
  • Invalid configuration: Raises ValueError during setup

ExamplesDirect link to Examples

Basic UsageDirect link to Basic Usage

from filter_sam3_detector import FilterSAM3Detector
from openfilter.filter_runtime.filter import Filter

filters = [
(FilterSAM3Detector, {
"sources": "tcp://127.0.0.1:5555",
"outputs": ["tcp://127.0.0.1:5556"],
"text_prompt": "person",
"confidence_threshold": 0.5,
}),
]

Filter.run_multi(filters)

With ExemplarsDirect link to With Exemplars

filters = [
(FilterSAM3Detector, {
"sources": "tcp://127.0.0.1:5555",
"outputs": ["tcp://127.0.0.1:5556"],
"exemplars_path": "./cup_examples/",
"confidence_threshold": 0.3,
}),
]

With Reference BoxesDirect link to With Reference Boxes

filters = [
(FilterSAM3Detector, {
"sources": "tcp://127.0.0.1:5555",
"outputs": ["tcp://127.0.0.1:5556"],
"text_prompt": "person", # optional when using ref boxes
"positive_boxes": [[480, 290, 110, 360], [370, 280, 115, 375]],
"negative_boxes": [[100, 100, 50, 200]],
"visualize": True, # green=positive ref, red=negative ref, blue=detections
"viz_topic": "viz", # optional: main=original+meta, viz=drawn frame+meta
}),
]

Custom Output LabelDirect link to Custom Output Label

filters = [
(FilterSAM3Detector, {
"sources": "tcp://127.0.0.1:5555",
"outputs": ["tcp://127.0.0.1:5556"],
"text_prompt": "car",
"output_label": "vehicle_detections",
}),
]