ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

numpy实现NMS

2022-01-08 11:06:01  阅读:191  来源: 互联网

标签:index NMS 实现 x2 y1 np dets y2 numpy


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有