Reference for ultralytics/solutions/instance_segmentation.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/solutions/instance_segmentation.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.solutions.instance_segmentation.InstanceSegmentation
Bases: BaseSolution
A class to manage instance segmentation in images or video streams.
This class extends the BaseSolution class and provides functionality for performing instance segmentation, including drawing segmented masks with bounding boxes and labels.
Attributes:
Name | Type | Description |
---|---|---|
model |
str
|
The segmentation model to use for inference. |
line_width |
int
|
Width of the bounding box and text lines. |
names |
Dict[int, str]
|
Dictionary mapping class indices to class names. |
clss |
List[int]
|
List of detected class indices. |
track_ids |
List[int]
|
List of track IDs for detected instances. |
masks |
List[ndarray]
|
List of segmentation masks for detected instances. |
Methods:
Name | Description |
---|---|
process |
Process the input image to perform instance segmentation and annotate results. |
extract_tracks |
Extract tracks including bounding boxes, classes, and masks from model predictions. |
Examples:
>>> segmenter = InstanceSegmentation()
>>> frame = cv2.imread("frame.jpg")
>>> results = segmenter.segment(frame)
>>> print(f"Total segmented instances: {results['total_tracks']}")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
Any
|
Keyword arguments passed to the BaseSolution parent class. model (str): Model name or path, defaults to "yolo11n-seg.pt". |
{}
|
Source code in ultralytics/solutions/instance_segmentation.py
process
Perform instance segmentation on the input image and annotate the results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im0
|
ndarray
|
The input image for segmentation. |
required |
Returns:
Type | Description |
---|---|
SolutionResults
|
Object containing the annotated image and total number of tracked instances. |
Examples:
>>> segmenter = InstanceSegmentation()
>>> frame = cv2.imread("image.jpg")
>>> summary = segmenter.segment(frame)
>>> print(summary)