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
RegionCounter(**kwargs: Any)
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 |
Add a new counting region with specified attributes. |
process |
Process video frames to count objects in each region. |
Examples:
Initialize a RegionCounter and add a counting region
>>> counter = RegionCounter()
>>> counter.add_region("Zone1", [(100, 100), (200, 100), (200, 200), (100, 200)], (255, 0, 0), (255, 255, 255))
>>> results = counter.process(frame)
>>> print(f"Total tracks: {results.total_tracks}")
Source code in ultralytics/solutions/region_counter.py
38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
add_region
add_region(
name: str,
polygon_points: List[Tuple],
region_color: Tuple[int, int, int],
text_color: Tuple[int, int, int],
) -> None
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[int, int, int]
|
BGR color for region visualization. |
required |
text_color
|
Tuple[int, int, int]
|
BGR color for the text within the region. |
required |
Source code in ultralytics/solutions/region_counter.py
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 |
|
process
process(im0: ndarray) -> SolutionResults
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 |
Source code in ultralytics/solutions/region_counter.py
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
|