Reference for ultralytics/models/fastsam/model.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/models/fastsam/model.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.models.fastsam.model.FastSAM
FastSAM(model: str = 'FastSAM-x.pt')
Bases: Model
FastSAM model interface for segment anything tasks.
This class extends the base Model class to provide specific functionality for the FastSAM (Fast Segment Anything Model) implementation, allowing for efficient and accurate image segmentation with optional prompting support.
Attributes:
Name | Type | Description |
---|---|---|
model |
str
|
Path to the pre-trained FastSAM model file. |
task |
str
|
The task type, set to "segment" for FastSAM models. |
Methods:
Name | Description |
---|---|
predict |
Perform segmentation prediction on image or video source with optional prompts. |
task_map |
Returns mapping of segment task to predictor and validator classes. |
Examples:
Initialize FastSAM model and run prediction
>>> from ultralytics import FastSAM
>>> model = FastSAM("FastSAM-x.pt")
>>> results = model.predict("ultralytics/assets/bus.jpg")
Run prediction with bounding box prompts
>>> results = model.predict("image.jpg", bboxes=[[100, 100, 200, 200]])
Source code in ultralytics/models/fastsam/model.py
37 38 39 40 41 42 |
|
task_map
property
task_map: Dict[str, Dict[str, Any]]
Returns a dictionary mapping segment task to corresponding predictor and validator classes.
predict
predict(
source,
stream: bool = False,
bboxes: Optional[List] = None,
points: Optional[List] = None,
labels: Optional[List] = None,
texts: Optional[List] = None,
**kwargs: Any
)
Perform segmentation prediction on image or video source.
Supports prompted segmentation with bounding boxes, points, labels, and texts. The method packages these prompts and passes them to the parent class predict method for processing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source
|
str | Image | ndarray
|
Input source for prediction, can be a file path, URL, PIL image, or numpy array. |
required |
stream
|
bool
|
Whether to enable real-time streaming mode for video inputs. |
False
|
bboxes
|
List
|
Bounding box coordinates for prompted segmentation in format [[x1, y1, x2, y2]]. |
None
|
points
|
List
|
Point coordinates for prompted segmentation in format [[x, y]]. |
None
|
labels
|
List
|
Class labels for prompted segmentation. |
None
|
texts
|
List
|
Text prompts for segmentation guidance. |
None
|
**kwargs
|
Any
|
Additional keyword arguments passed to the predictor. |
{}
|
Returns:
Type | Description |
---|---|
List
|
List of Results objects containing the prediction results. |
Source code in ultralytics/models/fastsam/model.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|