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(
names,
reg_pts=None,
line_thickness=2,
view_img=False,
view_in_counts=True,
view_out_counts=True,
draw_tracks=False,
)
A class to manage the counting of objects in a real-time video stream based on their tracks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
names |
dict
|
Dictionary of class names. |
required |
reg_pts |
list
|
List of points defining the counting region. |
None
|
line_thickness |
int
|
Line thickness for bounding boxes. |
2
|
view_img |
bool
|
Flag to control whether to display the video stream. |
False
|
view_in_counts |
bool
|
Flag to control whether to display the in counts on the video stream. |
True
|
view_out_counts |
bool
|
Flag to control whether to display the out counts on the video stream. |
True
|
draw_tracks |
bool
|
Flag to control whether to draw the object tracks. |
False
|
Source code in ultralytics/solutions/object_counter.py
display_frames
Displays the current frame with annotations and regions in a window.
Source code in ultralytics/solutions/object_counter.py
extract_and_process_tracks
Extracts and processes tracks for object counting in a video stream.
Source code in ultralytics/solutions/object_counter.py
123 124 125 126 127 128 129 130 131 132 133 134 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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
|
mouse_event_for_region
Handles mouse events for defining and moving the counting region in a real-time video stream.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event |
int
|
The type of mouse event (e.g., cv2.EVENT_MOUSEMOVE, cv2.EVENT_LBUTTONDOWN, etc.). |
required |
x |
int
|
The x-coordinate of the mouse pointer. |
required |
y |
int
|
The y-coordinate of the mouse pointer. |
required |
flags |
int
|
Any associated event flags (e.g., cv2.EVENT_FLAG_CTRLKEY, cv2.EVENT_FLAG_SHIFTKEY, etc.). |
required |
params |
dict
|
Additional parameters for the function. |
required |
Source code in ultralytics/solutions/object_counter.py
start_counting
Main function to start the object counting process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im0 |
ndarray
|
Current frame from the video stream. |
required |
tracks |
list
|
List of tracks obtained from the object tracking process. |
required |