Reference for ultralytics/models/sam/model.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/sam/model.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.models.sam.model.SAM
Bases: Model
SAM (Segment Anything Model) interface class.
SAM is designed for promptable real-time image segmentation. It can be used with a variety of prompts such as bounding boxes, points, or labels. The model has capabilities for zero-shot performance and is trained on the SA-1B dataset.
Source code in ultralytics/models/sam/model.py
task_map
property
Provides a mapping from the 'segment' task to its corresponding 'Predictor'.
Returns:
Type | Description |
---|---|
dict
|
A dictionary mapping the 'segment' task to its corresponding 'Predictor'. |
__call__(source=None, stream=False, bboxes=None, points=None, labels=None, **kwargs)
Alias for the 'predict' method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source |
str
|
Path to the image or video file, or a PIL.Image object, or a numpy.ndarray object. |
None
|
stream |
bool
|
If True, enables real-time streaming. Defaults to False. |
False
|
bboxes |
list
|
List of bounding box coordinates for prompted segmentation. Defaults to None. |
None
|
points |
list
|
List of points for prompted segmentation. Defaults to None. |
None
|
labels |
list
|
List of labels for prompted segmentation. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
list
|
The model predictions. |
Source code in ultralytics/models/sam/model.py
__init__(model='sam_b.pt')
Initializes the SAM model with a pre-trained model file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model |
str
|
Path to the pre-trained SAM model file. File should have a .pt or .pth extension. |
'sam_b.pt'
|
Raises:
Type | Description |
---|---|
NotImplementedError
|
If the model file extension is not .pt or .pth. |
Source code in ultralytics/models/sam/model.py
info(detailed=False, verbose=True)
Logs information about the SAM model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
detailed |
bool
|
If True, displays detailed information about the model. Defaults to False. |
False
|
verbose |
bool
|
If True, displays information on the console. Defaults to True. |
True
|
Returns:
Type | Description |
---|---|
tuple
|
A tuple containing the model's information. |
Source code in ultralytics/models/sam/model.py
predict(source, stream=False, bboxes=None, points=None, labels=None, **kwargs)
Performs segmentation prediction on the given image or video source.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source |
str
|
Path to the image or video file, or a PIL.Image object, or a numpy.ndarray object. |
required |
stream |
bool
|
If True, enables real-time streaming. Defaults to False. |
False
|
bboxes |
list
|
List of bounding box coordinates for prompted segmentation. Defaults to None. |
None
|
points |
list
|
List of points for prompted segmentation. Defaults to None. |
None
|
labels |
list
|
List of labels for prompted segmentation. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
list
|
The model predictions. |