Skip to content

Commit 61c2091

Browse files
authored
added docs to darwin/exporter/formats/dataloop.py (#356)
1 parent 66f73c0 commit 61c2091

File tree

1 file changed

+93
-5
lines changed

1 file changed

+93
-5
lines changed

darwin/exporter/formats/dataloop.py

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,67 @@
11
import json
22
from pathlib import Path
3-
from typing import Any, Dict, Iterable, Iterator
3+
from typing import Any, Dict, Iterable
4+
5+
import deprecation
46

57
import darwin.datatypes as dt
68
from darwin.exporter.formats.numpy_encoder import NumpyEncoder
9+
from darwin.version import __version__
10+
11+
DEPRECATION_MESSAGE = """This function is going to be turned into private. This means that breaking
12+
changes in its interface and implementation are to be expected. We encourage using ``export``
13+
instead of calling this low-level function directly."""
714

815

9-
def export(annotation_files: Iterator[dt.AnnotationFile], output_dir: Path) -> None:
16+
def export(annotation_files: Iterable[dt.AnnotationFile], output_dir: Path) -> None:
17+
"""
18+
Exports the given ``AnnotationFile``s into the dataloop format inside of the given ``output_dir``.
19+
20+
Parameters
21+
----------
22+
annotation_files : Iterable[dt.AnnotationFile]
23+
The ``AnnotationFile``s to be exported.
24+
output_dir : Path
25+
The folder where the new coco file will be.
26+
"""
1027
for id, annotation_file in enumerate(annotation_files):
11-
export_file(annotation_file, id, output_dir)
28+
_export_file(annotation_file, id, output_dir)
1229

1330

31+
@deprecation.deprecated(
32+
deprecated_in="0.7.8",
33+
removed_in="0.8.0",
34+
current_version=__version__,
35+
details=DEPRECATION_MESSAGE,
36+
)
1437
def export_file(annotation_file: dt.AnnotationFile, id: int, output_dir: Path) -> None:
15-
output: Dict[str, Any] = build_json(annotation_file, id)
38+
output: Dict[str, Any] = _build_json(annotation_file, id)
1639
output_file_path: Path = (output_dir / annotation_file.filename).with_suffix(".json")
1740
with open(output_file_path, "w") as f:
1841
json.dump(output, f, cls=NumpyEncoder, indent=1)
1942

2043

44+
@deprecation.deprecated(
45+
deprecated_in="0.7.8",
46+
removed_in="0.8.0",
47+
current_version=__version__,
48+
details=DEPRECATION_MESSAGE,
49+
)
2150
def build_json(annotation_file: dt.AnnotationFile, id: int) -> Dict[str, Any]:
2251
return {
2352
"_id": id,
2453
"filename": annotation_file.filename,
2554
"itemMetadata": [],
26-
"annotations": build_annotations(annotation_file, id),
55+
"annotations": _build_annotations(annotation_file, id),
2756
}
2857

2958

59+
@deprecation.deprecated(
60+
deprecated_in="0.7.8",
61+
removed_in="0.8.0",
62+
current_version=__version__,
63+
details=DEPRECATION_MESSAGE,
64+
)
3065
def build_annotations(annotation_file: dt.AnnotationFile, id: int) -> Iterable[Dict[str, Any]]:
3166
output = []
3267
for annotation_id, annotation in enumerate(annotation_file.annotations):
@@ -62,3 +97,56 @@ def build_annotations(annotation_file: dt.AnnotationFile, id: int) -> Iterable[D
6297
output.append(entry)
6398

6499
return output
100+
101+
102+
def _export_file(annotation_file: dt.AnnotationFile, id: int, output_dir: Path) -> None:
103+
output: Dict[str, Any] = _build_json(annotation_file, id)
104+
output_file_path: Path = (output_dir / annotation_file.filename).with_suffix(".json")
105+
with open(output_file_path, "w") as f:
106+
json.dump(output, f, cls=NumpyEncoder, indent=1)
107+
108+
109+
def _build_annotations(annotation_file: dt.AnnotationFile, id: int) -> Iterable[Dict[str, Any]]:
110+
output = []
111+
for annotation_id, annotation in enumerate(annotation_file.annotations):
112+
print(annotation)
113+
if annotation.annotation_class.annotation_type == "bounding_box":
114+
entry = {
115+
"id": annotation_id,
116+
"datasetId": "darwin",
117+
"type": "box",
118+
"label": annotation.annotation_class.name,
119+
"attributes": [],
120+
"coordinates": [
121+
{"x": annotation.data["x"], "y": annotation.data["y"], "z": 0},
122+
{
123+
"x": annotation.data["x"] + annotation.data["w"],
124+
"y": annotation.data["y"] + annotation.data["h"],
125+
"z": 0,
126+
},
127+
],
128+
"metadata": {},
129+
}
130+
output.append(entry)
131+
elif annotation.annotation_class.annotation_type == "polygon":
132+
entry = {
133+
"id": annotation_id,
134+
"datasetId": "darwin",
135+
"type": "segment",
136+
"label": annotation.annotation_class.name,
137+
"attributes": [],
138+
"coordinates": [{"x": point["x"], "y": point["y"], "z": 0} for point in annotation.data["path"]],
139+
"metadata": {},
140+
}
141+
output.append(entry)
142+
143+
return output
144+
145+
146+
def _build_json(annotation_file: dt.AnnotationFile, id: int) -> Dict[str, Any]:
147+
return {
148+
"_id": id,
149+
"filename": annotation_file.filename,
150+
"itemMetadata": [],
151+
"annotations": _build_annotations(annotation_file, id),
152+
}

0 commit comments

Comments
 (0)