Skip to content

Register now for

# 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: Any)

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.
`margin` | `int` | Margin for background rectangle size to display counts properly.

Methods:

Name | Description
--- | ---
`count_objects` | Count objects within a polygonal or linear region based on their tracks.
`display_counts` | Display object counts on the frame.
`process` | Process input data and update 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`

    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    def __init__(self, **kwargs: Any) -> None:
        """Initialize the ObjectCounter class for real-time object counting in video streams."""
        super().__init__(**kwargs)

        self.in_count = 0  # Counter for objects moving inward
        self.out_count = 0  # Counter for objects moving outward
        self.counted_ids = []  # List of IDs of objects that have been counted
        self.classwise_count = defaultdict(lambda: {"IN": 0, "OUT": 0})  # Dictionary for counts, categorized by class
        self.region_initialized = False  # Flag indicating whether the region has been initialized

        self.show_in = self.CFG["show_in"]
        self.show_out = self.CFG["show_out"]
        self.margin = self.line_width * 2  # Scales the background rectangle size to display counts properly

### `` count_objects

    count_objects(
        current_centroid: tuple[float, float],
        track_id: int,
        prev_position: tuple[float, float] | None,
        cls: int,
    ) -> None

Count 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`

     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
    113
    114
    115
    116
    117
    118
    119
    120
    def count_objects(
        self,
        current_centroid: tuple[float, float],
        track_id: int,
        prev_position: tuple[float, float] | None,
        cls: int,
    ) -> None:
        """
        Count objects within a polygonal or linear region based on their tracks.

        Args:
            current_centroid (tuple[float, float]): Current centroid coordinates (x, y) in the current frame.
            track_id (int): Unique identifier for the tracked object.
            prev_position (tuple[float, float], optional): Last frame position coordinates (x, y) of the track.
            cls (int): Class index for classwise count updates.

        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)
        """
        if prev_position is None or track_id in self.counted_ids:
            return

        if len(self.region) == 2:  # Linear region (defined as a line segment)
            if self.r_s.intersects(self.LineString([prev_position, current_centroid])):
                # Determine orientation of the region (vertical or horizontal)
                if abs(self.region[0][0] - self.region[1][0]) < abs(self.region[0][1] - self.region[1][1]):
                    # Vertical region: Compare x-coordinates to determine direction
                    if current_centroid[0] > prev_position[0]:  # Moving right
                        self.in_count += 1
                        self.classwise_count[self.names[cls]]["IN"] += 1
                    else:  # Moving left
                        self.out_count += 1
                        self.classwise_count[self.names[cls]]["OUT"] += 1
                # Horizontal region: Compare y-coordinates to determine direction
                elif current_centroid[1] > prev_position[1]:  # Moving downward
                    self.in_count += 1
                    self.classwise_count[self.names[cls]]["IN"] += 1
                else:  # Moving upward
                    self.out_count += 1
                    self.classwise_count[self.names[cls]]["OUT"] += 1
                self.counted_ids.append(track_id)

        elif len(self.region) > 2:  # Polygonal region
            if self.r_s.contains(self.Point(current_centroid)):
                # Determine motion direction for vertical or horizontal polygons
                region_width = max(p[0] for p in self.region) - min(p[0] for p in self.region)
                region_height = max(p[1] for p in self.region) - min(p[1] for p in self.region)

                if (
                    region_width < region_height
                    and current_centroid[0] > prev_position[0]
                    or region_width >= region_height
                    and current_centroid[1] > prev_position[1]
                ):  # Moving right or downward
                    self.in_count += 1
                    self.classwise_count[self.names[cls]]["IN"] += 1
                else:  # Moving left or upward
                    self.out_count += 1
                    self.classwise_count[self.names[cls]]["OUT"] += 1
                self.counted_ids.append(track_id)

### `` display_counts

    display_counts(plot_im) -> None

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`

    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    def display_counts(self, plot_im) -> None:
        """
        Display object counts on the input image or frame.

        Args:
            plot_im (np.ndarray): The image or frame to display counts on.

        Examples:
            >>> counter = ObjectCounter()
            >>> frame = cv2.imread("image.jpg")
            >>> counter.display_counts(frame)
        """
        labels_dict = {
            str.capitalize(key): f"{'IN ' + str(value['IN']) if self.show_in else ''} "
            f"{'OUT ' + str(value['OUT']) if self.show_out else ''}".strip()
            for key, value in self.classwise_count.items()
            if value["IN"] != 0 or value["OUT"] != 0 and (self.show_in or self.show_out)
        }
        if labels_dict:
            self.annotator.display_analytics(plot_im, labels_dict, (104, 31, 17), (255, 255, 255), self.margin)

### `` process

    process(im0) -> SolutionResults

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 `im0`, 'in_count' (int, count of objects entering the region), 'out_count' (int, count of objects exiting the region), 'classwise_count' (dict, per-class object count), and 'total_tracks' (int, total number of tracked objects).

Examples:

    >>> counter = ObjectCounter()
    >>> frame = cv2.imread("path/to/image.jpg")
    >>> results = counter.process(frame)

Source code in `ultralytics/solutions/object_counter.py`

    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
    def process(self, im0) -> SolutionResults:
        """
        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.

        Args:
            im0 (np.ndarray): The input image or frame to be processed.

        Returns:
            (SolutionResults): Contains processed image `im0`, 'in_count' (int, count of objects entering the region),
                'out_count' (int, count of objects exiting the region), 'classwise_count' (dict, per-class object count),
                and 'total_tracks' (int, total number of tracked objects).

        Examples:
            >>> counter = ObjectCounter()
            >>> frame = cv2.imread("path/to/image.jpg")
            >>> results = counter.process(frame)
        """
        if not self.region_initialized:
            self.initialize_region()
            self.region_initialized = True

        self.extract_tracks(im0)  # Extract tracks
        self.annotator = SolutionAnnotator(im0, line_width=self.line_width)  # Initialize annotator

        self.annotator.draw_region(
            reg_pts=self.region, color=(104, 0, 123), thickness=self.line_width * 2
        )  # Draw region

        # Iterate over bounding boxes, track ids and classes index
        for box, track_id, cls, conf in zip(self.boxes, self.track_ids, self.clss, self.confs):
            # Draw bounding box and counting region
            self.annotator.box_label(box, label=self.adjust_box_label(cls, conf, track_id), color=colors(cls, True))
            self.store_tracking_history(track_id, box)  # Store track history

            # Store previous position of track for object counting
            prev_position = None
            if len(self.track_history[track_id]) > 1:
                prev_position = self.track_history[track_id][-2]
            self.count_objects(self.track_history[track_id][-1], track_id, prev_position, cls)  # object counting

        plot_im = self.annotator.result()
        self.display_counts(plot_im)  # Display the counts on the frame
        self.display_output(plot_im)  # Display output with base class function

        # Return SolutionResults
        return SolutionResults(
            plot_im=plot_im,
            in_count=self.in_count,
            out_count=self.out_count,
            classwise_count=dict(self.classwise_count),
            total_tracks=len(self.track_ids),
        )

📅 Created 1 year ago ✏️ Updated 1 year ago