Reference for ultralytics/solutions/region_counter.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/solutions/region_counter.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.solutions.region_counter.RegionCounter
Bases: BaseSolution
A class designed for real-time counting of objects within user-defined regions in a video stream.
This class inherits from BaseSolution
and offers functionalities to define polygonal regions in a video
frame, track objects, and count those objects that pass through each defined region. This makes it useful
for applications that require counting in specified areas, such as monitoring zones or segmented sections.
Attributes:
Name | Type | Description |
---|---|---|
region_template |
dict
|
A template for creating new counting regions with default attributes including the name, polygon coordinates, and display colors. |
counting_regions |
list
|
A list storing all defined regions, where each entry is based on |
Methods:
Name | Description |
---|---|
add_region |
Adds a new counting region with specified attributes, such as the region's name, polygon points, region color, and text color. |
count |
Processes video frames to count objects in each region, drawing regions and displaying counts on the frame. Handles object detection, region definition, and containment checks. |
Source code in ultralytics/solutions/region_counter.py
add_region
Adds a new region to the counting list based on the provided template with specific attributes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name assigned to the new region. |
required |
polygon_points
|
list[tuple]
|
List of (x, y) coordinates defining the region's polygon. |
required |
region_color
|
tuple
|
BGR color for region visualization. |
required |
text_color
|
tuple
|
BGR color for the text within the region. |
required |
Source code in ultralytics/solutions/region_counter.py
count
Processes the input frame to detect and count objects within each defined region.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im0
|
ndarray
|
Input image frame where objects and regions are annotated. |
required |
Returns:
Name | Type | Description |
---|---|---|
im0 |
ndarray
|
Processed image frame with annotated counting information. |