20
20
from ...simple_object_detection_label_input import SimpleObjectDetectionInput
21
21
22
22
23
- def _create_label_file (tmp_path : Path ) -> Path :
23
+ def _create_label_file (tmp_path : Path , filename : str ) -> Path :
24
24
"""Create a dummy label file in the given directory."""
25
25
annotation = json .dumps (
26
26
{
27
- "file_name" : "image.jpg" ,
27
+ "file_name" : filename ,
28
28
"predictions" : [
29
29
{
30
30
"category_id" : 1 ,
@@ -37,7 +37,7 @@ def _create_label_file(tmp_path: Path) -> Path:
37
37
],
38
38
}
39
39
)
40
- label_path = tmp_path / "labels" / "image .json"
40
+ label_path = ( tmp_path / "labels" / filename ). with_suffix ( " .json")
41
41
label_path .parent .mkdir (parents = True , exist_ok = True )
42
42
label_path .write_text (annotation )
43
43
return label_path
@@ -63,12 +63,15 @@ def _create_schema_file(tmp_path: Path) -> Path:
63
63
class TestLightlyObjectDetectionInput :
64
64
def test_get_labels (self , tmp_path : Path , mocker : MockerFixture ) -> None :
65
65
# Prepare inputs.
66
- _create_label_file (tmp_path = tmp_path )
66
+ _create_label_file (tmp_path = tmp_path , filename = "image.jpg" )
67
+ _create_label_file (tmp_path = tmp_path , filename = "subdir/image.jpg" )
67
68
_create_schema_file (tmp_path = tmp_path )
68
69
69
70
# Mock the image file.
70
71
(tmp_path / "images" ).mkdir ()
71
72
(tmp_path / "images/image.jpg" ).touch ()
73
+ (tmp_path / "images/subdir" ).mkdir (parents = True )
74
+ (tmp_path / "images/subdir/image.jpg" ).touch ()
72
75
mocker .patch ("PIL.Image.open" , autospec = True ).return_value .size = (100 , 200 )
73
76
74
77
# Convert.
@@ -100,12 +103,35 @@ def test_get_labels(self, tmp_path: Path, mocker: MockerFixture) -> None:
100
103
),
101
104
),
102
105
],
103
- )
106
+ ),
107
+ ImageObjectDetection (
108
+ image = Image (id = 1 , filename = "subdir/image.jpg" , width = 100 , height = 200 ),
109
+ objects = [
110
+ SingleObjectDetection (
111
+ category = Category (id = 1 , name = "dog" ),
112
+ box = BoundingBox (
113
+ xmin = 10.0 ,
114
+ ymin = 20.0 ,
115
+ xmax = 30.0 ,
116
+ ymax = 40.0 ,
117
+ ),
118
+ ),
119
+ SingleObjectDetection (
120
+ category = Category (id = 0 , name = "cat" ),
121
+ box = BoundingBox (
122
+ xmin = 50.0 ,
123
+ ymin = 60.0 ,
124
+ xmax = 70.0 ,
125
+ ymax = 80.0 ,
126
+ ),
127
+ ),
128
+ ],
129
+ ),
104
130
]
105
131
106
132
def test_get_labels__raises_label_without_image (self , tmp_path : Path ) -> None :
107
133
# Prepare inputs.
108
- _create_label_file (tmp_path = tmp_path )
134
+ _create_label_file (tmp_path = tmp_path , filename = "image.jpg" )
109
135
_create_schema_file (tmp_path = tmp_path )
110
136
111
137
# Try to convert.
@@ -123,7 +149,7 @@ def test_get_labels__skip_label_without_image(
123
149
self , tmp_path : Path , mocker : MockerFixture
124
150
) -> None :
125
151
# Prepare inputs.
126
- _create_label_file (tmp_path = tmp_path )
152
+ _create_label_file (tmp_path = tmp_path , filename = "image.jpg" )
127
153
_create_schema_file (tmp_path = tmp_path )
128
154
129
155
# Convert.
0 commit comments