-
Notifications
You must be signed in to change notification settings - Fork 1
/
spatial_similarity_refPoints.py
75 lines (66 loc) · 2.75 KB
/
spatial_similarity_refPoints.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import car_counting_refPoints as counting_cars
def get_iou(bb1, bb2):
"""
source: https://stackoverflow.com/a/42874377
Calculate the Intersection over Union (IoU) of two bounding boxes.
Parameters
----------
bb1 : dict
Keys: {'x1', 'x2', 'y1', 'y2'}
The (x1, y1) position is at the top left corner,
the (x2, y2) position is at the bottom right corner
bb2 : dict
Keys: {'x1', 'x2', 'y1', 'y2'}
The (x, y) position is at the top left corner,
the (x2, y2) position is at the bottom right corner
Returns
-------
float
in [0, 1]
"""
assert bb1['x1'] < bb1['x2']
assert bb1['y1'] < bb1['y2']
assert bb2['x1'] < bb2['x2']
assert bb2['y1'] < bb2['y2']
# determine the coordinates of the intersection rectangle
x_left = max(bb1['x1'], bb2['x1'])
y_top = max(bb1['y1'], bb2['y1'])
x_right = min(bb1['x2'], bb2['x2'])
y_bottom = min(bb1['y2'], bb2['y2'])
if x_right < x_left or y_bottom < y_top:
return 0.0
# The intersection of two axis-aligned bounding boxes is always an
# axis-aligned bounding box
intersection_area = (x_right - x_left) * (y_bottom - y_top)
# compute the area of both AABBs
bb1_area = (bb1['x2'] - bb1['x1']) * (bb1['y2'] - bb1['y1'])
bb2_area = (bb2['x2'] - bb2['x1']) * (bb2['y2'] - bb2['y1'])
# compute the intersection over union by taking the intersection
# area and dividing it by the sum of prediction + ground-truth
# areas - the interesection area
iou = intersection_area / float(bb1_area + bb2_area - intersection_area)
assert iou >= 0.0
assert iou <= 1.0
return iou
def area(bbox):
return (bbox["x2"]-bbox["x1"]) * (bbox["y2"]-bbox["y1"])
def similarityBBoxObject(det1, det2):
'''
calculate the similarity of two bounding boxes
-> the more similiar two bounding boxes are,
the more likely it is that they are of the same car
'''
detection1 = det1.bbox
detection2 = det2.bbox
bbox_1 = dict({"x1": detection1.startX, "y1": detection1.startY,
"x2": detection1.endX, "y2": detection1.endY})
bbox_2 = dict({"x1": detection2.startX, "y1": detection2.startY,
"x2": detection2.endX, "y2": detection2.endY})
area_ratio = counting_cars.ratio(area(bbox_1), area(bbox_2))
x_sim = counting_cars.ratio((bbox_1["x1"]+bbox_1["x2"])/2, (bbox_2["x1"]+bbox_2["x2"])/2)
y_sim = counting_cars.ratio((bbox_1["y1"]+bbox_1["y2"])/2, (bbox_2["y1"]+bbox_2["y2"])/2)
centroid_sim = (x_sim + y_sim) / 2
'''if get_iou(bbox_1, bbox_2) < 0.1:
print((area_ratio+centroid_sim)/2 * get_iou(bbox_1, bbox_2))'''
#return get_iou(bbox_1, bbox_2)
return (area_ratio+centroid_sim)/2 * get_iou(bbox_1, bbox_2)