Computer vision answers · Object Counting

How do I stop computer vision from double-counting objects that overlap or touch?

Without a count that jumps every time two items touch.

Short answer

Train an object detection model on Roboflow for free with one tight box per object, including objects that touch, then remove duplicate boxes with class-agnostic non-maximum suppression before you count. On video, track each object and count it once when it crosses a line, not once per frame. Measure overlap against the smaller box, so a duplicate box sitting inside an object is removed but two neighbors that only touch both stay.

Roboflow agent · free to start

Skip the setup. Describe your images.

Describe your images and what you need to find. The agent sets up labeling, training and deployment with you.

Key facts

What you need
Images or video of the objects, and a detection model trained on them
What you get
One detection per object, so each count matches what is there
Image sources
Cameras, phone photos, scans and recorded video
Runs on
Cloud API, or an edge device or server running Docker
Overhead view of a pallet loaded with 9 cardboard cartons and 3 gray plastic totes, all outlined with object detection bounding boxes and labels.

How to build object counting without double counts, step by step

Six steps from images of crowded objects to a count with one entry per object.

  1. Connect Roboflow to your coding agent, ChatGPT or Codex

    Add the Roboflow MCP server to Claude, Cursor or Gemini CLI, or install the Roboflow plugin in ChatGPT or Codex. Your assistant can then upload and auto-label images, start training, build the Workflow and run the model from a prompt, at no extra cost beyond your Roboflow plan.

  2. Label every object, even where objects touch

    Draw one tight box around every object, including objects that touch or partly hide each other, and never one box around a group. Roboflow Annotate can pre-label images with SAM 3, so you correct boxes instead of drawing each one. Where objects overlap heavily, label polygons and train an instance segmentation model, so each object keeps its own outline.

  3. Train a detection model

    Train an object detection model such as RF-DETR on your labeled images with Roboflow Train. Include crowded images in the training set so the model learns to separate neighbors.

  4. Remove duplicate boxes before you count

    Run the model and apply non-maximum suppression across all classes, so an object predicted twice, or as two classes, counts once. Measuring overlap against the smaller box removes a box nested inside another, while two objects that only touch both stay.

    Python
    import supervision as sv
    from collections import Counter
    from inference import get_model
    
    model = get_model("your-project/1")
    
    result = model.infer("shelf.jpg", confidence=0.4)[0]
    detections = sv.Detections.from_inference(result)
    detections = detections.with_nms(
        threshold=0.5, class_agnostic=True, overlap_metric=sv.OverlapMetric.IOS
    )
    
    print(len(detections), Counter(detections.data["class_name"]))
    # 24 Counter({'carton': 18, 'tote': 6})

    Rather not write code? Ask the Roboflow agent to set this up for you

  5. Count each object once in video

    Build a Workflow with a Byte Tracker, which gives each object an ID across frames, and a Line Counter, which counts each ID once when it crosses a line. Counting the detections in every frame would add the same object again on each frame.

  6. Check the counts and deploy

    Compare the model's counts with hand counts on a few crowded images, and adjust the confidence and overlap thresholds until they match. Run it on images or a camera stream with Roboflow Deploy in the cloud or on an edge device.

Why 2 million engineers build on Roboflow

  • Label faster

    Auto-label with SAM 3 or your own model, then fix the misses instead of drawing every box.

  • RF-DETR

    Roboflow’s open-source real-time detection model, built for accuracy (opens in a new tab) on small objects.

  • Logic built in

    Counting, zones, tracking and alerts are ready-made Workflow blocks, so there is less code to write.

  • Your model, your weights

    Use your trained weights on every plan, including free, through the open-source Inference package.

  • Run it anywhere

    Self-host on a workstation, Docker, an on-prem GPU or an edge device, or call the cloud API.