小米算法NMS
小米9.15算法笔试,NMS那题只A了43%。是代码有什么问题吗?
import operator def iou(box0, box1): area0 = (box0[2] - box0[0]) * (box0[3] - box0[1]) area1 = (box1[2] - box1[0]) * (box1[3] - box1[1]) inter_x0 = max(box0[0], box1[0]) inter_x1 = min(box0[2], box1[2]) inter_y0 = max(box0[1], box1[1]) inter_y1 = min(box0[3], box1[3]) inter_area = max(inter_x1 - inter_x0, 0) * max(inter_y1 - inter_y0, 0) res = inter_area / (area0 + area1 - inter_area) return res def NMS(bboxes, th): bboxes.sort(key=operator.itemgetter(4)) res = [] while bboxes: box0 = bboxes.pop() bboxes = [box1 for box1 in bboxes if iou(box0, box1) <= th] res.append(box0) return res n, th = input().split() n, th = int(n), float(th) bboxes = [tuple([float(x) for x in input().split()]) for _ in range(n)] bboxes = NMS(bboxes, th) for box in bboxes: for x in box: print('{}'.format(x), end=' ') print()