用Python写nms函数
时间: 2023-03-13 17:41:03 浏览: 102
NMS python代码
答案:我可以提供一些代码示例来帮助你写NMS函数:import numpy as npdef nms(boxes, scores, threshold): """Non-maximum suppression. Args: boxes: (np.array of floats) bounding boxes in (x1, y1, x2, y2) format, shape=(n, 4) scores: (np.array of floats) corresponding scores, shape=(n,) threshold: (float) threshold of overlap Returns: keep: (np.array of ints) indices of boxes that don't overlap too much with other boxes, shape=(k,) """ x1 = boxes[:, 0] y1 = boxes[:, 1] x2 = boxes[:, 2] y2 = boxes[:, 3] areas = (x2 - x1 + 1) * (y2 - y1 + 1) # keep score index of boxes scores_order = scores.argsort()[::-1] keep = [] while scores_order.size > 0: i = scores_order[0] keep.append(i) xx1 = np.maximum(x1[i], x1[scores_order[1:]]) yy1 = np.maximum(y1[i], y1[scores_order[1:]]) xx2 = np.minimum(x2[i], x2[scores_order[1:]]) yy2 = np.minimum(y2[i], y2[scores_order[1:]]) w = np.maximum(0.0, xx2 - xx1 + 1) h = np.maximum(0.0, yy2 - yy1 + 1) overlap = (w * h) / areas[scores_order[1:]] scores_order = scores_order[1:][overlap <= threshold] return np.array(keep)
阅读全文