Skip to main content
Segment an image with SAM-3 using exactly one prompt type.

Parameters

ImageInput
required
RGB image as file path, URL, PIL Image, or numpy array.
Optional[str]
Text prompt describing objects to segment (exclusive with points/boxes).
Optional[Iterable[Iterable[float]]]
List of [x, y] point coordinates (exclusive with text/boxes).
Optional[Iterable[Iterable[float]]]
List of [x0, y0, x1, y1] box coordinates (exclusive with text/points).
Optional[Iterable[int]]
Required for points/boxes. 1=foreground, 0=background.
Optional[float]
Optional HTTP timeout in seconds.

Returns

np.ndarray: Binary segmentation mask as numpy array (H, W) with dtype uint8. Foreground pixels are 255, background pixels are 0.

Example

SAM3 offers two client methods:
  • run() — Returns a single union mask. Use when you only need the combined segmentation.
  • run_with_detections() — Returns the union mask plus per-instance masks, bounding boxes, and confidence scores. Use when you need individual object information (e.g. counting objects, filtering by confidence, or processing instances separately). Only available for text and box prompts — point prompts have no detection semantics.

run_with_detections()

Same call signature as run(). Returns a dict with keys:
  • union_mask — Combined binary mask (H, W), dtype uint8 (same as run() output)
  • masks — List of per-instance binary masks, each (H, W) dtype uint8
  • boxes — List of [x0, y0, x1, y1] bounding boxes (text/box prompts only)
  • scores — List of confidence scores (sorted descending)
For point prompts, masks, boxes, and scores will be empty lists.

Example Output — run()

SAM3 run() output — union mask overlay on input image Use run() when you only need the combined mask — e.g. masking a region, computing area, or passing to a downstream model.

Example Output — run_with_detections()

SAM3 run_with_detections() output — per-instance masks, bounding boxes, and confidence scores Use run_with_detections() when you need per-instance information — e.g. counting objects, filtering by confidence, or processing each instance separately. Works with text and box prompts only.

Related topics

sam3CortexClient