一段计算IoU的代码
之前偶然间看到达摩院有个团队招Research Intern,跟我方向很相近,就投了个简历。这几天接到了电话面试,不得不说阿里的面试好长啊,技术面就有六轮。今天第三轮考了个代码,感觉挺简单的,写完了后一查,好像网上还有一些错的,就把自己写的放上来吧,因为博客荒废许久了,想借此机会重新更新,算是驱使自己文献阅读时好好做一下记录吧。
import numpy as np
def cal_iou(box_a, box_b):
np_x =np.array([box_a[0],box_b[0]])
np_w =np.array([box_a[2],box_b[2]])
np_y =np.array([box_a[1],box_b[1]])
np_h =np.array([box_a[3],box_b[3]])
if (abs(box_a[0] - box_b[0]) < np_w[np_x.argmin()]) and (abs(box_a[1] - box_b[1]) < np_h[np_y.argmin()]):
box_area = box_a[2]*box_a[3] + box_b[2]*box_b[3]
q_area =(np_x.min() + np_w[np_x.argmin()]-np_x.max()) * (np_y.min() + np_h[np_y.argmin()]-np_y.max())
iou_c = q_area/(box_area - q_area)
print("iou calculated")
else:
print("No intersection!!")
iou_c = 0
return iou_c
a = np.array([10,10,50,50])
b = np.array([30,30,40,40])
iou = cal_iou(a,b)
print (iou)
a = np.array([10,10,40,5])
b = np.array([5,20,40,40])
iou = cal_iou(a,b)
print (iou)