def non_maximum_suppression(
    preds: dict[str, np.ndarray], iou_threshold: float, confidence_threshold: float | None = None
) -> dict[str, np.ndarray]:
    """
    Perform non-maximum suppression on the predicted object detection labels of a single image.

    :param preds: Predicted labels of format `dict[str, np.ndarray]` for a single image. The fields of the dict are
                  as follows:

                  - boxes [N, 4]: the boxes in [x1, y1, x2, y2] format, with 0 <= x1 < x2 <= W and 0 <= y1 < y2 <= H.
                  - labels [N]: the labels for each image.
                  - scores [N]: the scores of each prediction.
    :param iou_threshold: The IoU threshold to discard overlapping bounding boxes.
    :param confidence_threshold: The confidence threshold to discard bounding boxes.
    return: Filtered predicted labels of the single image in the same format as the input.
    """
    boxes = preds["boxes"]
    labels = preds["labels"]
    scores = preds["scores"]

    # Filter out bounding boxes below confidence threshold
    if confidence_threshold is not None:
        mask = scores >= confidence_threshold
        boxes = boxes[mask]
        labels = labels[mask]
        scores = scores[mask]

    # Candidate bounding boxes
    keep_indices = []
    indices = np.argsort(scores)[::-1]

    while len(indices) > 0:
        # Get first bounding box
        current_idx = indices[0]
        box_1 = boxes[current_idx]
        label_1 = labels[current_idx]

        keep_indices.append(current_idx)
        remove_indices = [0]

        # Find overlapping bounding boxes above IoU threshold
        for i, idx in enumerate(indices[1:], start=1):
            box_2 = boxes[idx]
            label_2 = labels[idx]

            if label_1 != label_2:
                continue

            iou = intersection_over_union(box_1, box_2)
            if iou >= iou_threshold:
                remove_indices.append(i)

        # Remove overlapping bounding boxes from candidates
        indices = np.delete(indices, remove_indices)

    filtered_preds = {
        "boxes": boxes[keep_indices],
        "labels": labels[keep_indices],
        "scores": scores[keep_indices],
    }
    return filtered_preds

    Perform non-maximum suppression on the predicted object detection labels of a single image.

    :param preds: Predicted labels of format `dict[str, np.ndarray]` for a single image. The fields of the dict are
                  as follows:

                  - boxes [N, 4]: the boxes in [x1, y1, x2, y2] format, with 0 <= x1 < x2 <= W and 0 <= y1 < y2 <= H.
                  - labels [N]: the labels for each image.
                  - scores [N]: the scores of each prediction.
    :param iou_threshold: The IoU threshold to discard overlapping bounding boxes.
    :param confidence_threshold: The confidence threshold to discard bounding boxes.
    return: Filtered predicted labels of the single image in the same format as the input.
    
art/utils.py