det iou计算偏慢的问题
Created by: WenmuZhou
在这一行里,https://github.com/PaddlePaddle/PaddleOCR/blob/develop/tools/eval_utils/eval_det_iou.py#L140
使用的是shapely.geometry.Polygon
包来计算两个框的iou,但是当gt和pred的框都多达数百之后,计算效率就会急剧下降,一张图片需要1s+,甚至更久,请问有什么好的解决办法吗?
对于倾斜矩形的计算,我写了下面的方法,速度会快很多,但是精度会不一致
def iou_rotate(box_a, box_b, method='union'):
rect_a = cv2.minAreaRect(box_a)
rect_b = cv2.minAreaRect(box_b)
r1 = cv2.rotatedRectangleIntersection(rect_a, rect_b)
if r1[0] == 0:
return 0
else:
inter_area = cv2.contourArea(r1[1])
area_a = cv2.contourArea(box_a)
area_b = cv2.contourArea(box_b)
union_area = area_a + area_b - inter_area
if union_area == 0 or inter_area == 0:
return 0
if method == 'union':
iou = inter_area / union_area
elif method == 'intersection':
iou = inter_area / min(area_a, area_b)
else:
raise NotImplementedError
return iou