Skip to content

Instantly share code, notes, and snippets.

@codeperfectplus
Created March 22, 2021 08:59
Show Gist options
  • Save codeperfectplus/9dc61a6dca4400adb54de94d04ad7dc9 to your computer and use it in GitHub Desktop.
Save codeperfectplus/9dc61a6dca4400adb54de94d04ad7dc9 to your computer and use it in GitHub Desktop.
Meta Data Writer
''' Writing MetaData to TfLite Model
save it as MetaDataWriterFile.py'''
from tflite_support.metadata_writers import object_detector
from tflite_support.metadata_writers import writer_utils
from tflite_support import metadata
ObjectDetectorWriter = object_detector.MetadataWriter
_MODEL_PATH = <tf_lite_model_path>
_LABEL_FILE = <label_path>
_SAVE_TO_PATH = <path_to_tflite_path/tflite_with_metadata.tflite>
writer = ObjectDetectorWriter.create_for_inference(
writer_utils.load_file(_MODEL_PATH), [127.5], [127.5], [_LABEL_FILE])
writer_utils.save_file(writer.populate(), _SAVE_TO_PATH)
# Verify the populated metadata and associated files.
displayer = metadata.MetadataDisplayer.with_model_file(_SAVE_TO_PATH)
print("Metadata populated:")
print(displayer.get_metadata_json())
print("Associated file(s) populated:")
print(displayer.get_packed_associated_file_list())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment