Skip to content

Commit 02a02b6

Browse files
committed
coco decoder
1 parent 4067f81 commit 02a02b6

File tree

20 files changed

+118
-298
lines changed

20 files changed

+118
-298
lines changed

superannotate/input_converters/converters/coco_converters/coco_to_sa_pixel.py

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,28 +103,51 @@ def coco_instance_segmentation_to_sa_pixel(coco_path, images_path):
103103
hexcolors = blue_color_generator(len(coco_json['annotations']))
104104
for i, annot in enumerate(coco_json['annotations']):
105105
if str(annot['image_id']) not in images_dict:
106-
print('check')
107106
continue
108107

109108
hexcolor = hexcolors[images_dict[str(annot['image_id'])]['segments_num']
110109
]
111110
color = hex_to_rgb(hexcolor)
112111
images_dict[str(annot['image_id'])]['segments_num'] += 1
113112

114-
if isinstance(annot['segmentation'], dict):
115-
annot['segmentation'] = _rle_to_polygon(coco_path, annot)
116-
117-
segment = annot['segmentation'][0]
118113
H, W, C = images_dict[str(annot['image_id'])]['mask'].shape
119-
bitmask = np.zeros((H, W)).astype(np.uint8)
120-
pts = np.array(
121-
[segment[2 * i:2 * (i + 1)] for i in range(len(segment) // 2)],
122-
dtype=np.int32
123-
)
114+
if isinstance(annot['segmentation'], dict):
115+
if isinstance(annot['segmentation']['counts'], list):
116+
annot['segmentation'] = _rle_to_polygon(coco_path, annot)
117+
for segment in annot['segmentation']:
118+
bitmask = np.zeros((H, W)).astype(np.uint8)
119+
pts = np.array(
120+
[
121+
segment[2 * i:2 * (i + 1)]
122+
for i in range(len(segment) // 2)
123+
],
124+
dtype=np.int32
125+
)
126+
127+
cv2.fillPoly(bitmask, [pts], 1)
128+
images_dict[str(
129+
annot['image_id']
130+
)]['mask'][bitmask == 1] = list(color)[::-1] + [255]
131+
else:
132+
images_dict[str(
133+
annot['image_id']
134+
)]['mask'][maskUtils.decode(annot['segmentation']) == 1
135+
] = list(color)[::-1] + [255]
136+
else:
137+
for segment in annot['segmentation']:
138+
bitmask = np.zeros((H, W)).astype(np.uint8)
139+
pts = np.array(
140+
[
141+
segment[2 * i:2 * (i + 1)]
142+
for i in range(len(segment) // 2)
143+
],
144+
dtype=np.int32
145+
)
124146

125-
cv2.fillPoly(bitmask, [pts], 1)
126-
images_dict[str(annot['image_id']
127-
)]['mask'][bitmask == 1] = list(color)[::-1] + [255]
147+
cv2.fillPoly(bitmask, [pts], 1)
148+
images_dict[str(
149+
annot['image_id']
150+
)]['mask'][bitmask == 1] = list(color)[::-1] + [255]
128151

129152
sa_obj = {
130153
"classId": annot['category_id'],

superannotate/input_converters/converters/coco_converters/coco_to_sa_vector.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pycocotools.coco import COCO
1212
from panopticapi.utils import id2rgb
1313
from tqdm import tqdm
14+
import pycocotools.mask as maskUtils
1415

1516

1617
# Returns unique values of list. Values can be dicts or lists!
@@ -44,16 +45,40 @@ def coco_instance_segmentation_to_sa_vector(coco_path, images_path):
4445
for cat in coco_json['categories']:
4546
cat_id_to_cat[cat['id']] = cat
4647

48+
instance_groups = {}
49+
for annot in coco_json['annotations']:
50+
if 'id' in annot:
51+
if annot['id'] in instance_groups:
52+
instance_groups[annot['id']] += 1
53+
else:
54+
instance_groups[annot['id']] = 1
55+
4756
for annot in tqdm(coco_json['annotations'], "Converting annotations"):
48-
if 'iscrowd' in annot and annot['iscrowd'] == 1:
49-
try:
57+
if isinstance(annot['segmentation'], dict):
58+
if isinstance(annot['segmentation']['counts'], list):
5059
annot['segmentation'] = _rle_to_polygon(coco_path, annot)
51-
except IndexError:
52-
print("List index out of range")
60+
else:
61+
binary_mask = maskUtils.decode(annot['segmentation'])
62+
contours, _ = cv2.findContours(
63+
binary_mask.astype(np.uint8), cv2.RETR_EXTERNAL,
64+
cv2.CHAIN_APPROX_SIMPLE
65+
)
66+
segments = []
67+
for contour in contours:
68+
contour = contour.flatten().tolist()
69+
if len(contour) > 4:
70+
segments.append(contour)
71+
annot['segmentation'] = segments
5372

5473
cat = cat_id_to_cat[annot['category_id']]
55-
sa_polygon_loader = [
56-
{
74+
sa_polygon_loader = []
75+
groupid = 0
76+
for polygon in annot['segmentation']:
77+
if 'id' in annot:
78+
if annot['id'] in instance_groups and instance_groups[
79+
annot['id']] > 1:
80+
groupid = annot['id']
81+
sa_obj = {
5782
'type': 'polygon',
5883
'points': polygon,
5984
'className': cat['name'],
@@ -62,10 +87,10 @@ def coco_instance_segmentation_to_sa_vector(coco_path, images_path):
6287
'probability': 100,
6388
'locked': False,
6489
'visible': True,
65-
'groupId': annot['id'],
90+
'groupId': groupid,
6691
'imageId': annot['image_id']
67-
} for polygon in annot['segmentation']
68-
]
92+
}
93+
sa_polygon_loader.append(sa_obj)
6994

7095
for polygon in sa_polygon_loader:
7196
if polygon['imageId'] not in image_id_to_annotations:
@@ -79,7 +104,7 @@ def coco_instance_segmentation_to_sa_vector(coco_path, images_path):
79104
continue
80105
f_loader = image_id_to_annotations[img['id']]
81106
if 'file_name' in img:
82-
image_path = img['file_name']
107+
image_path = os.path.basename(img['file_name'])
83108
else:
84109
image_path = img['coco_url'].split('/')[-1]
85110
file_name = image_path + "___objects.json"
@@ -133,7 +158,7 @@ def coco_object_detection_to_sa_vector(coco_path, images_path):
133158
continue
134159
f_loader = image_id_to_annotations[img['id']]
135160
if 'file_name' in img:
136-
image_path = img['file_name']
161+
image_path = os.path.basename(img['file_name'])
137162
else:
138163
image_path = img['coco_url'].split('/')[-1]
139164
file_name = image_path + "___objects.json"
@@ -239,6 +264,6 @@ def coco_keypoint_detection_to_sa_vector(coco_path, images_path):
239264
for img_id, img_data in loader:
240265
if img['id'] == img_id:
241266
f_loader.append(img_data)
242-
file_name = img['file_name'] + "___objects.json"
267+
file_name = os.path.basename(img['file_name']) + "___objects.json"
243268
sa_jsons[file_name] = f_loader
244269
return sa_jsons
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"dog3.jpg":[{"type":"bbox","classId":1,"probability":100,"points":{"x1":192.5,"x2":554.2,"y1":65.6,"y2":681},"groupId":0,"pointLabels":{},"locked":false,"visible":true,"attributes":[]},{"type":"meta","name":"lastAction","timestamp":1605698344455}],"dog2.jpg":[{"type":"bbox","classId":1,"probability":100,"points":{"x1":119.55786895751953,"x2":407.70001220703125,"y1":28.329744338989258,"y2":458.6000061035156},"groupId":0,"pointLabels":{},"locked":false,"visible":true,"attributes":[]},{"type":"meta","name":"lastAction","timestamp":1605698356828}],"dog1.jpg":[{"type":"bbox","classId":1,"probability":100,"points":{"x1":181.88186645507812,"x2":653.2000122070312,"y1":83.06184387207031,"y2":462.3999938964844},"groupId":0,"pointLabels":{},"locked":false,"visible":true,"attributes":[]},{"type":"meta","name":"lastAction","timestamp":1605698370093}],"cat3.jpg":[{"type":"bbox","classId":2,"probability":100,"points":{"x1":0,"x2":529.7525634765625,"y1":30.130569458007812,"y2":862.4000244140625},"groupId":0,"pointLabels":{},"locked":false,"visible":true,"attributes":[]},{"type":"meta","name":"lastAction","timestamp":1605698384506}],"cat2.jpg":[{"type":"bbox","classId":2,"probability":100,"points":{"x1":254.9,"x2":1043.2,"y1":101.9,"y2":732},"groupId":0,"pointLabels":{},"locked":false,"visible":true,"attributes":[]},{"type":"meta","name":"lastAction","timestamp":1605698428282}],"cat1.jpg":[{"type":"bbox","classId":2,"probability":100,"points":{"x1":483.3,"x2":1547.9,"y1":0,"y2":1924.3},"groupId":0,"pointLabels":{},"locked":false,"visible":true,"attributes":[]},{"type":"meta","name":"lastAction","timestamp":1605698430409}]}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"attribute_groups":[],"color":"#32a852","id":1,"name":"Dog","opened":true},{"attribute_groups":[],"color":"#6a010f","id":2,"name":"Cat","opened":true}]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"pathSeparator":"/","os":"linux"}
748 KB
Loading
135 KB
Loading
109 KB
Loading
141 KB
Loading
55.9 KB
Loading

0 commit comments

Comments
 (0)