目标检测后处理之NMS

  1. 给定boxes(xmin、ymin、xmax、ymax)、scores、conf_thresh、nms_thresh、topk等参数,对目标框进行非极大值抑制(NMS)操作。
  2. 闲话少说,直接上代码:
import numpy as np

def py_nms_cpu(boxes, scores, nms_thresh):
    xmin = boxes[:, 0]
    ymin = boxes[:, 1]
    xmax = boxes[:, 2]
    ymax = boxes[:, 3]
    areas = (xmax - xmin + 1) * (ymax - ymin + 1)
    order = scores.argsort()[::-1]

    keep = []
    while order.size > 0:
        i = order[0]
        keep.append(i)
        xxmin = np.maximum(xmin[i], xmin[order[1:]])
        yymin = np.maximum(ymin[i], ymin[order[1:]])
        xxmax = np.minimum(xmax[i], xmax[order[1:]])
        yymax = np.minimum(ymax[i], ymax[order[1:]])
        
        ww = np.maximum(0.0, xxmax - xxmin + 1)
        hh = np.maximum(0.0, yymax - yymin + 1)
        inter = ww * hh
        ovr = inter / (areas[i] + areas[order[1:]] - inter)
        
        inds = np.where(ovr <= nms_thresh)[0]
        order = order[inds + 1]
    
    return keep
    
# conf_thresh
inds = np.where(scores > conf_thresh)[0]
scores = scores[inds]
boxes = boxes[inds]
# topk
order = scores.argsort()[::-1][:topk]
scores = scores[order]
boxes = boxes[order]
# do nms
keep = py_nms_cpu(boxes, scores, nms_thresh)
boxes = boxes[keep]
scores = scores[keep]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值