(captions_file, image_dir)
return image_metadata
Loads image metadata from a JSON file and processes the captions.
    Args:
      captions_file: Json file containing caption annotations.
      image_dir: Directory containing the image files.
    Returns:
      A list of ImageMetadata.
Loads image metadata from a JSON file and processes the captions.
    Args:
      captions_file: Json file containing caption annotations.
      image_dir: Directory containing the image files.
    Returns:
      A list of ImageMetadata.
Loads image metadata from a JSON file and processes the captions . Args : captions_file : Json file containing caption annotations . image_dir : Directory containing the image files . Returns : A list of ImageMetadata .
def _load_and_process_metadata(captions_file, image_dir):
    """Loads image metadata from a JSON file and processes the captions.
    Args:
      captions_file: Json file containing caption annotations.
      image_dir: Directory containing the image files.
    Returns:
      A list of ImageMetadata.
    """
    image_id = set([])
    id_to_captions = {}
    with open(captions_file, 'r') as f:
        caption_data = json.load(f)
    for data in caption_data:
        image_name = data['image_id'].split('.')[0]
        descriptions = data['caption']
        if image_name not in image_id:
            id_to_captions.setdefault(image_name, [])
            image_id.add(image_name)
        caption_num = len(descriptions)
        for i in range(caption_num):
            caption_temp = descriptions[i].strip().strip("。").replace('\n', '')
            if caption_temp != '':
                id_to_captions[image_name].append(caption_temp)


    print("Loaded caption metadata for %d images from %s and image_id num is %s" %
          (len(id_to_captions), captions_file, len(image_id)))
    # Process the captions and combine the data into a list of ImageMetadata.
    print("Proccessing captions.")
    image_metadata = []
    num_captions = 0
    id = 0
    for base_filename in image_id:
        filename = os.path.join(image_dir, base_filename + '.jpg')
        # captions = [_process_caption(c) for c in id_to_captions[base_filename]]
        captions = [_process_caption_jieba(c) for c in id_to_captions[base_filename]]
        image_metadata.append(ImageMetadata(id, filename, captions))
        id = id + 1
        num_captions += len(captions)
    print("Finished processing %d captions for %d images in %s" %
          (num_captions, len(id_to_captions), captions_file))
    return image_metadata
def _load_and_process_metadata ( captions_file , image_dir ) : image_id = set ( [ ] ) id_to_captions = { } with open ( captions_file , 'r' ) as f : caption_data = json . load ( f ) for data in caption_data : image_name = data [ 'image_id' ] . split ( '.' ) [ 0 ] descriptions = data [ 'caption' ] if image_name not in image_id : id_to_captions . setdefault ( image_name , [ ] ) image_id . add ( image_name ) caption_num = len ( descriptions ) for i in range ( caption_num ) : caption_temp = descriptions [ i ] . strip ( ) . strip ( "。"). r e place(' \ n',  ' ) if caption_temp != '' : id_to_captions [ image_name ] . append ( caption_temp ) print ( "Loaded caption metadata for %d images from %s and image_id num is %s" % ( len ( id_to_captions ) , captions_file , len ( image_id ) ) ) # Process the captions and combine the data into a list of ImageMetadata. print ( "Proccessing captions." ) image_metadata = [ ] num_captions = 0 id = 0 for base_filename in image_id : filename = os . path . join ( image_dir , base_filename + '.jpg' ) # captions = [_process_caption(c) for c in id_to_captions[base_filename]] captions = [ _process_caption_jieba ( c ) for c in id_to_captions [ base_filename ] ] image_metadata . append ( ImageMetadata ( id , filename , captions ) ) id = id + 1 num_captions += len ( captions ) print ( "Finished processing %d captions for %d images in %s" % ( num_captions , len ( id_to_captions ) , captions_file ) ) return image_metadata