Skip to content

Commit 2367681

Browse files
authored
Add basic bbox helper methods (#3)
1 parent d7469e9 commit 2367681

2 files changed

Lines changed: 99 additions & 1 deletion

File tree

image_dataset_viz/__init__.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,63 @@
11

2-
__version__ = '0.1'
2+
__version__ = '0.2'
3+
4+
import numpy as np
35

46
from image_dataset_viz.dataset_exporter import DatasetExporter, render_datapoint
7+
8+
9+
def bbox_to_points(bbox_xyxy):
10+
"""Helper method to convert bounding box as list/tuple of [x1, y1, x2, y2] into points `ndarray` of shape (N, 2)
11+
12+
Args:
13+
bbox_xyxy (list or tuple): bounding box as list/tuple of [x1, y1, x2, y2]
14+
15+
Returns:
16+
ndarray of points
17+
18+
"""
19+
assert isinstance(bbox_xyxy, (list, tuple)) and len(bbox_xyxy) == 4, \
20+
"Argument bbox_xyxy should be a list/tuple of [x1, y1, x2, y2]"
21+
22+
return np.array([
23+
[bbox_xyxy[0], bbox_xyxy[1]],
24+
[bbox_xyxy[2], bbox_xyxy[1]],
25+
[bbox_xyxy[2], bbox_xyxy[3]],
26+
[bbox_xyxy[0], bbox_xyxy[3]],
27+
])
28+
29+
30+
def xywh_to_xyxy(xywh):
31+
"""Helper method to transform bounding box of type [x1, y1, width, height] into [x1, y1, x2, y2]
32+
33+
Args:
34+
xywh (list or tuple): bounding box of type [x1, y1, width, height]
35+
36+
Returns:
37+
list [x1, y1, x2, y2]
38+
39+
"""
40+
assert isinstance(xywh, (list, tuple)) and len(xywh) == 4, \
41+
"Argument xywh should be a list/tuple of [x1, y1, width, height]"
42+
x1, y1 = xywh[0], xywh[1]
43+
x2 = x1 + max(0, xywh[2] - 1)
44+
y2 = y1 + max(0, xywh[3] - 1)
45+
return [x1, y1, x2, y2]
46+
47+
48+
def xyxy_to_xywh(xyxy):
49+
"""Helper method to transform bounding box of type [x1, y1, x2, y2] into [x1, y1, width, height]
50+
51+
Args:
52+
xyxy (list or tuple): bounding box of type [x1, y1, x2, y2]
53+
54+
Returns:
55+
list [x1, y1, width, height]
56+
57+
"""
58+
assert isinstance(xyxy, (list, tuple)) and len(xyxy) == 4, \
59+
"Argument xyxy should be a list/tuple of [x1, y1, x2, y2]"
60+
x1, y1 = xyxy[0], xyxy[1]
61+
w = xyxy[2] - x1 + 1
62+
h = xyxy[3] - y1 + 1
63+
return [x1, y1, w, h]

tests/test_image_dataset_viz.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from unittest import TestCase, main
2+
3+
import numpy as np
4+
5+
from image_dataset_viz import bbox_to_points, xywh_to_xyxy, xyxy_to_xywh
6+
7+
8+
class TestHelperMethods(TestCase):
9+
10+
def test_bbox_to_points(self):
11+
12+
bbox = (10, 12, 34, 45)
13+
true_points = np.array([
14+
[10, 12],
15+
[34, 12],
16+
[34, 45],
17+
[10, 45]
18+
])
19+
points = bbox_to_points(bbox)
20+
21+
self.assertTrue((true_points == points).all())
22+
23+
def test_xywh_to_xyxy(self):
24+
25+
xywh = (10, 12, 34, 45)
26+
true_xyxy = [10, 12, 43, 56]
27+
xyxy = xywh_to_xyxy(xywh)
28+
self.assertEqual(true_xyxy, xyxy)
29+
30+
def test_xyxy_to_xywh(self):
31+
32+
xyxy = [10, 12, 43, 56]
33+
true_xywh = [10, 12, 34, 45]
34+
xywh = xyxy_to_xywh(xyxy)
35+
self.assertEqual(true_xywh, xywh)
36+
37+
38+
if __name__ == "__main__":
39+
main()

0 commit comments

Comments
 (0)