Reference for ultralytics/solutions/object_counter.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/solutions/object_counter.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.solutions.object_counter.ObjectCounter
Bases: BaseSolution
A class to manage the counting of objects in a real-time video stream based on their tracks.
This class extends the BaseSolution class and provides functionality for counting objects moving in and out of a specified region in a video stream. It supports both polygonal and linear regions for counting.
Attributes:
Name | Type | Description |
---|---|---|
in_count |
int
|
Counter for objects moving inward. |
out_count |
int
|
Counter for objects moving outward. |
counted_ids |
List[int]
|
List of IDs of objects that have been counted. |
classwise_counts |
Dict[str, Dict[str, int]]
|
Dictionary for counts, categorized by object class. |
region_initialized |
bool
|
Flag indicating whether the counting region has been initialized. |
show_in |
bool
|
Flag to control display of inward count. |
show_out |
bool
|
Flag to control display of outward count. |
Methods:
Name | Description |
---|---|
count_objects |
Counts objects within a polygonal or linear region. |
store_classwise_counts |
Initializes class-wise counts if not already present. |
display_counts |
Displays object counts on the frame. |
count |
Processes input data (frames or object tracks) and updates counts. |
Examples:
>>> counter = ObjectCounter()
>>> frame = cv2.imread("frame.jpg")
>>> processed_frame = counter.count(frame)
>>> print(f"Inward count: {counter.in_count}, Outward count: {counter.out_count}")
Source code in ultralytics/solutions/object_counter.py
count
Processes input data (frames or object tracks) and updates object counts.
This method initializes the counting region, extracts tracks, draws bounding boxes and regions, updates object counts, and displays the results on the input image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im0
|
ndarray
|
The input image or frame to be processed. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The processed image with annotations and count information. |
Examples:
>>> counter = ObjectCounter()
>>> frame = cv2.imread("path/to/image.jpg")
>>> processed_frame = counter.count(frame)
Source code in ultralytics/solutions/object_counter.py
count_objects
Counts objects within a polygonal or linear region based on their tracks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
current_centroid
|
Tuple[float, float]
|
Current centroid values in the current frame. |
required |
track_id
|
int
|
Unique identifier for the tracked object. |
required |
prev_position
|
Tuple[float, float]
|
Last frame position coordinates (x, y) of the track. |
required |
cls
|
int
|
Class index for classwise count updates. |
required |
Examples:
>>> counter = ObjectCounter()
>>> track_line = {1: [100, 200], 2: [110, 210], 3: [120, 220]}
>>> box = [130, 230, 150, 250]
>>> track_id = 1
>>> prev_position = (120, 220)
>>> cls = 0
>>> counter.count_objects(current_centroid, track_id, prev_position, cls)
Source code in ultralytics/solutions/object_counter.py
display_counts
Displays object counts on the input image or frame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im0
|
ndarray
|
The input image or frame to display counts on. |
required |
Examples:
Source code in ultralytics/solutions/object_counter.py
store_classwise_counts
Initialize class-wise counts for a specific object class if not already present.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls
|
int
|
Class index for classwise count updates. |
required |
This method ensures that the 'classwise_counts' dictionary contains an entry for the specified class, initializing 'IN' and 'OUT' counts to zero if the class is not already present.
Examples:
>>> counter = ObjectCounter()
>>> counter.store_classwise_counts(0) # Initialize counts for class index 0
>>> print(counter.classwise_counts)
{'person': {'IN': 0, 'OUT': 0}}