nms算法实现(带示例)

nms完整代码测试示例:
可直接运行

# -*- coding: utf-8 -*-
import numpy as np

def solution(boxes, probThresh, nmsThresh):
    boxes = np.array(boxes)
    x1 = boxes[:, 0]
    y1 = boxes[:, 1]
    x2 = boxes[:, 2]
    y2 = boxes[:, 3]
    scores = boxes[:, 4]

    areas = (x2 - x1 + 1) * (y2 - y1 + 1)
    order = np.argsort(scores)
    tmp = []
    while order.size > 0:
        i = order[-1]
        tmp.append(boxes[i])
        xx1 = np.maximum(x1[i], x1[order[:-1]])
        yy1 = np.maximum(y1[i], y1[order[:-1]])
        xx2 = np.minimum(x2[i], y2[order[:-1]])
        yy2 = np.minimum(y2[i], y2[order[:-1]])

        w = np.maximum(0.0, xx2 - xx1 + 1)
        h = np.maximum(0.0, yy2 - yy1 + 1)
        inter = w * h
        over = inter / (areas[i] + areas[order[:-1]]-inter)
        inds = np.where(over <= nmsThresh)
        order = order[inds]
    return tmp
def main():
    boxes = [[100, 100, 200, 200, 0.5],
             [120, 120, 220, 220, 0.7],
             [101, 101, 201, 201, 0.8],
             [90, 95, 192, 192, 0.77],
             [99, 101, 166, 170, 0.79],
             [98, 121, 199, 222, 0.8]]
    ret = solution(boxes, 0.2, 0.5)
    print(ret)


if __name__ == "__main__":
    main()

输出结果:

[array([ 98. , 121. , 199. , 222. ,   0.8]), array([ 99.  , 101.  , 166.  , 170.  ,   0.79])]

np.maximum(X, Y, out=None)

*X和Y逐位进行比较,选择最大值.
最少接受两个参数*

ex:

np.maximum([-3, -2, 0, 1, 2], 0)
array([0, 0, 0, 1, 2])

猜你喜欢

转载自blog.csdn.net/WYKB_Mr_Q/article/details/118370784
NMS