-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_cvat_annotations.py
62 lines (49 loc) · 1.69 KB
/
merge_cvat_annotations.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
import argparse
import json
import os
from config import (
DATASET_ANNOTATIONS_FOLDER
)
from dataset import Dataset
from utils import setup
def merge(data: dict, metadata: dict) -> dict:
merged_data = {
"info": metadata["info"],
"images": metadata["images"],
"annotations": data["annotations"],
"categories": metadata["categories"],
}
return merged_data
def main():
setup()
parser = argparse.ArgumentParser(description="Merges 2 annotation json files, preserving the metadata in one and labels in the other")
parser.add_argument(
"-metadata",
type=str,
required=True,
help="File with correct metadata"
)
parser.add_argument(
"-data",
type=str,
required=True,
help="File with correct data"
)
args = parser.parse_args()
metadata_filename = args.metadata
data_filename = args.data
metadata_filename = os.path.join(DATASET_ANNOTATIONS_FOLDER, metadata_filename)
assert os.path.exists(metadata_filename), "Metadata filename not found"
data_filename = os.path.join(DATASET_ANNOTATIONS_FOLDER, data_filename)
assert os.path.exists(data_filename), "Data filename not found"
with open(metadata_filename) as file:
metadata = json.load(file)
with open(data_filename) as file:
data = json.load(file)
print("Merging cvat annotations...")
merged_data = merge(data, metadata)
with open(Dataset.get_annotations_path(), "w") as file:
json.dump(merged_data, file, indent=4)
print(f"CVAT annotations succesfully written to {Dataset.get_annotations_path()}")
if __name__ == "__main__":
main()