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
ObjectCounter(**kwargs)
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. |
display_counts |
Displays object counts on the frame. |
process |
Processes input data (frames or object tracks) and updates counts. |
Examples:
>>> counter = ObjectCounter()
>>> frame = cv2.imread("frame.jpg")
>>> results = counter.process(frame)
>>> print(f"Inward count: {counter.in_count}, Outward count: {counter.out_count}")
Source code in ultralytics/solutions/object_counter.py
37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
count_objects
count_objects(current_centroid, track_id, prev_position, cls)
Counts objects within a polygonal or linear region based on their tracks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
current_centroid
|
Tuple[float, float]
|
Current centroid coordinates (x, y) 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_num = 1
>>> previous_position = (120, 220)
>>> class_to_count = 0 # In COCO model, class 0 = person
>>> counter.count_objects((140, 240), track_id_num, previous_position, class_to_count)
Source code in ultralytics/solutions/object_counter.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
display_counts
display_counts(plot_im)
Display object counts on the input image or frame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
plot_im
|
ndarray
|
The image or frame to display counts on. |
required |
Examples:
>>> counter = ObjectCounter()
>>> frame = cv2.imread("image.jpg")
>>> counter.display_counts(frame)
Source code in ultralytics/solutions/object_counter.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
process
process(im0)
Process input data (frames or object tracks) and update 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 |
---|---|
SolutionResults
|
Contains processed image |
Examples:
>>> counter = ObjectCounter()
>>> frame = cv2.imread("path/to/image.jpg")
>>> results = counter.process(frame)
Source code in ultralytics/solutions/object_counter.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
|