Skip to content

Commit 5ae4986

Browse files
authored
Merge pull request #257 from fastlabel/feature/allow-zero-dimensions-in-converters
feat: Allow tasks with zero height/width in COCO and Pascal VOC converters
2 parents e18f3b7 + 2e3306e commit 5ae4986

2 files changed

Lines changed: 201 additions & 6 deletions

File tree

fastlabel/converters.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ def to_coco(
3434
annotation_id = 0
3535
image_index = 0
3636
for task in tasks:
37-
if task["height"] == 0 or task["width"] == 0:
38-
continue
39-
4037
if is_video_project_type(project_type):
4138
image_file_names = _export_image_files_for_video_task(
4239
task, str((Path(output_dir) / "images").resolve())
@@ -726,9 +723,6 @@ def _truncate(n, decimals=0) -> float:
726723
def to_pascalvoc(project_type: str, tasks: list, output_dir: str) -> list:
727724
pascalvoc = []
728725
for task in tasks:
729-
if task["height"] == 0 or task["width"] == 0:
730-
continue
731-
732726
if is_video_project_type(project_type):
733727
image_file_names = _export_image_files_for_video_task(
734728
task, str((Path(output_dir) / "images").resolve())

tests/test_converters.py

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
from fastlabel import converters
2+
3+
4+
class TestToCoco:
5+
"""Tests for to_coco converter function."""
6+
7+
def test_to_coco_with_zero_height_task(self, tmp_path):
8+
"""Test that tasks with height=0 are included in output."""
9+
tasks = [
10+
{
11+
"name": "image1.jpg",
12+
"height": 0,
13+
"width": 100,
14+
"annotations": [
15+
{
16+
"type": "bbox",
17+
"value": "cat",
18+
"points": [10, 10, 50, 50],
19+
"color": "#FF0000",
20+
}
21+
],
22+
},
23+
{
24+
"name": "image2.jpg",
25+
"height": 100,
26+
"width": 100,
27+
"annotations": [
28+
{
29+
"type": "bbox",
30+
"value": "cat",
31+
"points": [10, 10, 50, 50],
32+
"color": "#FF0000",
33+
}
34+
],
35+
},
36+
]
37+
38+
result = converters.to_coco(
39+
project_type="image_bbox",
40+
tasks=tasks,
41+
output_dir=str(tmp_path),
42+
)
43+
44+
assert len(result["images"]) == 2
45+
assert result["images"][0]["height"] == 0
46+
assert result["images"][1]["height"] == 100
47+
48+
def test_to_coco_with_zero_width_task(self, tmp_path):
49+
"""Test that tasks with width=0 are included in output."""
50+
tasks = [
51+
{
52+
"name": "image1.jpg",
53+
"height": 100,
54+
"width": 0,
55+
"annotations": [
56+
{
57+
"type": "bbox",
58+
"value": "dog",
59+
"points": [10, 10, 50, 50],
60+
"color": "#00FF00",
61+
}
62+
],
63+
},
64+
]
65+
66+
result = converters.to_coco(
67+
project_type="image_bbox",
68+
tasks=tasks,
69+
output_dir=str(tmp_path),
70+
)
71+
72+
assert len(result["images"]) == 1
73+
assert result["images"][0]["width"] == 0
74+
75+
def test_to_coco_with_zero_dimensions_task(self, tmp_path):
76+
"""Test that tasks with both height=0 and width=0 are included in output."""
77+
tasks = [
78+
{
79+
"name": "image1.jpg",
80+
"height": 0,
81+
"width": 0,
82+
"annotations": [
83+
{
84+
"type": "bbox",
85+
"value": "bird",
86+
"points": [10, 10, 50, 50],
87+
"color": "#0000FF",
88+
}
89+
],
90+
},
91+
]
92+
93+
result = converters.to_coco(
94+
project_type="image_bbox",
95+
tasks=tasks,
96+
output_dir=str(tmp_path),
97+
)
98+
99+
assert len(result["images"]) == 1
100+
assert result["images"][0]["height"] == 0
101+
assert result["images"][0]["width"] == 0
102+
103+
104+
class TestToPascalVoc:
105+
"""Tests for to_pascalvoc converter function."""
106+
107+
def test_to_pascalvoc_with_zero_height_task(self, tmp_path):
108+
"""Test that tasks with height=0 are included in output."""
109+
tasks = [
110+
{
111+
"name": "image1.jpg",
112+
"height": 0,
113+
"width": 100,
114+
"annotations": [
115+
{
116+
"type": "bbox",
117+
"value": "cat",
118+
"points": [10, 10, 50, 50],
119+
"attributes": [],
120+
}
121+
],
122+
},
123+
{
124+
"name": "image2.jpg",
125+
"height": 100,
126+
"width": 100,
127+
"annotations": [
128+
{
129+
"type": "bbox",
130+
"value": "cat",
131+
"points": [10, 10, 50, 50],
132+
"attributes": [],
133+
}
134+
],
135+
},
136+
]
137+
138+
result = converters.to_pascalvoc(
139+
project_type="image_bbox",
140+
tasks=tasks,
141+
output_dir=str(tmp_path),
142+
)
143+
144+
assert len(result) == 2
145+
assert result[0]["annotation"]["size"]["height"] == 0
146+
assert result[1]["annotation"]["size"]["height"] == 100
147+
148+
def test_to_pascalvoc_with_zero_width_task(self, tmp_path):
149+
"""Test that tasks with width=0 are included in output."""
150+
tasks = [
151+
{
152+
"name": "image1.jpg",
153+
"height": 100,
154+
"width": 0,
155+
"annotations": [
156+
{
157+
"type": "bbox",
158+
"value": "dog",
159+
"points": [10, 10, 50, 50],
160+
"attributes": [],
161+
}
162+
],
163+
},
164+
]
165+
166+
result = converters.to_pascalvoc(
167+
project_type="image_bbox",
168+
tasks=tasks,
169+
output_dir=str(tmp_path),
170+
)
171+
172+
assert len(result) == 1
173+
assert result[0]["annotation"]["size"]["width"] == 0
174+
175+
def test_to_pascalvoc_with_zero_dimensions_task(self, tmp_path):
176+
"""Test that tasks with both height=0 and width=0 are included in output."""
177+
tasks = [
178+
{
179+
"name": "image1.jpg",
180+
"height": 0,
181+
"width": 0,
182+
"annotations": [
183+
{
184+
"type": "bbox",
185+
"value": "bird",
186+
"points": [10, 10, 50, 50],
187+
"attributes": [],
188+
}
189+
],
190+
},
191+
]
192+
193+
result = converters.to_pascalvoc(
194+
project_type="image_bbox",
195+
tasks=tasks,
196+
output_dir=str(tmp_path),
197+
)
198+
199+
assert len(result) == 1
200+
assert result[0]["annotation"]["size"]["height"] == 0
201+
assert result[0]["annotation"]["size"]["width"] == 0

0 commit comments

Comments
 (0)