目标检测后处理之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]

版权声明:本文为CSDN博主「freedomUSTB」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/freedomUSTB/article/details/122843859

freedomUSTB

我还没有学会写个人说明!

暂无评论

发表评论

相关推荐