7 """Suppress overlapping detections.
9 Original code from [1]_ has been adapted to include confidence score.
11 .. [1] http://www.pyimagesearch.com/2015/02/16/
12 faster-non-maximum-suppression-python/
17 >>> boxes = [d.roi for d in detections]
18 >>> scores = [d.confidence for d in detections]
19 >>> indices = non_max_suppression(boxes, max_bbox_overlap, scores)
20 >>> detections = [detections[i] for i in indices]
25 Array of ROIs (x, y, width, height).
26 max_bbox_overlap : float
27 ROIs that overlap more than this values are suppressed.
28 scores : Optional[array_like]
29 Detector confidence score.
34 Returns indices of detections that have survived non-maxima suppression.
40 boxes = boxes.astype(np.float)
45 x2 = boxes[:, 2] + boxes[:, 0]
46 y2 = boxes[:, 3] + boxes[:, 1]
48 area = (x2 - x1 + 1) * (y2 - y1 + 1)
49 if scores
is not None:
50 idxs = np.argsort(scores)
59 xx1 = np.maximum(x1[i], x1[idxs[:last]])
60 yy1 = np.maximum(y1[i], y1[idxs[:last]])
61 xx2 = np.minimum(x2[i], x2[idxs[:last]])
62 yy2 = np.minimum(y2[i], y2[idxs[:last]])
64 w = np.maximum(0, xx2 - xx1 + 1)
65 h = np.maximum(0, yy2 - yy1 + 1)
67 overlap = (w * h) / area[idxs[:last]]
71 ([last], np.where(overlap > max_bbox_overlap)[0])))