Skip to content

Commit e30667d

Browse files
authored
update converting unique coco labels (#27)
1 parent c552243 commit e30667d

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

general_json2yolo.py

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import cv2
55
import pandas as pd
66
from PIL import Image
7+
from collections import defaultdict
78

89
from utils import *
910

@@ -262,36 +263,49 @@ def convert_coco_json(json_dir='../coco/annotations/', use_segments=False, cls91
262263

263264
# Create image dict
264265
images = {'%g' % x['id']: x for x in data['images']}
266+
# Create image-annotations dict
267+
imgToAnns = defaultdict(list)
268+
for ann in data['annotations']:
269+
imgToAnns[ann['image_id']].append(ann)
265270

266271
# Write labels file
267-
for x in tqdm(data['annotations'], desc=f'Annotations {json_file}'):
268-
if x['iscrowd']:
269-
continue
270-
271-
img = images['%g' % x['image_id']]
272+
for img_id, anns in tqdm(imgToAnns.items(), desc=f'Annotations {json_file}'):
273+
img = images['%g' % img_id]
272274
h, w, f = img['height'], img['width'], img['file_name']
273275

274-
# The COCO box format is [top left x, top left y, width, height]
275-
box = np.array(x['bbox'], dtype=np.float64)
276-
box[:2] += box[2:] / 2 # xy top-left corner to center
277-
box[[0, 2]] /= w # normalize x
278-
box[[1, 3]] /= h # normalize y
279-
280-
# Segments
281-
if use_segments:
282-
if len(x['segmentation']) > 1:
283-
s = merge_multi_segment(x['segmentation'])
284-
s = (np.concatenate(s, axis=0) / np.array([w, h])).reshape(-1).tolist()
285-
286-
else:
287-
segments = [j for i in x['segmentation'] for j in i] # all segments concatenated
288-
s = (np.array(segments).reshape(-1, 2) / np.array([w, h])).reshape(-1).tolist()
276+
bboxes = []
277+
segments = []
278+
for ann in anns:
279+
if ann['iscrowd']:
280+
continue
281+
# The COCO box format is [top left x, top left y, width, height]
282+
box = np.array(ann['bbox'], dtype=np.float64)
283+
box[:2] += box[2:] / 2 # xy top-left corner to center
284+
box[[0, 2]] /= w # normalize x
285+
box[[1, 3]] /= h # normalize y
286+
if box[2] <= 0 or box[3] <= 0: # if w <= 0 and h <= 0
287+
continue
288+
289+
cls = coco80[ann['category_id'] - 1] if cls91to80 else ann['category_id'] - 1 # class
290+
box = [cls] + box.tolist()
291+
if box not in bboxes:
292+
bboxes.append(box)
293+
# Segments
294+
if use_segments:
295+
if len(ann['segmentation']) > 1:
296+
s = merge_multi_segment(ann['segmentation'])
297+
s = (np.concatenate(s, axis=0) / np.array([w, h])).reshape(-1).tolist()
298+
else:
299+
s = [j for i in ann['segmentation'] for j in i] # all segments concatenated
300+
s = (np.array(s).reshape(-1, 2) / np.array([w, h])).reshape(-1).tolist()
301+
s = [cls] + s
302+
if s not in segments:
303+
segments.append(s)
289304

290305
# Write
291-
if box[2] > 0 and box[3] > 0: # if w > 0 and h > 0
292-
cls = coco80[x['category_id'] - 1] if cls91to80 else x['category_id'] - 1 # class
293-
line = cls, *(s if use_segments else box) # cls, box or segments
294-
with open((fn / f).with_suffix('.txt'), 'a') as file:
306+
with open((fn / f).with_suffix('.txt'), 'a') as file:
307+
for i in range(len(bboxes)):
308+
line = *(segments[i] if use_segments else bboxes[i]), # cls, box or segments
295309
file.write(('%g ' * len(line)).rstrip() % line + '\n')
296310

297311

0 commit comments

Comments
 (0)