Reference for ultralytics/solutions/solutions.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/solutions/solutions.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.solutions.solutions.BaseSolution
A base class for managing Ultralytics Solutions.
This class provides core functionality for various Ultralytics Solutions, including model loading, object tracking, and region initialization.
Attributes:
Name | Type | Description |
---|---|---|
LineString |
LineString
|
Class for creating line string geometries. |
Polygon |
Polygon
|
Class for creating polygon geometries. |
Point |
Point
|
Class for creating point geometries. |
CFG |
Dict
|
Configuration dictionary loaded from a YAML file and updated with kwargs. |
region |
List[Tuple[int, int]]
|
List of coordinate tuples defining a region of interest. |
line_width |
int
|
Width of lines used in visualizations. |
model |
YOLO
|
Loaded YOLO model instance. |
names |
Dict[int, str]
|
Dictionary mapping class indices to class names. |
env_check |
bool
|
Flag indicating whether the environment supports image display. |
track_history |
defaultdict
|
Dictionary to store tracking history for each object. |
Methods:
Name | Description |
---|---|
extract_tracks |
Apply object tracking and extract tracks from an input image. |
store_tracking_history |
Store object tracking history for a given track ID and bounding box. |
initialize_region |
Initialize the counting region and line segment based on configuration. |
display_output |
Display the results of processing, including showing frames or saving results. |
Examples:
>>> solution = BaseSolution(model="yolo11n.pt", region=[(0, 0), (100, 0), (100, 100), (0, 100)])
>>> solution.initialize_region()
>>> image = cv2.imread("image.jpg")
>>> solution.extract_tracks(image)
>>> solution.display_output(image)
IS_CLI (optional): Enables CLI mode if set.
Source code in ultralytics/solutions/solutions.py
display_output
Display the results of the processing, which could involve showing frames, printing counts, or saving results.
This method is responsible for visualizing the output of the object detection and tracking process. It displays the processed frame with annotations, and allows for user interaction to close the display.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im0
|
ndarray
|
The input image or frame that has been processed and annotated. |
required |
Examples:
>>> solution = BaseSolution()
>>> frame = cv2.imread("path/to/image.jpg")
>>> solution.display_output(frame)
Notes
- This method will only display output if the 'show' configuration is set to True and the environment supports image display.
- The display can be closed by pressing the 'q' key.
Source code in ultralytics/solutions/solutions.py
extract_tracks
Applies object tracking and extracts tracks from an input image or frame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im0
|
ndarray
|
The input image or frame. |
required |
Examples:
>>> solution = BaseSolution()
>>> frame = cv2.imread("path/to/image.jpg")
>>> solution.extract_tracks(frame)
Source code in ultralytics/solutions/solutions.py
initialize_region
Initialize the counting region and line segment based on configuration settings.
Source code in ultralytics/solutions/solutions.py
store_tracking_history
Stores the tracking history of an object.
This method updates the tracking history for a given object by appending the center point of its bounding box to the track line. It maintains a maximum of 30 points in the tracking history.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
track_id
|
int
|
The unique identifier for the tracked object. |
required |
box
|
List[float]
|
The bounding box coordinates of the object in the format [x1, y1, x2, y2]. |
required |
Examples: