-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiou.py
54 lines (44 loc) · 1.72 KB
/
iou.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
import numpy as np
from utils import get_data, check_results
def calculate_iou(gt_bbox, pred_bbox):
"""
calculate iou
args:
- gt_bbox [array]: 1x4 single gt bbox
- pred_bbox [array]: 1x4 single pred bbox
returns:
- iou [float]: iou between 2 bboxes
"""
xmin = np.max([gt_bbox[0], pred_bbox[0]])
ymin = np.max([gt_bbox[1], pred_bbox[1]])
xmax = np.min([gt_bbox[2], pred_bbox[2]])
ymax = np.min([gt_bbox[3], pred_bbox[3]])
intersection = max(0, xmax - xmin + 1) * max(0, ymax - ymin + 1)
gt_area = (gt_bbox[2] - gt_bbox[0]) * (gt_bbox[3] - gt_bbox[1])
pred_area = (pred_bbox[2] - pred_bbox[0]) * (pred_bbox[3] - pred_bbox[1])
union = gt_area + pred_area - intersection
return intersection / union
def calculate_ious(gt_bboxes, pred_bboxes):
"""
calculate ious between 2 sets of bboxes
args:
- gt_bboxes [array]: Nx4 ground truth array
- pred_bboxes [array]: Mx4 pred array
returns:
- iou [array]: NxM array of ious
"""
ious = np.zeros((gt_bboxes.shape[0], pred_bboxes.shape[0]))
for i, gt_bbox in enumerate(gt_bboxes):
for j, pred_bbox in enumerate(pred_bboxes):
ious[i,j] = calculate_iou(gt_bbox, pred_bbox)
return ious
if __name__ == "__main__":
ground_truth, predictions = get_data()
# get bboxes array
filename = 'segment-1231623110026745648_480_000_500_000_with_camera_labels_38.png'
gt_bboxes = [g['boxes'] for g in ground_truth if g['filename'] == filename][0]
gt_bboxes = np.array(gt_bboxes)
pred_bboxes = [p['boxes'] for p in predictions if p['filename'] == filename][0]
pred_boxes = np.array(pred_bboxes)
ious = calculate_ious(gt_bboxes, pred_boxes)
check_results(ious)