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 for real-time counting of objects within user-defined regions in a video stream.
This class inherits from BaseSolution
and provides functionality to define polygonal regions in a video frame,
track objects, and count those objects that pass through each defined region. Useful for applications requiring
counting in specified areas, such as monitoring zones or segmented sections.
Attributes:
Name | Type | Description |
---|---|---|
region_template |
Dict
|
Template for creating new counting regions with default attributes including name, polygon coordinates, and display colors. |
counting_regions |
List
|
List storing all defined regions, where each entry is based on |
region_counts |
Dict
|
Dictionary storing the count of objects for each named region. |
Methods:
Name | Description |
---|---|
add_region |
Adds a new counting region with specified attributes. |
process |
Processes video frames to count objects in each region. |
Source code in ultralytics/solutions/region_counter.py
add_region
Add 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
process
Process 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:
Type | Description |
---|---|
SolutionResults
|
Contains processed image |