numpy实现NMS
作者:互联网
import numpy as np
import matplotlib.pyplot as plt
def py_cpu_nms(dets, thresh):
x1 = dets[:, 0]
y1 = dets[:, 1]
x2 = dets[:, 2]
y2 = dets[:, 3]
scores = dets[:, 4]
areas = (x2-x1+1)*(y2-y1+1)
res = []
index = scores.argsort()[::-1]
while index.size>0:
i = index[0]
res.append(i)
x11 = np.maximum(x1[i],x1[index[1:]])
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)
h = np.maximum(0,y22-y11+1)
overlaps = w * h
iou = overlaps/(areas[i]+areas[index[1:]]-overlaps)
idx = np.where(iou<=thresh)[0]
index = index[idx+1]
print(res)
return res
def plot_boxs(box,c):
x1 = box[:, 0]
y1 = box[:, 1]
x2 = box[:, 2]
y2 = box[:, 3]
plt.plot([x1,x2],[y1,y1],c)
plt.plot([x1,x2],[y2,y2],c)
plt.plot([x1,x1],[y1,y2],c)
plt.plot([x2,x2],[y1,y2],c)
if __name__ == '__main__':
boxes = np.array([[100, 100, 210, 210, 0.72],
[250, 250, 420, 420, 0.8],
[220, 220, 320, 330, 0.92],
[230, 240, 325, 330, 0.81],
[220, 230, 315, 340, 0.9]])
plt.figure()
ax1 = plt.subplot(121)
ax2 = plt.subplot(122)
plt.sca(ax1)
plot_boxs(boxes,'k')
res = py_cpu_nms(boxes,0.7)
plt.sca(ax2)
plot_boxs(boxes[res],'r')
plt.show()
标签:index,NMS,实现,x2,y1,np,dets,y2,numpy 来源: https://blog.csdn.net/EMIvv/article/details/122377369