In [19]:
def do_nms(boxes, nms_thresh): 
    # Non-maximal suppression
    if len(boxes) > 0: 
        nb_class = len(boxes[0].classes) 
    else: 
        return 

    for c in range(nb_class): 
        sorted_indices = np.argsort([-box.classes[c] for box in boxes]) 
 
        for i in range(len(sorted_indices)): 
            index_i = sorted_indices[i] 
            
            if boxes[index_i].classes[c] == 0: continue 
 
            for j in range(i+1, len(sorted_indices)): 
                index_j = sorted_indices[j] 
 
                if bbox_iou(boxes[index_i], boxes[index_j]) >= nms_thresh: 
                    boxes[index_j].classes[c] = 0 
In [20]:
# suppress non-maximal boxes
do_nms(boxes, 0.6)