Skip to content

Commit d40b138

Browse files
committed
- Add tests
- Fix parameter issues in `add_multiple_T_labels` and `draw_multiple_flags_with_labels`
1 parent f6e9f4c commit d40b138

File tree

4 files changed

+158
-7
lines changed

4 files changed

+158
-7
lines changed

bbox_visualizer/bbox_visualizer.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,9 @@ def add_multiple_T_labels(img,
329329
"""
330330

331331
for label, bbox in zip(labels, bboxes):
332-
add_T_label(img, label, bbox, draw_bg, text_bg_color, text_color)
332+
img = add_T_label(img, label, bbox, size=1, thickness=2,
333+
draw_bg=draw_bg, text_bg_color=text_bg_color,
334+
text_color=text_color)
333335

334336
return img
335337

@@ -349,12 +351,12 @@ def draw_multiple_flags_with_labels(img,
349351
the image on which the flags are to be drawn
350352
labels : list
351353
labels that are written inside the flags
352-
bbox : list
354+
bboxes : list
353355
a list of lists, each inner list containing x_min, y_min, x_max and y_max of the rectangle positions
354356
write_label : bool, optional
355-
if True, writes the labels, otherwise, it's just a vertical line for each object, by default True
357+
if True, writes the labels, otherwise, they're just vertical lines, by default True
356358
line_color : tuple, optional
357-
the color of the pole of the flags, by default (255, 255, 255)
359+
the color of the poles of the flags, by default (255, 255, 255)
358360
text_bg_color : tuple, optional
359361
the background color of the labels that are filled, by default (255, 255, 255)
360362
text_color : tuple, optional
@@ -367,6 +369,9 @@ def draw_multiple_flags_with_labels(img,
367369
"""
368370

369371
for label, bbox in zip(labels, bboxes):
370-
img = draw_flag_with_label(img, label, bbox, write_label, line_color,
371-
text_bg_color, text_color)
372+
img = draw_flag_with_label(img, label, bbox, size=1, thickness=2,
373+
write_label=write_label, line_color=line_color,
374+
text_bg_color=text_bg_color,
375+
text_color=text_color)
376+
372377
return img

requirements_dev.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ bump2version==0.5.11
33
wheel==0.38.1
44
flake8==3.7.8
55
Sphinx==1.8.5
6-
twine==1.14.0
6+
twine==1.14.0
7+
pytest==7.4.0
8+
numpy>=1.19.0
9+
opencv-python>=4.1.0.25

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Tests for bbox-visualizer package."""

tests/test_bbox_visualizer.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import pytest
2+
import numpy as np
3+
import cv2
4+
from bbox_visualizer import bbox_visualizer
5+
6+
@pytest.fixture
7+
def sample_image():
8+
"""Create a sample image for testing."""
9+
return np.zeros((100, 100, 3), dtype=np.uint8)
10+
11+
@pytest.fixture
12+
def sample_bbox():
13+
"""Create a sample bounding box for testing."""
14+
return [10, 10, 50, 50] # x_min, y_min, x_max, y_max
15+
16+
@pytest.fixture
17+
def sample_label():
18+
"""Create a sample label for testing."""
19+
return "test"
20+
21+
def test_draw_rectangle_basic(sample_image, sample_bbox):
22+
"""Test basic rectangle drawing functionality."""
23+
result = bbox_visualizer.draw_rectangle(sample_image, sample_bbox)
24+
assert isinstance(result, np.ndarray)
25+
assert result.shape == sample_image.shape
26+
# Check if rectangle was drawn (should have some non-zero pixels)
27+
assert np.sum(result) > 0
28+
29+
def test_draw_rectangle_opaque(sample_image, sample_bbox):
30+
"""Test opaque rectangle drawing."""
31+
result = bbox_visualizer.draw_rectangle(
32+
sample_image, sample_bbox, is_opaque=True, alpha=0.5
33+
)
34+
assert isinstance(result, np.ndarray)
35+
assert result.shape == sample_image.shape
36+
assert np.sum(result) > 0
37+
38+
def test_add_label(sample_image, sample_bbox, sample_label):
39+
"""Test adding label to bounding box."""
40+
result = bbox_visualizer.add_label(
41+
sample_image, sample_label, sample_bbox
42+
)
43+
assert isinstance(result, np.ndarray)
44+
assert result.shape == sample_image.shape
45+
assert np.sum(result) > 0
46+
47+
def test_add_T_label(sample_image, sample_bbox, sample_label):
48+
"""Test adding T-label to bounding box."""
49+
result = bbox_visualizer.add_T_label(
50+
sample_image, sample_label, sample_bbox
51+
)
52+
assert isinstance(result, np.ndarray)
53+
assert result.shape == sample_image.shape
54+
assert np.sum(result) > 0
55+
56+
def test_draw_flag_with_label(sample_image, sample_bbox, sample_label):
57+
"""Test drawing flag with label."""
58+
result = bbox_visualizer.draw_flag_with_label(
59+
sample_image, sample_label, sample_bbox
60+
)
61+
assert isinstance(result, np.ndarray)
62+
assert result.shape == sample_image.shape
63+
assert np.sum(result) > 0
64+
65+
def test_draw_multiple_rectangles(sample_image):
66+
"""Test drawing multiple rectangles."""
67+
bboxes = [[10, 10, 30, 30], [40, 40, 60, 60]]
68+
result = bbox_visualizer.draw_multiple_rectangles(
69+
sample_image, bboxes
70+
)
71+
assert isinstance(result, np.ndarray)
72+
assert result.shape == sample_image.shape
73+
assert np.sum(result) > 0
74+
75+
def test_add_multiple_labels(sample_image):
76+
"""Test adding multiple labels."""
77+
bboxes = [[10, 10, 30, 30], [40, 40, 60, 60]]
78+
labels = ["obj1", "obj2"]
79+
result = bbox_visualizer.add_multiple_labels(
80+
sample_image, labels, bboxes
81+
)
82+
assert isinstance(result, np.ndarray)
83+
assert result.shape == sample_image.shape
84+
assert np.sum(result) > 0
85+
86+
def test_add_multiple_T_labels(sample_image):
87+
"""Test adding multiple T-labels."""
88+
bboxes = [[10, 10, 30, 30], [40, 40, 60, 60]]
89+
labels = ["obj1", "obj2"]
90+
# Note: The function internally sets size=1 and thickness=2
91+
result = bbox_visualizer.add_multiple_T_labels(
92+
sample_image, labels, bboxes
93+
)
94+
assert isinstance(result, np.ndarray)
95+
assert result.shape == sample_image.shape
96+
assert np.sum(result) > 0
97+
98+
def test_draw_multiple_flags_with_labels(sample_image):
99+
"""Test drawing multiple flags with labels."""
100+
bboxes = [[10, 10, 30, 30], [40, 40, 60, 60]]
101+
labels = ["obj1", "obj2"]
102+
# Note: The function internally sets size=1 and thickness=2
103+
result = bbox_visualizer.draw_multiple_flags_with_labels(
104+
sample_image, labels, bboxes
105+
)
106+
assert isinstance(result, np.ndarray)
107+
assert result.shape == sample_image.shape
108+
assert np.sum(result) > 0
109+
110+
def test_invalid_bbox_values(sample_image):
111+
"""Test handling of invalid bbox values."""
112+
# Testing with invalid bbox coordinates (negative values)
113+
invalid_bbox = [-10, -10, 20, 20]
114+
result = bbox_visualizer.draw_rectangle(sample_image, invalid_bbox)
115+
# The function should handle invalid coordinates by clipping them
116+
assert isinstance(result, np.ndarray)
117+
assert result.shape == sample_image.shape
118+
119+
def test_empty_inputs():
120+
"""Test handling of empty inputs for multiple object functions."""
121+
img = np.zeros((100, 100, 3), dtype=np.uint8)
122+
empty_bboxes = []
123+
empty_labels = []
124+
125+
# These should handle empty inputs gracefully
126+
result = bbox_visualizer.draw_multiple_rectangles(img, empty_bboxes)
127+
assert np.array_equal(result, img)
128+
129+
result = bbox_visualizer.add_multiple_labels(img, empty_labels, empty_bboxes)
130+
assert np.array_equal(result, img)
131+
132+
def test_color_parameters(sample_image, sample_bbox):
133+
"""Test different color parameters."""
134+
# Test with different colors
135+
colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)]
136+
for color in colors:
137+
result = bbox_visualizer.draw_rectangle(
138+
sample_image, sample_bbox, bbox_color=color
139+
)
140+
assert isinstance(result, np.ndarray)
141+
assert result.shape == sample_image.shape
142+
assert np.sum(result) > 0

0 commit comments

Comments
 (0)