Skip to content

Commit 788057f

Browse files
committed
Fix old jsons conversion
1 parent 076f4bb commit 788057f

File tree

9 files changed

+249
-8
lines changed

9 files changed

+249
-8
lines changed

superannotate/db/annotation_classes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,21 @@ def fill_class_and_attribute_names(annotations_json, annotation_classes_dict):
330330
def fill_class_and_attribute_ids(annotation_json, annotation_classes_dict):
331331
if "instances" not in annotation_json:
332332
return
333+
unknown_classes = {}
334+
for ann in annotation_json["instances"]:
335+
if "className" not in ann:
336+
continue
337+
annotation_class_name = ann["className"]
338+
if not annotation_class_name in annotation_classes_dict:
339+
if annotation_class_name not in unknown_classes:
340+
class_num = -(len(unknown_classes) + 1)
341+
unknown_classes[annotation_class_name] = {
342+
'id': class_num,
343+
'attribute_groups': {}
344+
}
345+
for k in unknown_classes:
346+
annotation_classes_dict[k] = unknown_classes[k]
347+
333348
for ann in annotation_json["instances"]:
334349
if "className" not in ann:
335350
logger.warning("No className in annotation instance")

superannotate/input_converters/converters/coco_converters/coco_converter.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
class CocoBaseStrategy(baseStrategy):
2020
project_type_to_json_ending = {
21-
'Pixel': '___pixel.json',
22-
'Vector': '___objects.json',
2321
'pixel': '___pixel.json',
2422
'vector': '___objects.json'
2523
}
@@ -173,7 +171,7 @@ def _parse_json_into_common_format(self, sa_annotation_json, fpath):
173171
'metadata'] or sa_annotation_json['metadata']['name'] is None:
174172
fname = fpath.name
175173
fname = fname[:-len(
176-
self.project_type_to_json_ending[self.project_type]
174+
self.project_type_to_json_ending[self.project_type.lower()]
177175
)]
178176
sa_annotation_json['metadata']['name'] = fname
179177
sa_annotation_json['metadata']['image_path'] = str(

superannotate/mixp/decorators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ def __call__(self, *args, **kwargs):
3636
properties = {**default, **properties}
3737
mp.track(user_id, event_name, properties)
3838
except:
39-
print('--- mix panel exception ---')
39+
pass
4040
return self.function(*args, **kwargs)

tests/converter_test/test_conversion.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from pathlib import Path
2-
32
from mixpanel import json_dumps
43

54
import superannotate as sa
@@ -100,7 +99,9 @@ def test_instance_segmentation_sa2coco_vector_empty_array(tmpdir):
10099
input_dir = Path(
101100
"tests"
102101
) / "converter_test" / "COCO" / "input" / "fromSuperAnnotate" / "cats_dogs_vector_instance_segm_empty_array"
103-
out_path = Path(tmpdir) / "fromSuperAnnotate" / "instance_test_vector"
102+
out_path = Path(
103+
tmpdir
104+
) / "empty_array" / "fromSuperAnnotate" / "instance_test_vector"
104105
sa.export_annotation(
105106
input_dir, out_path, "COCO", "instance_test_vector", "Vector",
106107
"instance_segmentation"
@@ -112,14 +113,16 @@ def test_instance_segmentation_sa2coco_vector_empty_array(tmpdir):
112113
truth_path = input_dir / "truth.json"
113114
with open(truth_path, 'r') as f:
114115
truth = json.loads(f.read())
115-
assert truth == data
116+
assert truth['annotations'][0]['bbox'] == data['annotations'][0]['bbox']
116117

117118

118119
def test_instance_segmentation_sa2coco_vector_empty_name(tmpdir):
119120
input_dir = Path(
120121
"tests"
121122
) / "converter_test" / "COCO" / "input" / "fromSuperAnnotate" / "vector_no_name"
122-
out_path = Path("sssss1") / "fromSuperAnnotate" / "instance_test_vector"
123+
out_path = Path(
124+
tmpdir
125+
) / "empty_name" / "fromSuperAnnotate" / "instance_test_vector"
123126
sa.export_annotation(
124127
input_dir, out_path, "COCO", "instance_test_vector", "Vector",
125128
"instance_segmentation"
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
[
2+
{
3+
"id": 55917,
4+
"project_id": 11979,
5+
"name": "Personal vehicle",
6+
"color": "#ecb65f",
7+
"count": 25,
8+
"createdAt": "2020-10-12T11:35:20.000Z",
9+
"updatedAt": "2020-10-12T11:48:19.000Z",
10+
"attribute_groups": [
11+
{
12+
"id": 17245,
13+
"class_id": 55917,
14+
"name": "Num doors",
15+
"is_multiselect": 0,
16+
"createdAt": "2020-10-12T11:35:20.000Z",
17+
"updatedAt": "2020-10-12T11:35:20.000Z",
18+
"attributes": [
19+
{
20+
"id": 62792,
21+
"group_id": 17245,
22+
"project_id": 11979,
23+
"name": "2",
24+
"count": 1,
25+
"createdAt": "2020-10-12T11:35:20.000Z",
26+
"updatedAt": "2020-10-12T11:46:28.000Z"
27+
},
28+
{
29+
"id": 62793,
30+
"group_id": 17245,
31+
"project_id": 11979,
32+
"name": "4",
33+
"count": 1,
34+
"createdAt": "2020-10-12T11:35:20.000Z",
35+
"updatedAt": "2020-10-12T11:35:20.000Z"
36+
}
37+
]
38+
}
39+
]
40+
},
41+
{
42+
"id": 55918,
43+
"project_id": 11979,
44+
"name": "Large vehicle",
45+
"color": "#737b28",
46+
"count": 1,
47+
"createdAt": "2020-10-12T11:35:20.000Z",
48+
"updatedAt": "2020-10-12T11:48:19.000Z",
49+
"attribute_groups": [
50+
{
51+
"id": 17246,
52+
"class_id": 55918,
53+
"name": "swedish",
54+
"is_multiselect": 0,
55+
"createdAt": "2020-10-12T11:35:20.000Z",
56+
"updatedAt": "2020-10-12T11:35:20.000Z",
57+
"attributes": [
58+
{
59+
"id": 62794,
60+
"group_id": 17246,
61+
"project_id": 11979,
62+
"name": "yes",
63+
"count": 0,
64+
"createdAt": "2020-10-12T11:35:20.000Z",
65+
"updatedAt": "2020-10-12T11:35:20.000Z"
66+
},
67+
{
68+
"id": 62795,
69+
"group_id": 17246,
70+
"project_id": 11979,
71+
"name": "no",
72+
"count": 1,
73+
"createdAt": "2020-10-12T11:35:20.000Z",
74+
"updatedAt": "2020-10-12T11:46:28.000Z"
75+
}
76+
]
77+
},
78+
{
79+
"id": 17247,
80+
"class_id": 55918,
81+
"name": "Num doors",
82+
"is_multiselect": 0,
83+
"createdAt": "2020-10-12T11:35:20.000Z",
84+
"updatedAt": "2020-10-12T11:35:20.000Z",
85+
"attributes": [
86+
{
87+
"id": 62796,
88+
"group_id": 17247,
89+
"project_id": 11979,
90+
"name": "2",
91+
"count": 0,
92+
"createdAt": "2020-10-12T11:35:20.000Z",
93+
"updatedAt": "2020-10-12T11:35:20.000Z"
94+
},
95+
{
96+
"id": 62797,
97+
"group_id": 17247,
98+
"project_id": 11979,
99+
"name": "4",
100+
"count": 1,
101+
"createdAt": "2020-10-12T11:35:20.000Z",
102+
"updatedAt": "2020-10-12T11:46:28.000Z"
103+
}
104+
]
105+
}
106+
]
107+
},
108+
{
109+
"id": 55919,
110+
"project_id": 11979,
111+
"name": "Human",
112+
"color": "#e4542b",
113+
"count": 9,
114+
"createdAt": "2020-10-12T11:35:20.000Z",
115+
"updatedAt": "2020-10-12T11:48:14.000Z",
116+
"attribute_groups": [
117+
{
118+
"id": 17248,
119+
"class_id": 55919,
120+
"name": "Height",
121+
"is_multiselect": 0,
122+
"createdAt": "2020-10-12T11:35:20.000Z",
123+
"updatedAt": "2020-10-12T11:35:20.000Z",
124+
"attributes": [
125+
{
126+
"id": 62798,
127+
"group_id": 17248,
128+
"project_id": 11979,
129+
"name": "Tall",
130+
"count": 0,
131+
"createdAt": "2020-10-12T11:35:20.000Z",
132+
"updatedAt": "2020-10-12T11:35:20.000Z"
133+
},
134+
{
135+
"id": 62799,
136+
"group_id": 17248,
137+
"project_id": 11979,
138+
"name": "Short",
139+
"count": 0,
140+
"createdAt": "2020-10-12T11:35:20.000Z",
141+
"updatedAt": "2020-10-12T11:35:20.000Z"
142+
}
143+
]
144+
}
145+
]
146+
},
147+
{
148+
"id": 55920,
149+
"project_id": 11979,
150+
"name": "Plant",
151+
"color": "#46ccb2",
152+
"count": 0,
153+
"createdAt": "2020-10-12T11:35:20.000Z",
154+
"updatedAt": "2020-10-12T11:35:20.000Z",
155+
"attribute_groups": []
156+
}
157+
]
209 KB
Loading
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"metadata": {
3+
"name": "example_image_1.jpg",
4+
"width": 1024,
5+
"height": 683,
6+
"status": "Completed",
7+
"pinned": false,
8+
"isPredicted": null,
9+
"projectId": null,
10+
"annotatorEmail": null,
11+
"qaEmail": null
12+
},
13+
"instances": [
14+
{
15+
"type": "bbox",
16+
"probability": 100,
17+
"points": {
18+
"x1": 437.16,
19+
"x2": 465.23,
20+
"y1": 341.5,
21+
"y2": 357.09
22+
},
23+
"groupId": 0,
24+
"pointLabels": {},
25+
"locked": false,
26+
"visible": false,
27+
"attributes": [
28+
{
29+
"name": "unknown attribute name",
30+
"groupName": "unknown group name"
31+
}
32+
],
33+
"trackingId": "aaa97f80c9e54a5f2dc2e920fc92e5033d9af45b",
34+
"error": null,
35+
"createdAt": null,
36+
"createdBy": null,
37+
"creationType": null,
38+
"updatedAt": null,
39+
"updatedBy": null,
40+
"className": "Unknown123"
41+
}
42+
],
43+
"tags": [],
44+
"comments": []
45+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"annotation_json": {"metadata": {"name": "example_image_1.jpg", "width": 1024, "height": 683, "status": "Completed", "pinned": false, "isPredicted": null, "projectId": null, "annotatorEmail": null, "qaEmail": null}, "instances": [{"type": "bbox", "probability": 100, "points": {"x1": 437.16, "x2": 465.23, "y1": 341.5, "y2": 357.09}, "groupId": 0, "pointLabels": {}, "locked": false, "visible": false, "attributes": [{"name": "unknown attribute name", "groupName": "unknown group name"}], "trackingId": "aaa97f80c9e54a5f2dc2e920fc92e5033d9af45b", "error": null, "createdAt": null, "createdBy": null, "creationType": null, "updatedAt": null, "updatedBy": null, "className": "Unknown123", "classId": -1}], "tags": [], "comments": []}, "annotation_json_filename": "example_image_1.jpg___objects.json"}

tests/test_basic_project.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,25 @@ def test_basic_project(project_type, name, description, from_folder, tmpdir):
9494
found = True
9595
break
9696
assert found, mask_in_folder
97+
98+
99+
def test_upload_annotations():
100+
FROM_FOLDER = Path("./tests/sample_annotation_no_class")
101+
PROJECT_NAME = "testnoclass"
102+
projects_found = sa.search_projects(PROJECT_NAME, return_metadata=True)
103+
for pr in projects_found:
104+
sa.delete_project(pr)
105+
project = sa.create_project(PROJECT_NAME, 'test', 'Vector')
106+
project = project["name"]
107+
sa.upload_images_from_folder_to_project(
108+
project, FROM_FOLDER, annotation_status="InProgress"
109+
)
110+
sa.create_annotation_classes_from_classes_json(
111+
project, FROM_FOLDER / "classes" / "classes.json"
112+
)
113+
sa.upload_annotations_from_folder_to_project(project, FROM_FOLDER)
114+
annot = sa.get_image_annotations(project, "example_image_1.jpg")
115+
truth_path = FROM_FOLDER / "truth.json"
116+
with open(truth_path, 'r') as f:
117+
data = json.loads(f.read())
118+
assert data == annot

0 commit comments

Comments
 (0)