-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.py
70 lines (57 loc) · 2.23 KB
/
utils.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
from numpy.random import randint
roaddamage_label_names = (
'D00', 'D01', 'D10', 'D11',
'D20', 'D40', 'D43', 'D44'
)
def are_overlapping(bbox1, bbox2):
ymin1, xmin1, ymax1, xmax1 = bbox1
ymin2, xmin2, ymax2, xmax2 = bbox2
vertexes = [
(xmin1, ymin1),
(xmin1, ymax1),
(xmax1, ymin1),
(xmax1, ymax1)
]
for (x, y) in vertexes:
if xmin2 < x < xmax2 and ymin2 < y < ymax2:
return True
return False
def generate_background_bbox(image_shape, bbox_shape, existing_bboxes,
n_attempts=10):
"""
Generate a bounding box that does not overlap with any `existing_bboxes`.
The function tries generating a bounding box at most `n_attempts` times.
Raises `RuntimeError` if a bounding box that doesn't overlap with any
existing bounding boxes cannot be generated.
Args:
image_shape (tuple): The shape of the original image in the format of
(height, width). Bounding boxes are generated to fit within
`image_shape`.
bbox_shape (tuple): The shape of a bounding box to be generated.
existing_bboxes (list of tuples): Existing bounding boxes. The
generated bounding box should not overlap with any
`existing_bboxes`.
n_attempts (int): The number of attempts to generate a bounding box
"""
def generate_candidate():
xmin = randint(0, image_shape[0] - bbox_shape[0] + 1)
ymin = randint(0, image_shape[1] - bbox_shape[1] + 1)
xmax = xmin + bbox_shape[0]
ymax = ymin + bbox_shape[1]
return (ymin, xmin, ymax, xmax)
def at_least_one_overlapping(candidate, bboxes):
"""
Whether there is at least one bbox that overlaps with the candidate
"""
for bbox in bboxes:
if are_overlapping(candidate, bbox):
return True
return False
for i in range(n_attempts):
candidate = generate_candidate()
if at_least_one_overlapping(candidate, existing_bboxes):
continue
# return if there is no existing bounding box
# that overlaps with the candidate
return candidate
raise RuntimeError("Background could not be generated")