Skip to content

Commit e289be2

Browse files
authored
Merge pull request #9 from DagsHub/bug/context-path-image
Bug: Handle YOLO annotation setups where "image" is part of the main path and not the annotations
2 parents b533374 + 51e53db commit e289be2

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

dagshub_annotation_converter/converters/yolo.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,17 @@ def export_to_fs(
180180
export_path = Path(export_dir)
181181

182182
for filename, anns in grouped_annotations.items():
183-
annotation_filepath = replace_folder(
184-
Path(filename), context.image_dir_name, context.label_dir_name, context.label_extension
183+
annotation_filepath = export_path / context.path / filename
184+
out_path = replace_folder(
185+
annotation_filepath, context.image_dir_name, context.label_dir_name, context.label_extension
185186
)
186-
if annotation_filepath is None:
187+
if out_path is None:
187188
logger.warning(f"Couldn't generate annotation file path for image file [{filename}]")
188189
continue
189-
annotation_filename = export_path / context.path / annotation_filepath
190-
annotation_filename.parent.mkdir(parents=True, exist_ok=True)
190+
out_path.parent.mkdir(parents=True, exist_ok=True)
191191
annotation_content = annotations_to_string(anns, context)
192192
if annotation_content is not None:
193-
with open(annotation_filename, "w") as f:
193+
with open(out_path, "w") as f:
194194
f.write(annotation_content)
195195

196196
guessed_train_path, guessed_val_path, guessed_test_path = _guess_train_val_test_split(

tests/fs_export/yolo/test_fs_export.py

+42-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
def test_bbox_export(tmp_path):
18-
ctx = YoloContext(annotation_type="bbox", path="data")
18+
ctx = YoloContext(annotation_type="bbox", path=Path("data"))
1919
ctx.categories.add(name="cat")
2020
ctx.categories.add(name="dog")
2121
annotations = [
@@ -53,7 +53,7 @@ def test_bbox_export(tmp_path):
5353

5454

5555
def test_segmentation_export(tmp_path):
56-
ctx = YoloContext(annotation_type="segmentation", path="data")
56+
ctx = YoloContext(annotation_type="segmentation", path=Path("data"))
5757
ctx.categories.add(name="cat")
5858
ctx.categories.add(name="dog")
5959
annotations = [
@@ -85,7 +85,7 @@ def test_segmentation_export(tmp_path):
8585

8686

8787
def test_pose_export(tmp_path):
88-
ctx = YoloContext(annotation_type="pose", path="data")
88+
ctx = YoloContext(annotation_type="pose", path=Path("data"))
8989
ctx.categories.add(name="cat")
9090
ctx.categories.add(name="dog")
9191
ctx.keypoints_in_annotation = 2
@@ -118,7 +118,7 @@ def test_pose_export(tmp_path):
118118

119119

120120
def test_not_exporting_wrong_annotations(tmp_path):
121-
ctx = YoloContext(annotation_type="bbox", path="data")
121+
ctx = YoloContext(annotation_type="bbox", path=Path("data"))
122122
ctx.categories.add(name="cat")
123123
ctx.categories.add(name="dog")
124124
annotations = [
@@ -173,3 +173,41 @@ def test__get_common_folder_with_part(paths, prefix, expected):
173173
expected = Path(expected)
174174

175175
assert actual == expected
176+
177+
178+
def test_export_with_image_in_path(tmp_path):
179+
ctx = YoloContext(annotation_type="bbox", path=Path("data/images"))
180+
ctx.categories.add(name="cat")
181+
ctx.categories.add(name="dog")
182+
annotations = [
183+
IRBBoxImageAnnotation(
184+
filename="cats/1.jpg",
185+
categories={"cat": 1.0},
186+
top=0.0,
187+
left=0.0,
188+
width=0.5,
189+
height=0.5,
190+
image_width=100,
191+
image_height=200,
192+
coordinate_style=CoordinateStyle.NORMALIZED,
193+
),
194+
IRBBoxImageAnnotation(
195+
filename="dogs/2.jpg",
196+
categories={"dog": 1.0},
197+
top=0.5,
198+
left=0.5,
199+
width=0.5,
200+
height=0.5,
201+
image_width=100,
202+
image_height=200,
203+
coordinate_style=CoordinateStyle.NORMALIZED,
204+
),
205+
]
206+
207+
p = export_to_fs(ctx, annotations, export_dir=tmp_path)
208+
209+
assert p == tmp_path / "yolo_dagshub.yaml"
210+
211+
assert (tmp_path / "yolo_dagshub.yaml").exists()
212+
assert (tmp_path / "data" / "labels" / "cats" / "1.txt").exists()
213+
assert (tmp_path / "data" / "labels" / "dogs" / "2.txt").exists()

0 commit comments

Comments
 (0)