1. 程式人生 > >【NMS與IOU代碼】

【NMS與IOU代碼】

以及 索引 [] [1] while 更新 HERE append pre

# -*- coding: utf-8 -*-
import numpy as np
def IOU1(A,B):
    #左上右下坐標(x1,y1,x2,y2)
    w=max(0,min(A[2],B[2])-max(A[0],B[0]))
    h=max(0,min(A[3],B[3])-max(A[1],B[1]))
    areaA=(A[2]-A[0]+1)*(A[3]-A[1]+1)
    areaB=(B[2]-B[0]+1)*(B[3]-B[1]+1)
    inter=w*h
    union=areaA+areaB-inter
    return inter/union
def nms(dets, thresh): """Pure Python NMS baseline.""" #x1、y1、x2、y2、以及score賦值 x1 = dets[:, 0] y1 = dets[:, 1] x2 = dets[:, 2] y2 = dets[:, 3] scores = dets[:, 4] 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:重疊面積/(面積1+面積2-重疊面積) IOU = inter / (areas[i] + areas[order[1:]] - inter) #找到重疊度不高於閾值的矩形框索引 left_index = np.where(IOU <= thresh)[0] #將order序列更新,由於前面得到的矩形框索引要比矩形框在原order序列中的索引小1,所以要把這個1加回來 order = order[left_index + 1] print(keep) if __name__ == __main__: dets=[[0,0,100,101,0.9],[5,6,90,110,0.7],[17,19,80,120,0.8],[10,8,115,105,0.5]] dets=np.array(dets) nms(dets,0.5) print IOU1(dets[0],dets[2])

【NMS與IOU代碼】