Skip to content

Commit da56c12

Browse files
committed
Fix SAS-3401 - SAS-2283
1 parent cca132b commit da56c12

File tree

6 files changed

+112
-179
lines changed

6 files changed

+112
-179
lines changed

superannotate/db/annotation_classes.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
)
1313
from .project_api import get_project_metadata_bare
1414
from ..mixp.decorators import Trackable
15+
from .utils import get_templates_mapping
1516

1617
logger = logging.getLogger("superannotate-python-sdk")
1718

@@ -344,7 +345,11 @@ def fill_class_and_attribute_ids(annotation_json, annotation_classes_dict):
344345
'attribute_groups': {}
345346
}
346347
annotation_classes_dict = {**annotation_classes_dict, **unknown_classes}
347-
348+
templates_map = get_templates_mapping()
349+
for ann in (
350+
i for i in annotation_json["instances"] if i['type'] == 'template'
351+
):
352+
ann['templateId'] = templates_map.get(ann['templateName'], -1)
348353
for ann in annotation_json["instances"]:
349354
if "className" not in ann:
350355
logger.warning("No className in annotation instance")

superannotate/db/utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,3 +747,19 @@ def __attach_image_urls_to_project_thread(
747747
logger.warning(e)
748748
else:
749749
uploaded[thread_id] += uploaded_imgs
750+
751+
752+
def get_templates_mapping():
753+
response = _api.send_request(
754+
req_type='GET', path=f'/templates', params={"team_id": _api.team_id}
755+
)
756+
if not response.ok:
757+
raise SABaseException(
758+
response.status_code, "Couldn't get templates " + response.text
759+
)
760+
res = response.json()
761+
templates = res['data']
762+
templates_map = {}
763+
for template in templates:
764+
templates_map[template['name']] = template['id']
765+
return templates_map
Lines changed: 1 addition & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -1,175 +1 @@
1-
{
2-
"instances": [
3-
{
4-
"type": "template",
5-
"pointLabels": {
6-
"0": "ca",
7-
"1": "cb",
8-
"2": "cd",
9-
"3": "cc",
10-
"4": "ce"
11-
},
12-
"attributes": [],
13-
"creationType": "Pre-annotation",
14-
"points": [
15-
{
16-
"id": 1,
17-
"x": 271.99,
18-
"y": 114.9
19-
},
20-
{
21-
"id": 2,
22-
"x": 419.26,
23-
"y": 137.22
24-
},
25-
{
26-
"id": 3,
27-
"x": 279.01,
28-
"y": 351.5
29-
},
30-
{
31-
"id": 4,
32-
"x": 453.32,
33-
"y": 362.66
34-
},
35-
{
36-
"id": 5,
37-
"x": 349.13,
38-
"y": 233.2
39-
}
40-
],
41-
"connections": [
42-
{
43-
"id": 7,
44-
"from": 1,
45-
"to": 5
46-
},
47-
{
48-
"id": 7,
49-
"from": 2,
50-
"to": 5
51-
},
52-
{
53-
"id": 7,
54-
"from": 5,
55-
"to": 3
56-
},
57-
{
58-
"id": 7,
59-
"from": 5,
60-
"to": 4
61-
}
62-
],
63-
"className": "custom"
64-
},
65-
{
66-
"type": "template",
67-
"pointLabels": {
68-
"0": "ta",
69-
"1": "tb",
70-
"2": "tc",
71-
"3": "td"
72-
},
73-
"attributes": [],
74-
"creationType": "Pre-annotation",
75-
"points": [
76-
{
77-
"id": 1,
78-
"x": 668.76,
79-
"y": 219.62
80-
},
81-
{
82-
"id": 2,
83-
"x": 763.61,
84-
"y": 208.26
85-
},
86-
{
87-
"id": 3,
88-
"x": 799.82,
89-
"y": 377.39
90-
},
91-
{
92-
"id": 4,
93-
"x": 713.64,
94-
"y": 416.52
95-
}
96-
],
97-
"connections": [
98-
{
99-
"id": 6,
100-
"from": 1,
101-
"to": 4
102-
},
103-
{
104-
"id": 6,
105-
"from": 2,
106-
"to": 4
107-
},
108-
{
109-
"id": 6,
110-
"from": 3,
111-
"to": 4
112-
}
113-
],
114-
"className": "template2"
115-
},
116-
{
117-
"type": "template",
118-
"pointLabels": {
119-
"0": "ta",
120-
"1": "tb",
121-
"2": "tc",
122-
"3": "td"
123-
},
124-
"attributes": [],
125-
"creationType": "Pre-annotation",
126-
"points": [
127-
{
128-
"id": 1,
129-
"x": 455.12,
130-
"y": 533.02
131-
},
132-
{
133-
"id": 2,
134-
"x": 652.62,
135-
"y": 522.44
136-
},
137-
{
138-
"id": 3,
139-
"x": 728.01,
140-
"y": 679.91
141-
},
142-
{
143-
"id": 4,
144-
"x": 548.56,
145-
"y": 716.34
146-
}
147-
],
148-
"connections": [
149-
{
150-
"id": 6,
151-
"from": 1,
152-
"to": 4
153-
},
154-
{
155-
"id": 6,
156-
"from": 2,
157-
"to": 4
158-
},
159-
{
160-
"id": 6,
161-
"from": 3,
162-
"to": 4
163-
}
164-
],
165-
"className": "template2"
166-
}
167-
],
168-
"metadata": {
169-
"name": "68307_47130_68308_47130_68307_47131_68308_47131_0.png",
170-
"width": 1000,
171-
"height": 1000
172-
},
173-
"tags": [],
174-
"comments": []
175-
}
1+
{"instances": [{"type": "template", "pointLabels": {"0": "ca", "1": "cb", "2": "cd", "3": "cc", "4": "ce"}, "attributes": [], "creationType": "Pre-annotation", "points": [{"id": 1, "x": 271.99, "y": 114.9}, {"id": 2, "x": 419.26, "y": 137.22}, {"id": 3, "x": 279.01, "y": 351.5}, {"id": 4, "x": 453.32, "y": 362.66}, {"id": 5, "x": 349.13, "y": 233.2}], "connections": [{"id": 7, "from": 1, "to": 5}, {"id": 7, "from": 2, "to": 5}, {"id": 7, "from": 5, "to": 3}, {"id": 7, "from": 5, "to": 4}], "className": "class1", "templateName": "custom"}, {"type": "template", "pointLabels": {"0": "ta", "1": "tb", "2": "tc", "3": "td"}, "attributes": [], "creationType": "Pre-annotation", "points": [{"id": 1, "x": 668.76, "y": 219.62}, {"id": 2, "x": 763.61, "y": 208.26}, {"id": 3, "x": 799.82, "y": 377.39}, {"id": 4, "x": 713.64, "y": 416.52}], "connections": [{"id": 6, "from": 1, "to": 4}, {"id": 6, "from": 2, "to": 4}, {"id": 6, "from": 3, "to": 4}], "className": "class1", "templateName": "template2"}, {"type": "template", "pointLabels": {"0": "ta", "1": "tb", "2": "tc", "3": "td"}, "attributes": [], "creationType": "Pre-annotation", "points": [{"id": 1, "x": 455.12, "y": 533.02}, {"id": 2, "x": 652.62, "y": 522.44}, {"id": 3, "x": 728.01, "y": 679.91}, {"id": 4, "x": 548.56, "y": 716.34}], "connections": [{"id": 6, "from": 1, "to": 4}, {"id": 6, "from": 2, "to": 4}, {"id": 6, "from": 3, "to": 4}], "className": "class1", "templateName": "template2"}], "metadata": {"name": "68307_47130_68308_47130_68307_47131_68308_47131_0.png", "width": 1000, "height": 1000}, "tags": [], "comments": []}

tests/converter_test/test_conversion.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ def test_keypoint_detection_coco2sa_multi_template(tmpdir):
3636
out_path = Path(
3737
tmpdir
3838
) / "toSuperAnnotate" / "keypoint_detection_multi_template"
39-
print(input_dir)
40-
print(out_path)
4139

4240
sa.import_annotation(
4341
input_dir, out_path, "COCO", "keypoint_multi_template_test", "Vector",
@@ -54,7 +52,6 @@ def test_keypoint_detection_coco2sa_multi_template(tmpdir):
5452
)
5553
) as f:
5654
data = json.loads(f.read())
57-
5855
assert data == truth
5956

6057

@@ -150,3 +147,26 @@ def test_instance_segmentation_sa2coco_vector_empty_name(tmpdir):
150147
input_dir, out_path, "COCO", "instance_test_vector", "Vector",
151148
"instance_segmentation"
152149
)
150+
151+
152+
def test_upload_annotations_with_template_id(tmpdir):
153+
tmpdir = Path(tmpdir)
154+
project_name = "test_templates"
155+
for project in sa.search_projects(project_name):
156+
sa.delete_project(project)
157+
project = sa.create_project(project_name, "test", "Vector")
158+
sa.upload_images_from_folder_to_project(
159+
project, "./tests/sample_coco_with_templates"
160+
)
161+
input_dir = Path("tests") / "sample_coco_with_templates"
162+
out_path = Path(
163+
tmpdir
164+
) / "toSuperAnnotate" / "keypoint_detection_multi_template"
165+
166+
sa.import_annotation(
167+
input_dir, out_path, "COCO", "sample_coco", "Vector",
168+
"keypoint_detection"
169+
)
170+
sa.upload_annotations_from_folder_to_project(project, out_path)
171+
image_metadata = sa.get_image_annotations(project_name, "t.png")
172+
assert image_metadata['annotation_json']['instances'][0]['templateId'] == -1
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"info": {
3+
"description": "This is dataset.",
4+
"url": "https://superannotate.ai",
5+
"version": "1.0",
6+
"year": 2021,
7+
"contributor": "Superannotate AI",
8+
"date_created": "03/06/2021"
9+
},
10+
"licenses": [
11+
{
12+
"url": "https://superannotate.ai",
13+
"id": 1,
14+
"name": "Superannotate AI"
15+
}
16+
],
17+
"images": [
18+
{
19+
"id": 1,
20+
"file_name": "t.png",
21+
"height": 1540,
22+
"width": 1234,
23+
"license": 1
24+
}
25+
],
26+
"annotations": [
27+
{
28+
"id": 1,
29+
"image_id": 1,
30+
"iscrowd": 0,
31+
"bbox": [
32+
359,
33+
694,
34+
574,
35+
542
36+
],
37+
"area": 311108,
38+
"num_keypoints": 3,
39+
"keypoints": [
40+
359.26,
41+
912.51,
42+
2,
43+
817.11,
44+
1236.6,
45+
2,
46+
933.52,
47+
694.98,
48+
2
49+
],
50+
"category_id": 1
51+
}
52+
],
53+
"categories": [
54+
{
55+
"name": "template-9",
56+
"supercategory": "dfgs",
57+
"skeleton": [],
58+
"keypoints": [
59+
1,
60+
2,
61+
3
62+
],
63+
"id": 1
64+
}
65+
]
66+
}
110 KB
Loading

0 commit comments

Comments
 (0)