Skip to content

Register now for

# Object Counting in Different Regions using Ultralytics YOLO 🚀

## What is Object Counting in Regions?

Object counting in regions with Ultralytics YOLO11 involves precisely determining the number of objects within specified areas using advanced computer vision. This approach is valuable for optimizing processes, enhancing security, and improving efficiency in various applications.

Watch: Object Counting in Different Regions using Ultralytics YOLO11 | Ultralytics Solutions 🚀

## Advantages of Object Counting in Regions?

- Precision and Accuracy: Object counting in regions with advanced computer vision ensures precise and accurate counts, minimizing errors often associated with manual counting.
- Efficiency Improvement: Automated object counting enhances operational efficiency, providing real-time results and streamlining processes across different applications.
- Versatility and Application: The versatility of object counting in regions makes it applicable across various domains, from manufacturing and surveillance to traffic monitoring, contributing to its widespread utility and effectiveness.

## Real World Applications

Retail | Market Streets
--- | ---
People Counting in Different Region using Ultralytics YOLO11 | Crowd Counting in Different Region using Ultralytics YOLO11

## Usage Examples

Region counting using Ultralytics YOLO

Python

    import cv2

    from ultralytics import solutions

    cap = cv2.VideoCapture("path/to/video.mp4")
    assert cap.isOpened(), "Error reading video file"

    # Pass region as list
    # region_points = [(20, 400), (1080, 400), (1080, 360), (20, 360)]

    # Pass region as dictionary
    region_points = {
        "region-01": [(50, 50), (250, 50), (250, 250), (50, 250)],
        "region-02": [(640, 640), (780, 640), (780, 720), (640, 720)],
    }

    # Video writer
    w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
    video_writer = cv2.VideoWriter("region_counting.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))

    # Initialize region counter object
    regioncounter = solutions.RegionCounter(
        show=True,  # display the frame
        region=region_points,  # pass region points
        model="yolo11n.pt",  # model for counting in regions i.e yolo11s.pt
    )

    # Process video
    while cap.isOpened():
        success, im0 = cap.read()

        if not success:
            print("Video frame is empty or processing is complete.")
            break

        results = regioncounter(im0)

        # print(results)  # access the output

        video_writer.write(results.plot_im)

    cap.release()
    video_writer.release()
    cv2.destroyAllWindows()  # destroy all opened windows

Ultralytics Example Code

The Ultralytics region counting module is available in our examples section. You can explore this example for code customization and modify it to suit your specific use case.

### `RegionCounter` Arguments

Here's a table with the `RegionCounter` arguments:

Argument | Type | Default | Description
--- | --- | --- | ---
`model` | `str` | `None` | Path to Ultralytics YOLO Model File.
`region` | `list` | `'[(20, 400), (1260, 400)]'` | List of points defining the counting region.

The `RegionCounter` solution enables the use of object tracking parameters:

Argument | Type | Default | Description
--- | --- | --- | ---
`tracker` | `str` | `'botsort.yaml'` | Specifies the tracking algorithm to use, e.g., `bytetrack.yaml` or `botsort.yaml`.
`conf` | `float` | `0.3` | Sets the confidence threshold for detections; lower values allow more objects to be tracked but may include false positives.
`iou` | `float` | `0.5` | Sets the Intersection over Union (IoU) threshold for filtering overlapping detections.
`classes` | `list` | `None` | Filters results by class index. For example, `classes=[0, 2, 3]` only tracks the specified classes.
`verbose` | `bool` | `True` | Controls the display of tracking results, providing a visual output of tracked objects.
`device` | `str` | `None` | Specifies the device for inference (e.g., `cpu`, `cuda:0` or `0`). Allows users to select between CPU, a specific GPU, or other compute devices for model execution.

Additionally, the following visualization settings are supported:

Argument | Type | Default | Description
--- | --- | --- | ---
`show` | `bool` | `False` | If `True`, displays the annotated images or videos in a window. Useful for immediate visual feedback during development or testing.
`line_width` | `None or int` | `None` | Specifies the line width of bounding boxes. If `None`, the line width is automatically adjusted based on the image size. Provides visual customization for clarity.
`show_conf` | `bool` | `True` | Displays the confidence score for each detection alongside the label. Gives insight into the model's certainty for each detection.
`show_labels` | `bool` | `True` | Displays labels for each detection in the visual output. Provides immediate understanding of detected objects.

## FAQ

### What is object counting in specified regions using Ultralytics YOLO11?

Object counting in specified regions with Ultralytics YOLO11 involves detecting and tallying the number of objects within defined areas using advanced computer vision. This precise method enhances efficiency and accuracy across various applications like manufacturing, surveillance, and traffic monitoring.

### How do I run the region based object counting script with Ultralytics YOLO11?

Follow these steps to run object counting in Ultralytics YOLO11:

1. Clone the Ultralytics repository and navigate to the directory:

       git clone https://github.com/ultralytics/ultralytics
       cd ultralytics/examples/YOLOv8-Region-Counter

2. Execute the region counting script:

       python yolov8_region_counter.py --source "path/to/video.mp4" --save-img

For more options, visit the Usage Examples section.

### Why should I use Ultralytics YOLO11 for object counting in regions?

Using Ultralytics YOLO11 for object counting in regions offers several advantages:

1. Real-time Processing: YOLO11's architecture enables fast inference, making it ideal for applications requiring immediate counting results.
2. Flexible Region Definition: The solution allows you to define multiple custom regions as polygons, rectangles, or lines to suit your specific monitoring needs.
3. Multi-class Support: Count different object types simultaneously within the same regions, providing comprehensive analytics.
4. Integration Capabilities: Easily integrate with existing systems through the Ultralytics Python API or command-line interface.

Explore deeper benefits in the Advantages section.

### What are some real-world applications of object counting in regions?

Object counting with Ultralytics YOLO11 can be applied to numerous real-world scenarios:

- Retail Analytics: Count customers in different store sections to optimize layout and staffing.
- Traffic Management: Monitor vehicle flow in specific road segments or intersections.
- Manufacturing: Track products moving through different production zones.
- Warehouse Operations: Count inventory items in designated storage areas.
- Public Safety: Monitor crowd density in specific zones during events.

Explore more examples in the Real World Applications section and the TrackZone solution for additional zone-based monitoring capabilities.

📅 Created 1 year ago ✏️ Updated 4 months ago

## Comments