/
exporter.py
69 lines (60 loc) · 2.06 KB
/
exporter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from pathlib import Path
from typing import Iterator, List
from darwin.datatypes import AnnotationFile, ExportParser, PathLike
from darwin.utils import parse_darwin_json, split_video_annotation
def darwin_to_dt_gen(
file_paths: List[PathLike], split_sequences: bool
) -> Iterator[AnnotationFile]:
"""
Parses the given paths recursively and into an ``Iterator`` of ``AnnotationFile``\\s.
Parameters
----------
file_paths : List[PathLike]
The paths of the files or directories we want to parse.
split_sequences: bool
When `True`, all videos will be split into individual frame images.
Returns
-------
Iterator[AnnotationFile]
An ``Iterator`` of the parsed ``AnnotationFile``\\s.
"""
count = 0
for file_path in map(Path, file_paths):
files = file_path.glob("**/*") if file_path.is_dir() else [file_path]
for f in files:
if f.suffix != ".json":
continue
data = parse_darwin_json(f, count)
if data:
if data.is_video and split_sequences:
for d in split_video_annotation(data):
d.seq = count
count += 1
print(count)
yield d
else:
yield data
count += 1
def export_annotations(
exporter: ExportParser,
file_paths: List[PathLike],
output_directory: PathLike,
split_sequences: bool = True,
) -> None:
"""
Converts a set of files to a different annotation format.
Parameters
----------
exporter : ExportParser
The parser to use.
file_paths : List[PathLike]
The files we want to parse.
output_directory : PathLike
Where the parsed files will be placed after the operation is complete.
"""
print("Converting annotations...")
exporter(
darwin_to_dt_gen(file_paths, split_sequences=split_sequences),
Path(output_directory),
)
print(f"Converted annotations saved at {output_directory}")