import json
import glob
import os
import shutil
from pathlib import Path
import numpy as np
from tqdm import tqdm
def make_folders(path='../out/'):
if os.path.exists(path):
shutil.rmtree(path)
os.makedirs(path)
os.makedirs(path + os.sep + 'labels')
os.makedirs(path + os.sep + 'images')
return path
def convert_coco_json(json_dir='../coco/annotations/'):
dir = make_folders(path='out/')
jsons = glob.glob(json_dir + '*.json')
coco80 = coco91_to_coco80_class()
for json_file in sorted(jsons):
fn = 'out/labels/%s/' % Path(json_file).stem.replace('instances_', '')
os.mkdir(fn)
with open(json_file) as f:
data = json.load(f)
images = {'%g' % x['id']: x for x in data['images']}
for x in tqdm(data['annotations'], desc='Annotations %s' % json_file):
if x['iscrowd']:
continue
img = images['%g' % x['image_id']]
h, w, f = img['height'], img['width'], img['file_name']
box = np.array(x['bbox'], dtype=np.float64)
box[:2] += box[2:] / 2
box[[0, 2]] /= w
box[[1, 3]] /= h
if (box[2] > 0.) and (box[3] > 0.):
with open(fn + Path(f).stem + '.txt', 'a') as file:
file.write('%g %.6f %.6f %.6f %.6f\n' % (coco80[x['category_id'] - 1], *box))
def coco91_to_coco80_class():
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, None, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, None, 24, 25, None,
None, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, None, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, None, 60, None, None, 61, None, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72,
None, 73, 74, 75, 76, 77, 78, 79, None]
return x
convert_coco_json()