roboflow/supervision · error · ValueError

Both Detections should have exactly 1 detected object.

Error message

Both Detections should have exactly 1 detected object.

What it means

merge_object_detection_pair (and its deprecated alias merge_inner_detection_object_pair) merges exactly two single-object Detections into one. It indexes xyxy[0] on each input, so both inputs must contain exactly one detection each; anything else raises this ValueError immediately.

Source

Thrown at src/supervision/detection/core.py:3424

    Example:
        ```python
        from supervision import _cv2 as cv2
        import supervision as sv
        from inference import get_model

        image = cv2.imread("<SOURCE_IMAGE_PATH>")
        model = get_model(model_id="yolov8s-640")

        result = model.infer(image)[0]
        detections = sv.Detections.from_inference(result)

        merged_detections = merge_object_detection_pair(
            detections[0], detections[1])
        ```
    """
    if len(detections_1) != 1 or len(detections_2) != 1:
        raise ValueError("Both Detections should have exactly 1 detected object.")

    _validate_fields_both_defined_or_none(detections_1, detections_2)

    xyxy_1 = detections_1.xyxy[0]
    xyxy_2 = detections_2.xyxy[0]
    if detections_1.confidence is None and detections_2.confidence is None:
        merged_confidence = None
    else:
        assert detections_1.confidence is not None
        assert detections_2.confidence is not None
        detection_1_area = (xyxy_1[2] - xyxy_1[0]) * (xyxy_1[3] - xyxy_1[1])
        detections_2_area = (xyxy_2[2] - xyxy_2[0]) * (xyxy_2[3] - xyxy_2[1])
        merged_confidence = (
            detection_1_area * detections_1.confidence[0]
            + detections_2_area * detections_2.confidence[0]
        ) / (detection_1_area + detections_2_area)
        merged_confidence = np.array([merged_confidence])

View on GitHub (pinned to 7f254d9784)

Solutions

  1. Slice each input to exactly one row: merge_object_detection_pair(detections_1[i:i+1], detections_2[j:j+1]) — note the i:i+1 slice form, not detections[i] alone unless that returns a length-1 Detections in your version.
  2. Check len(detections_1) == 1 and len(detections_2) == 1 before calling; skip or handle empty frames explicitly.
  3. If merging more than two overlapping objects, note this API is pair-only — use group_detections / merge_object_detections instead.

Example fix

# before
merged = merge_object_detection_pair(dets_a, dets_b)  # both multi-row

# after
assert len(dets_a) == 1 and len(dets_b) == 1, 'pair merge needs single-object inputs'
merged = merge_object_detection_pair(dets_a, dets_b)
Defensive patterns

Strategy: validation

Validate before calling

def assert_single_detection_pair(d1: sv.Detections, d2: sv.Detections) -> None:
    if len(d1) != 1 or len(d2) != 1:
        raise ValueError(
            f'pair merge needs len 1 inputs, got {len(d1)} and {len(d2)}'
        )

assert_single_detection_pair(dets_a, dets_b)
merged = merge_object_detection_pair(dets_a, dets_b)

Type guard

def is_single_detection(d: sv.Detections) -> bool:
    return isinstance(d, sv.Detections) and len(d) == 1

Try / catch

try:
    merged = merge_object_detection_pair(d1, d2)
except ValueError as e:
    if 'exactly 1 detected object' in str(e):
        merged = d1 if len(d1) else d2  # or log & skip frame
    else:
        raise

Prevention

When it happens

Trigger: Calling merge_object_detection_pair(detections_1, detections_2) where len(detections_1) != 1 or len(detections_2) != 1 — e.g. passing full multi-object Detections from model.infer(), passing Detections.empty(), or passing slices like detections[0:2] that keep 2 rows.

Common situations: Copying the docstring example but forgetting that detections[0] (single index) yields a 1-row Detections while detections[0:2] does not; passing an empty result from a frame with no objects; piping tracker/model output straight into the merge helper.

Related errors


AI-assisted analysis of roboflow/supervision@7f254d9784 (2026-08-15). Data as JSON: /api/errors/9c6799f38942d136. Report an issue: GitHub.