third_party/tflite_support/src/tensorflow_lite_support/metadata/python/metadata.py
def _populate_metadata_buffer(self):
    """Populates the metadata buffer (in bytearray) into the model file.

    Inserts metadata_buf into the metadata field of schema.Model. If the
    MetadataPopulator object is created using the method,
    with_model_file(model_file), the model file will be updated.

    Existing metadata buffer (if applied) will be overridden by the new metadata
    buffer.
    """

    with open(self._model_file, "rb") as f:
      model_buf = f.read()

    model = _schema_fb.ModelT.InitFromObj(
        _schema_fb.Model.GetRootAsModel(model_buf, 0))
    buffer_field = _schema_fb.BufferT()
    buffer_field.data = self._metadata_buf

    is_populated = False
    if not model.metadata:
      model.metadata = []
    else:
      # Check if metadata has already been populated.
      for meta in model.metadata:
        if meta.name.decode("utf-8") == self.METADATA_FIELD_NAME:
          is_populated = True
          model.buffers[meta.buffer] = buffer_field

    if not is_populated:
      if not model.buffers:
        model.buffers = []
      model.buffers.append(buffer_field)
      # Creates a new metadata field.
      metadata_field = _schema_fb.MetadataT()
      metadata_field.name = self.METADATA_FIELD_NAME
      metadata_field.buffer = len(model.buffers) - 1
      model.metadata.append(metadata_field)

    # Packs model back to a flatbuffer binaray file.
    b = flatbuffers.Builder(0)
    b.Finish(model.Pack(b), self.TFLITE_FILE_IDENTIFIER)
    model_buf = b.Output()

    # Saves the updated model buffer to model file.
    # Gets files that have been packed to self._model_file.
    packed_files = self.get_packed_associated_file_list()
    if packed_files:
      # Writes the updated model buffer and associated files into a new model
      # file. Then overwrites the original model file.
      with tempfile.NamedTemporaryFile() as temp:
        new_file = temp.name
      with open(new_file, "wb") as f:
        f.write(model_buf)
      self._copy_archived_files(self._model_file, new_file, packed_files)
      shutil.copy(new_file, self._model_file)
      os.remove(new_file)
    else:
      with open(self._model_file, "wb") as f:
        f.write(model_buf)
Populates the metadata buffer (in bytearray) into the model file.

    Inserts metadata_buf into the metadata field of schema.Model. If the
    MetadataPopulator object is created using the method,
    with_model_file(model_file), the model file will be updated.

    Existing metadata buffer (if applied) will be overridden by the new metadata
    buffer.
    
Populates the metadata buffer (in bytearray) into the model file.
Inserts metadata_buf into the metadata field of schema.Model. If the
MetadataPopulator object is created using the method,
with_model_file(model_file), the model file will be updated.

Existing metadata buffer (if applied) will be overridden by the new metadata
buffer.
Populates the metadata buffer ( in bytearray ) into the model file . Inserts metadata_buf into the metadata field of schema . Model . If the MetadataPopulator object is created using the method with_model_file ( model_file ) the model file will be updated . Existing metadata buffer ( if applied ) will be overridden by the new metadata buffer .
def _populate_metadata_buffer(self):
    with open(self._model_file, "rb") as f:
      model_buf = f.read()
    model = _schema_fb.ModelT.InitFromObj(
        _schema_fb.Model.GetRootAsModel(model_buf, 0))
    buffer_field = _schema_fb.BufferT()
    buffer_field.data = self._metadata_buf
    is_populated = False
    if not model.metadata:
      model.metadata = []
    else:
      for meta in model.metadata:
        if meta.name.decode("utf-8") == self.METADATA_FIELD_NAME:
          is_populated = True
          model.buffers[meta.buffer] = buffer_field
    if not is_populated:
      if not model.buffers:
        model.buffers = []
      model.buffers.append(buffer_field)
      metadata_field = _schema_fb.MetadataT()
      metadata_field.name = self.METADATA_FIELD_NAME
      metadata_field.buffer = len(model.buffers) - 1
      model.metadata.append(metadata_field)
    b = flatbuffers.Builder(0)
    b.Finish(model.Pack(b), self.TFLITE_FILE_IDENTIFIER)
    model_buf = b.Output()
    packed_files = self.get_packed_associated_file_list()
    if packed_files:
      with tempfile.NamedTemporaryFile() as temp:
        new_file = temp.name
      with open(new_file, "wb") as f:
        f.write(model_buf)
      self._copy_archived_files(self._model_file, new_file, packed_files)
      shutil.copy(new_file, self._model_file)
      os.remove(new_file)
    else:
      with open(self._model_file, "wb") as f:
        f.write(model_buf)
def _populate_metadata_buffer ( self ) : with open ( self . _model_file , "rb" ) as f : model_buf = f . read ( ) model = _schema_fb . ModelT . InitFromObj ( _schema_fb . Model . GetRootAsModel ( model_buf , 0 ) ) buffer_field = _schema_fb . BufferT ( ) buffer_field . data = self . _metadata_buf is_populated = False if not model . metadata : model . metadata = [ ] else : for meta in model . metadata : if meta . name . decode ( "utf-8" ) == self . METADATA_FIELD_NAME : is_populated = True model . buffers [ meta . buffer ] = buffer_field if not is_populated : if not model . buffers : model . buffers = [ ] model . buffers . append ( buffer_field ) metadata_field = _schema_fb . MetadataT ( ) metadata_field . name = self . METADATA_FIELD_NAME metadata_field . buffer = len ( model . buffers ) - 1 model . metadata . append ( metadata_field ) b = flatbuffers . Builder ( 0 ) b . Finish ( model . Pack ( b ) , self . TFLITE_FILE_IDENTIFIER ) model_buf = b . Output ( ) packed_files = self . get_packed_associated_file_list ( ) if packed_files : with tempfile . NamedTemporaryFile ( ) as temp : new_file = temp . name with open ( new_file , "wb" ) as f : f . write ( model_buf ) self . _copy_archived_files ( self . _model_file , new_file , packed_files ) shutil . copy ( new_file , self . _model_file ) os . remove ( new_file ) else : with open ( self . _model_file , "wb" ) as f : f . write ( model_buf )
Populates the metadata buffer (in bytearray) into the model file.
Populates the metadata buffer ( in bytearray ) into the model file .
"""Populates the metadata buffer (in bytearray) into the model file.

    Inserts metadata_buf into the metadata field of schema.Model. If the
    MetadataPopulator object is created using the method,
    with_model_file(model_file), the model file will be updated.

    Existing metadata buffer (if applied) will be overridden by the new metadata
    buffer.
    """ # Check if metadata has already been populated. # Creates a new metadata field. # Packs model back to a flatbuffer binaray file. # Saves the updated model buffer to model file. # Gets files that have been packed to self._model_file. # Writes the updated model buffer and associated files into a new model # file. Then overwrites the original model file.
self