5
5
6
6
from labelbox .data .annotation_types import Point , Rectangle , Mask , MaskData
7
7
from pydantic import ValidationError
8
+ from shapely .geometry import MultiPolygon , Polygon
8
9
9
10
10
11
def test_mask ():
@@ -18,112 +19,53 @@ def test_mask():
18
19
19
20
mask1 = Mask (mask = mask_data , color = (255 , 255 , 255 ))
20
21
21
- expected1 = {
22
- "type" : "MultiPolygon" ,
23
- "coordinates" : [
24
- (
25
- (
26
- (0.0 , 0.0 ),
27
- (0.0 , 1.0 ),
28
- (0.0 , 2.0 ),
29
- (0.0 , 3.0 ),
30
- (0.0 , 4.0 ),
31
- (0.0 , 5.0 ),
32
- (0.0 , 6.0 ),
33
- (0.0 , 7.0 ),
34
- (0.0 , 8.0 ),
35
- (0.0 , 9.0 ),
36
- (0.0 , 10.0 ),
37
- (1.0 , 10.0 ),
38
- (2.0 , 10.0 ),
39
- (3.0 , 10.0 ),
40
- (4.0 , 10.0 ),
41
- (5.0 , 10.0 ),
42
- (6.0 , 10.0 ),
43
- (7.0 , 10.0 ),
44
- (8.0 , 10.0 ),
45
- (9.0 , 10.0 ),
46
- (10.0 , 10.0 ),
47
- (10.0 , 9.0 ),
48
- (10.0 , 8.0 ),
49
- (10.0 , 7.0 ),
50
- (10.0 , 6.0 ),
51
- (10.0 , 5.0 ),
52
- (10.0 , 4.0 ),
53
- (10.0 , 3.0 ),
54
- (10.0 , 2.0 ),
55
- (10.0 , 1.0 ),
56
- (10.0 , 0.0 ),
57
- (9.0 , 0.0 ),
58
- (8.0 , 0.0 ),
59
- (7.0 , 0.0 ),
60
- (6.0 , 0.0 ),
61
- (5.0 , 0.0 ),
62
- (4.0 , 0.0 ),
63
- (3.0 , 0.0 ),
64
- (2.0 , 0.0 ),
65
- (1.0 , 0.0 ),
66
- (0.0 , 0.0 ),
67
- ),
68
- )
69
- ],
70
- }
71
- assert mask1 .geometry == expected1
72
- assert mask1 .shapely .__geo_interface__ == expected1
22
+ # Create expected geometry - a simple rectangle from (0,0) to (10,10)
23
+ # Using geometric equality instead of exact coordinate comparison
24
+ # to handle different coordinate ordering between OpenCV versions
25
+ expected_polygon1 = Polygon (
26
+ [(0.0 , 0.0 ), (0.0 , 10.0 ), (10.0 , 10.0 ), (10.0 , 0.0 ), (0.0 , 0.0 )]
27
+ )
28
+ expected_multipolygon1 = MultiPolygon ([expected_polygon1 ])
29
+
30
+ # Use geometric equality - both polygons represent the same shape
31
+ assert mask1 .shapely .equals (
32
+ expected_multipolygon1
33
+ ), f"Geometry mismatch: expected area { expected_multipolygon1 .area } , got area { mask1 .shapely .area } "
34
+
35
+ # Verify that the geometry has correct area and bounds
36
+ assert (
37
+ abs (mask1 .shapely .area - 100.0 ) < 1e-6
38
+ ), f"Expected area 100, got { mask1 .shapely .area } "
39
+ assert mask1 .shapely .bounds == (
40
+ 0.0 ,
41
+ 0.0 ,
42
+ 10.0 ,
43
+ 10.0 ,
44
+ ), f"Expected bounds (0,0,10,10), got { mask1 .shapely .bounds } "
73
45
74
46
mask2 = Mask (mask = mask_data , color = (0 , 255 , 255 ))
75
- expected2 = {
76
- "type" : "MultiPolygon" ,
77
- "coordinates" : [
78
- (
79
- (
80
- (20.0 , 20.0 ),
81
- (20.0 , 21.0 ),
82
- (20.0 , 22.0 ),
83
- (20.0 , 23.0 ),
84
- (20.0 , 24.0 ),
85
- (20.0 , 25.0 ),
86
- (20.0 , 26.0 ),
87
- (20.0 , 27.0 ),
88
- (20.0 , 28.0 ),
89
- (20.0 , 29.0 ),
90
- (20.0 , 30.0 ),
91
- (21.0 , 30.0 ),
92
- (22.0 , 30.0 ),
93
- (23.0 , 30.0 ),
94
- (24.0 , 30.0 ),
95
- (25.0 , 30.0 ),
96
- (26.0 , 30.0 ),
97
- (27.0 , 30.0 ),
98
- (28.0 , 30.0 ),
99
- (29.0 , 30.0 ),
100
- (30.0 , 30.0 ),
101
- (30.0 , 29.0 ),
102
- (30.0 , 28.0 ),
103
- (30.0 , 27.0 ),
104
- (30.0 , 26.0 ),
105
- (30.0 , 25.0 ),
106
- (30.0 , 24.0 ),
107
- (30.0 , 23.0 ),
108
- (30.0 , 22.0 ),
109
- (30.0 , 21.0 ),
110
- (30.0 , 20.0 ),
111
- (29.0 , 20.0 ),
112
- (28.0 , 20.0 ),
113
- (27.0 , 20.0 ),
114
- (26.0 , 20.0 ),
115
- (25.0 , 20.0 ),
116
- (24.0 , 20.0 ),
117
- (23.0 , 20.0 ),
118
- (22.0 , 20.0 ),
119
- (21.0 , 20.0 ),
120
- (20.0 , 20.0 ),
121
- ),
122
- )
123
- ],
124
- }
125
- assert mask2 .geometry == expected2
126
- assert mask2 .shapely .__geo_interface__ == expected2
47
+
48
+ # Create expected geometry for the second rectangle from (20,20) to (30,30)
49
+ expected_polygon2 = Polygon (
50
+ [(20.0 , 20.0 ), (20.0 , 30.0 ), (30.0 , 30.0 ), (30.0 , 20.0 ), (20.0 , 20.0 )]
51
+ )
52
+ expected_multipolygon2 = MultiPolygon ([expected_polygon2 ])
53
+
54
+ assert mask2 .shapely .equals (
55
+ expected_multipolygon2
56
+ ), f"Geometry mismatch: expected area { expected_multipolygon2 .area } , got area { mask2 .shapely .area } "
57
+
58
+ # Verify that the geometry has correct area and bounds
59
+ assert (
60
+ abs (mask2 .shapely .area - 100.0 ) < 1e-6
61
+ ), f"Expected area 100, got { mask2 .shapely .area } "
62
+ assert mask2 .shapely .bounds == (
63
+ 20.0 ,
64
+ 20.0 ,
65
+ 30.0 ,
66
+ 30.0 ,
67
+ ), f"Expected bounds (20,20,30,30), got { mask2 .shapely .bounds } "
68
+
127
69
gt_mask = cv2 .cvtColor (
128
70
cv2 .imread ("tests/data/assets/mask.png" ), cv2 .COLOR_BGR2RGB
129
71
)
0 commit comments