3D NMS(Non-Maximum Suppression)
import numpy as np
def nms(x, nms_th):
if len(x) == 0:
return x
x = x[np.argsort(-x[:, 0])]
bboxes = [x[0]]
for i in np.arange(1, len(x)):
bbox = x[i]
flag = 1
for j in range(len(bboxes)):
if iou(bbox[1:5], bboxes[j][1:5]) > nms_th:
flag = -1
break
if flag == 1:
bboxes.append(bbox)
bboxes = np.asarray(bboxes, np.float32)
return bboxes
3D IoU (Intersection over Union)
def IoU(box0, box1):
r0 = box0[3] / 2
s0 = box0[:3] - r0
e0 = box0[:3] + r0
r1 = box1[3] / 2
s1 = box1[:3] - r1
e1 = box1[:3] + r1
overlap = [max(0, min(e0[i], e1[i]) - max(s0[i], s1[i])) for i in range(3)]
intersection = reduce(lambda x,y:x*y, overlap)
union = pow(box0[3], 3) + pow(box1[3], 3) - intersection
return intersection / union