defiou(box, boxes, isMin =False):
area =(box[2]- box[0])*(box[3]- box[1])# 第一个框和剩下 所有的框比
areas =(boxes[:,2]- boxes[:,0])*(boxes[:,3]- boxes[:,1])
x1 = np.maximum(box[0], boxes[:,0])# 交集坐标
y1 = np.maximum(box[1], boxes[:,1])
x2 = np.minimum(box[2], boxes[:,2])
y2 = np.minimum(box[3], boxes[:,3])
w = np.maximum(0, x2 - x1)# 判断交集,无交集时宽高为0
h = np.maximum(0, y2 - y1)
inter = w * h # 交集面积if isMin:# 是否大框套小框
ovr = np.true_divide(inter, np.minimum(area, areas))else:
ovr = np.true_divide(inter,(areas + area - inter))return ovr
▶NMS
defpy_cpu_nms(dets, thresh, ismin=False):if dets.shape[0]==0:returnFalse
x1 = dets[:,0]
y1 = dets[:,1]
x2 = dets[:,2]
y2 = dets[:,3]
areas =(y2 - y1 +1)*(x2 - x1 +1)
scores = dets[:,4]
keep =[]
index = scores.argsort()[::-1]while index.size >0:
i = index[0]# every time the first is the biggst, and add it directly
keep.append(i)
x11 = np.maximum(x1[i], x1[index[1:]])# calculate the points of overlap
y11 = np.maximum(y1[i], y1[index[1:]])
x22 = np.minimum(x2[i], x2[index[1:]])
y22 = np.minimum(y2[i], y2[index[1:]])
w = np.maximum(0, x22 - x11 +1)# the weights of overlap
h = np.maximum(0, y22 - y11 +1)# the height of overlap
overlaps = w * h
if ismin:
ious = overlaps / np.maximum(areas[i], areas[index[1:]])else:
ious = overlaps /(areas[i]+ areas[index[1:]]- overlaps)
idx = np.where(ious <= thresh)[0]
index = index[idx +1]# because index start from 1return keep