如何对所有列组合的两个指标之间进行计算?

我有以下尝试来计算从面部检测器获得的几次检测的IOU。

import numpy as np


class BboxUtils(object):
    @staticmethod
    def ious(bboxes1, bboxes2):
        left1, top1, right1, bottom1 = BboxUtils.bbox_to_perimiters(bboxes1)
        left2, top2, right2, bottom2 = BboxUtils.bbox_to_perimiters(bboxes2)

        area1 = (right1 - left1) * (top1 - bottom1)
        area2 = (right2 - left2) * (top2 - bottom2)

        intersection_left = np.maximum(left1, left2)
        intersection_right = np.minimum(right1, right2)
        intersection_top = np.maximum(top1, top2)
        intersection_bottom = np.minimum(bottom1, bottom2)
        intersection_area = (intersection_right - intersection_left) * (intersection_top - intersection_bottom)
        intersection_area[intersection_area < 0] = 0

        iou = intersection_area / (area1 + area2 - intersection_area)

        return iou

    @staticmethod
    def bbox_to_perimiters(bboxes):
        left, w, top, h = np.split(bboxes.reshape(-1), 4)
        right = left + w
        bottom = top + h
        return left, right, top, bottom


# example usage:
detections1 = my_detector.detect(frame1) #np.array of shape (n1, 4)
detections2 = my_detector.detect(frame2) #np.array of shape (n2, 4)
ious = BboxUtils.ious(detections1, detections2)

此代码假定:

  1. The detections on the 2 frames (bboxes1 and bboxes2) are of the same length
  2. Each detection's index is the same in bboxes1 and bboxes2

我想使用与上面的代码相同的逻辑来计算IOU,但要避免for循环。

Notice bboxes1 and bboxes2 can be matrices of shape (n1, 4) and (n2, 4), where n1 is not necessarily equal n2.

如何才能做到这一点?

最后,可能有一个已经完成所有工作的库。 如果确实存在,请转给我。