def _load_and_process_metadata(captions_file, image_dir):
 with tf.gfile.FastGFile(captions_file, 'r') as f:
  caption_data = json.load(f)
 id_to_filename = [(x['id'], x['file_name']) for x in caption_data['images']]
 id_to_captions = {}
 for annotation in caption_data['annotations']:
  image_id = annotation['image_id']
  caption = annotation['caption']
  id_to_captions.setdefault(image_id, [])
  id_to_captions[image_id].append(caption)
 assert (len(id_to_filename) == len(id_to_captions))
 assert (set([x[0] for x in id_to_filename]) == set(id_to_captions.keys()))
 print(('Loaded caption metadata for %d images from %s' % (len(id_to_filename), captions_file)))
 print('Proccessing captions.')
 image_metadata = []
 num_captions = 0
 for (image_id, base_filename) in id_to_filename:
  filename = os.path.join(image_dir, base_filename)
  captions = [_process_caption(c) for c in id_to_captions[image_id]]
  image_metadata.append(ImageMetadata(image_id, filename, captions))
  num_captions += len(captions)
 print(('Finished processing %d captions for %d images in %s' % (num_captions, len(id_to_filename), captions_file)))
 return image_metadata

def _load_and_process_metadata ( captions_file , image_dir ) : with tf . gfile . FastGFile ( captions_file , 'r' ) as f : caption_data = json . load ( f ) id_to_filename = [ ( x [ 'id' ] , x [ 'file_name' ] ) for x in caption_data [ 'images' ] ] id_to_captions = { } for annotation in caption_data [ 'annotations' ] : image_id = annotation [ 'image_id' ] caption = annotation [ 'caption' ] id_to_captions . setdefault ( image_id , [ ] ) id_to_captions [ image_id ] . append ( caption ) assert ( len ( id_to_filename ) == len ( id_to_captions ) ) assert ( set ( [ x [ 0 ] for x in id_to_filename ] ) == set ( id_to_captions . keys ( ) ) ) print ( ( 'Loaded caption metadata for %d images from %s' % ( len ( id_to_filename ) , captions_file ) ) ) print ( 'Proccessing captions.' ) image_metadata = [ ] num_captions = 0 for ( image_id , base_filename ) in id_to_filename : filename = os . path . join ( image_dir , base_filename ) captions = [ _process_caption ( c ) for c in id_to_captions [ image_id ] ] image_metadata . append ( ImageMetadata ( image_id , filename , captions ) ) num_captions += len ( captions ) print ( ( 'Finished processing %d captions for %d images in %s' % ( num_captions , len ( id_to_filename ) , captions_file ) ) ) return image_metadata
loads image metadata from a json file and processes the captions .