NMS代码实现

def iou(the_first_score_location, the_rest_score_location):
    if the_first_score_location[0] > the_rest_score_location[0] and the_first_score_location[0] > \
            the_rest_score_location[2]:
        return 0

    elif the_first_score_location[2] < the_rest_score_location[0] and the_first_score_location[2] < \
            the_rest_score_location[2]:
        return 0

    elif the_first_score_location[1] > the_rest_score_location[1] and the_first_score_location[1] > \
            the_rest_score_location[3]:
        return 0

    elif the_first_score_location[3] < the_rest_score_location[1] and the_first_score_location[3] < \
            the_rest_score_location[3]:
        return 0

    x_location_past = []
    y_location_past = []
    x_location_past.append(the_first_score_location[0])
    x_location_past.append(the_first_score_location[2])
    x_location_past.append(the_rest_score_location[0])
    x_location_past.append(the_rest_score_location[2])
    y_location_past.append(the_first_score_location[1])
    y_location_past.append(the_first_score_location[3])
    y_location_past.append(the_rest_score_location[1])
    y_location_past.append(the_rest_score_location[3])
    x_location_new = sorted(x_location_past)
    y_location_new = sorted(y_location_past)
    w = x_location_new[2] - x_location_new[1]
    h = y_location_new[2] - y_location_new[1]
    union_set = w * h
    the_first_score_location_area = (the_first_score_location[2] - the_first_score_location[0]) * (
            the_first_score_location[3] - the_first_score_location[1])
    the_rest_score_location_area = (the_rest_score_location[2] - the_rest_score_location[0]) * (
            the_rest_score_location[3] - the_rest_score_location[1])
    intersection = the_first_score_location_area + the_rest_score_location_area - union_set
    iou_area = union_set / intersection
    return iou_area

animal = [(0, 0, 100, 100, 0.92, 0), (10, 10, 90, 90, 0.94, 0), (20, 20, 110, 110, 0.89, 0),
          (20, 20, 120, 120, 0.82, 0),
          (150, 20, 250, 120, 0.83, 0), (160, 30, 260, 130, 0.73, 0), (200, 200, 300, 300, 0.91, 1),
          (210, 210, 310, 310, 0.93, 1)]
threshold = 0.3
the_choose_location = []

while True:
    animal_index_and_value_outside = [one for one in enumerate(animal)]
    the_first_location = []
    animal_index_and_value_sort = sorted(animal_index_and_value_outside, key=lambda x: x[1][4], reverse=True)
    the_choose_location.append(animal_index_and_value_sort[0][1])
    the_first_location.append(animal_index_and_value_sort[0][1])
    animal_index_and_value_outside.remove(animal_index_and_value_sort[0])
    animal.remove(animal_index_and_value_sort[0][1])
    the_rest_location = []
    for i in animal_index_and_value_outside:
        if i[1][5] == animal_index_and_value_sort[0][1][5]:
            the_rest_location.append(i[1])
    for i in the_rest_location:
        if iou(the_first_location[0][0:4],i[0:4])>threshold:
            animal.remove(i)
    if len(animal)==0:
        break

print(the_choose_location)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NMS(Non-Maximum Suppression)是目标检测中一个常用的后处理步骤,用于抑制重叠的检测框,保留最有可能是真实目标的检测框。以下是Python实现NMS的示例代码: ```python def nms(boxes, scores, threshold=0.5): """ 进行非极大值抑制 :param boxes: 检测框列表 :param scores: 检测框对应的分数列表 :param threshold: 阈值 :return: 返回非极大值抑制后的检测框和分数 """ # 获取检测框的坐标 x1 = boxes[:, 0] y1 = boxes[:, 1] x2 = boxes[:, 2] y2 = boxes[:, 3] # 计算检测框的面积 areas = (x2 - x1 + 1) * (y2 - y1 + 1) # 对分数进行排序 order = scores.argsort()[::-1] # 保留下来的检测框和分数 keep = [] while order.size > 0: # 取出分数最高的检测框 i = order[0] keep.append(i) # 计算该检测框与其他检测框的重叠部分的面积 xx1 = np.maximum(x1[i], x1[order[1:]]) yy1 = np.maximum(y1[i], y1[order[1:]]) xx2 = np.minimum(x2[i], x2[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 # 计算重叠部分的面积占两个检测框面积的比例 iou = inter / (areas[i] + areas[order[1:]] - inter) # 找出IoU小于阈值的检测框 inds = np.where(iou <= threshold)[0] # 更新order order = order[inds + 1] # 返回非极大值抑制后的检测框和分数 return keep, scores[keep] ``` 其中,boxes是一个n×4的数组,每行表示一个检测框的坐标,scores是一个n×1的数组,表示每个检测框的分数。threshold是阈值,当两个检测框的IoU大于该阈值时,将分数较低的检测框抑制掉。函数返回保留下来的检测框的索引和分数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值