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