import numpy as np
boxes = np.array([[100, 100, 210, 210, 0.72],
[250, 250, 420, 420, 0.8],
[220, 220, 320, 330, 0.92],
[100, 100, 210, 210, 0.72],
[230, 240, 325, 330, 0.81],
[220, 230, 315, 340, 0.9]]) # (x1,y1,x2,y2,score)
def nms(boxes, threshold):
x1 = boxes[:, 0]
y1 = boxes[:, 1]
x2 = boxes[:, 2]
y2 = boxes[:, 3]
scores = boxes[:, 4]
areas = (x2-x1+1)*(y2-y1+1)
keep = []
idxs = scores.argsort()[::-1] # 从大到下排序,argsort函数返回从小到大的索引,[::-1]反序变成从大到小
while(idxs.size > 0):
i = idxs[0]
keep.append(i)
x11 = np.maximum(x1[i], x1[idxs[1:]])
y11 = np.maximum(y1[i], y1[idxs[1:]])
x22 = np.minimum(x2[i], y2[idxs[1:]])
y22 = np.minimum(y2[i], y2[idxs[1:]])
w = np.maximum(0, x22-x11+1)<