Skip to content

Commit cc2e85d

Browse files
authored
Fix lightly prediction subdirs (#13)
* Add support to list predictions from subdirectories * Update unit test
1 parent 0641d09 commit cc2e85d

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,5 @@ Temporary Items
199199
# End of https://www.toptal.com/developers/gitignore/api/macos
200200

201201
# VScode
202-
.vscode
202+
.vscode
203+
*.code-workspace

src/labelformat/formats/lightly.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def get_labels(self) -> Iterable[ImageObjectDetection]:
6565
}
6666
filename_to_image = {image.filename: image for image in self.get_images()}
6767

68-
for json_path in self._input_folder.glob("*.json"):
68+
for json_path in self._input_folder.rglob("*.json"):
6969
if json_path.name == "schema.json":
7070
continue
7171
data = json.loads(json_path.read_text())

tests/unit/formats/test_lightly.py

+33-7
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
from ...simple_object_detection_label_input import SimpleObjectDetectionInput
2121

2222

23-
def _create_label_file(tmp_path: Path) -> Path:
23+
def _create_label_file(tmp_path: Path, filename: str) -> Path:
2424
"""Create a dummy label file in the given directory."""
2525
annotation = json.dumps(
2626
{
27-
"file_name": "image.jpg",
27+
"file_name": filename,
2828
"predictions": [
2929
{
3030
"category_id": 1,
@@ -37,7 +37,7 @@ def _create_label_file(tmp_path: Path) -> Path:
3737
],
3838
}
3939
)
40-
label_path = tmp_path / "labels" / "image.json"
40+
label_path = (tmp_path / "labels" / filename).with_suffix(".json")
4141
label_path.parent.mkdir(parents=True, exist_ok=True)
4242
label_path.write_text(annotation)
4343
return label_path
@@ -63,12 +63,15 @@ def _create_schema_file(tmp_path: Path) -> Path:
6363
class TestLightlyObjectDetectionInput:
6464
def test_get_labels(self, tmp_path: Path, mocker: MockerFixture) -> None:
6565
# 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")
6768
_create_schema_file(tmp_path=tmp_path)
6869

6970
# Mock the image file.
7071
(tmp_path / "images").mkdir()
7172
(tmp_path / "images/image.jpg").touch()
73+
(tmp_path / "images/subdir").mkdir(parents=True)
74+
(tmp_path / "images/subdir/image.jpg").touch()
7275
mocker.patch("PIL.Image.open", autospec=True).return_value.size = (100, 200)
7376

7477
# Convert.
@@ -100,12 +103,35 @@ def test_get_labels(self, tmp_path: Path, mocker: MockerFixture) -> None:
100103
),
101104
),
102105
],
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+
),
104130
]
105131

106132
def test_get_labels__raises_label_without_image(self, tmp_path: Path) -> None:
107133
# Prepare inputs.
108-
_create_label_file(tmp_path=tmp_path)
134+
_create_label_file(tmp_path=tmp_path, filename="image.jpg")
109135
_create_schema_file(tmp_path=tmp_path)
110136

111137
# Try to convert.
@@ -123,7 +149,7 @@ def test_get_labels__skip_label_without_image(
123149
self, tmp_path: Path, mocker: MockerFixture
124150
) -> None:
125151
# Prepare inputs.
126-
_create_label_file(tmp_path=tmp_path)
152+
_create_label_file(tmp_path=tmp_path, filename="image.jpg")
127153
_create_schema_file(tmp_path=tmp_path)
128154

129155
# Convert.

0 commit comments

Comments
 (0)