import math
import numpy
def iou(box1,box2):
x1,y1,x2,y2=box1
x3,y3,x4,y4=box2
area1=max(0,x2-x1+1)*max(0,y2-y1+1)
area2=max(0,x4-x3+1)*max(0,y4-y3+1)
xx1=max(x1,x3)
yy1=max(y1,y3)
xx2=min(x2,x4)
yy2=min(y2,y4)
w=max(0,xx2-xx1+1)
h=max(0,yy2-yy1+1)
inter=w*h
u=area1+area2-inter
return inter/u
def giou(box1,box2):
x1, y1, x2, y2 = box1
x3, y3, x4, y4 = box2
area1 = max(0, x2 - x1 + 1) * max(0, y2 - y1 + 1)
area2 = max(0, x4 - x3 + 1) * max(0, y4 - y3 + 1)
xx1 = max(x1, x3)
yy1 = max(y1, y3)
xx2 = min(x2, x4)
yy2 = min(y2, y4)
ae_x1=min(x1,x3)
ae_y1=min(y1,y3)
ae_x2=max(x2,x4)
ae_y2=max(y2,y4)
inter=max(0,xx2-xx1+1)*max(0,yy2-yy1+1)
u = area1 + area2 - inter
ae=max(0,ae_x2-ae_x1+1)*max(ae_y2-ae_y1+1)
return inter/u-(ae-u)/ae
def diou(box1,box2):
x1, y1, x2, y2 = box1
x3, y3, x4, y4 = box2
area1 = max(0, x2 - x1 + 1) * max(0, y2 - y1 + 1)
area2 = max(0, x4 - x3 + 1) * max(0, y4 - y3 + 1)
c1_x,c1_y=(x2+x1)/2,(y2+y1)/2
c2_x,c2_y=(x4+x3)/2,(y4+y3)/2
p2=(c2_x-c1_x)*(c2_x-c1_x)+(c2_y-c1_y)*(c2_y-c1_y)
xx1 = max(x1, x3)
yy1 = max(y1, y3)
xx2 = min(x2, x4)
yy2 = min(y2, y4)
ae_x1=min(x1,x3)
ae_y1=min(y1,y3)
ae_x2=max(x2,x4)
ae_y2=max(y2,y4)
inter=max(0,xx2-xx1+1)*max(0,yy2-yy1+1)
u = area1 + area2 - inter
c2=(ae_x2-ae_x1)*(ae_x2-ae_x1)+(ae_y2-ae_y1)*(ae_y2-ae_y1)
return inter/u-p2/c2
def ciou(box1, box2,alpha):
x1, y1, x2, y2 = box1
x3, y3, x4, y4 = box2
w1,h1=max(x2-x1+1,0),max(y2-y1+1,1e-15)
w2, h2 = max(x4 - x3 + 1, 0), max(y4 - y3 + 1, 1e-15)
area1 = max(0, x2 - x1 + 1) * max(0, y2 - y1 + 1)
area2 = max(0, x4 - x3 + 1) * max(0, y4 - y3 + 1)
c1_x, c1_y = (x2 + x1) / 2, (y2 + y1) / 2
c2_x, c2_y = (x4 + x3) / 2, (y4 + y3) / 2
p2 = (c2_x - c1_x) * (c2_x - c1_x) + (c2_y - c1_y) * (c2_y - c1_y)
xx1 = max(x1, x3)
yy1 = max(y1, y3)
xx2 = min(x2, x4)
yy2 = min(y2, y4)
ae_x1 = min(x1, x3)
ae_y1 = min(y1, y3)
ae_x2 = max(x2, x4)
ae_y2 = max(y2, y4)
inter = max(0, xx2 - xx1 + 1) * max(0, yy2 - yy1 + 1)
u = area1 + area2 - inter
c2 = (ae_x2 - ae_x1) * (ae_x2 - ae_x1) + (ae_y2 - ae_y1) * (ae_y2 - ae_y1)
v=4*((numpy.arctan(w1/h1)-numpy.arctan(w2/h2))**2)/(numpy.pi**2)
return inter / u - p2 / c2-alpha*v
def nms(dets,thre):
x1,y1,x2,y2,s=dets[:0],dets[:1],dets[:2],dets[:3],dets[:4]
order=numpy.argsort(s)[::-1]
keep=[]
area=(x2-x1+1)*(y2-y1+1)
while order:
i=order[0]
keep.append(i)
xx1=numpy.maximum(x1[i],x1[order[1:]])
yy1 = numpy.maximum(y1[i], y1[order[1:]])
xx2 = numpy.minimum(x2[i], x2[order[1:]])
yy2 = numpy.minimum(y2[i], y2[order[1:]])
w=numpy.maximum(xx2-xx1+1,0)
h=numpy.maximum(yy2-yy1+1,0)
inter=w*h
iou=inter/(area[i]+area[order[1:]]-inter)
inds=numpy.where(iou<=thre)[0]
order=order[inds+1]
return keep