#!/usr/bin/env python3

import os
import json

def extract(coco_file,map_file):
    
    with open(coco_file) as fd:
        coco_data = json.loads(fd.read())

    with open(map_file,'w') as fd:
        for entry in coco_data['images']:
            img_fname = entry['file_name']
            coco_id = entry['id']
            fhash = os.path.basename(img_fname).split('.')[0]
            nabird_id = '{}-{}-{}-{}-{}'.format(fhash[0:8],
                                                fhash[8:12],
                                                fhash[12:16],
                                                fhash[16:20],
                                                fhash[20:])

            fd.write(f'{coco_id} {nabird_id}\n')

if __name__ == '__main__':

    for split in ['train','val','test']:
        extract(f'../data/{split}.json',f'../data/{split}.map')
