mediapipe
mediapipe copied to clipboard
How to convert a tflite file to mediapipe supported task file
This template is for miscellaneous issues not covered by the other issue categories
For questions on how to work with MediaPipe, or support for problems that are not verified bugs in MediaPipe, please go to StackOverflow and Slack communities.
If you are reporting a vulnerability, please use the dedicated reporting process.
Hi I'm trying to convert my tflite model to mediapipe task , can you please help me with this? Thanks
Hi @janaki112,
Please allow us a couple of days to provide an update on this, We will come back with this.
Thank you!!
Sure @kuaashish.
Hi @janaki112,
I apologize for the delayed response. All conversions from TFLite to tasks can be found here: MediaPipe Metadata Writers. Could you please check and let us know if you need any further assistance from us?
Thank you!!
Hi @kuaashish , Yeah I understand but to convert a tflite model to task file , one need to run all the dependent files and most of them are not python files they are in .fb format? can you please help me with this?
I have been trying to create a task file for my custom(Unit8) tflite model but i keep hitting one error below is both the code and the error, i am using Python 3.9.24 on wsl
pip list:
(mp_env) shiv0007@DESKTOP-JL2O30E:/mnt/c/mediapipeapi$ pip list Package Version
absl-py 2.3.1 astunparse 1.6.3 attrs 25.4.0 cachetools 6.2.1 certifi 2025.10.5 cffi 2.0.0 charset-normalizer 3.4.4 contourpy 1.3.0 cycler 0.12.1 flatbuffers 25.9.23 fonttools 4.60.1 gast 0.4.0 google-auth 2.43.0 google-auth-oauthlib 0.4.6 google-pasta 0.2.0 grpcio 1.76.0 h5py 3.14.0 idna 3.11 importlib_metadata 8.7.0 importlib_resources 6.5.2 keras 2.10.0 Keras-Preprocessing 1.1.2 kiwisolver 1.4.7 libclang 18.1.1 Markdown 3.9 MarkupSafe 3.0.3 matplotlib 3.9.4 mediapipe 0.10.5 numpy 1.24.3 oauthlib 3.3.1 opencv-contrib-python 4.7.0.72 opt_einsum 3.4.0 packaging 25.0 pillow 11.3.0 pip 23.0.1 protobuf 3.19.6 pyasn1 0.6.1 pyasn1_modules 0.4.2 pybind11 3.0.1 pycparser 2.23 pyparsing 3.2.5 python-dateutil 2.9.0.post0 requests 2.32.5 requests-oauthlib 2.0.0 rsa 4.9.1 setuptools 79.0.1 six 1.17.0 sounddevice 0.5.3 tensorboard 2.10.1 tensorboard-data-server 0.6.1 tensorboard-plugin-wit 1.8.1 tensorflow 2.10.0 tensorflow-estimator 2.10.0 tensorflow-io-gcs-filesystem 0.37.1 termcolor 3.1.0 tflite-support 0.4.4 typing_extensions 4.15.0 urllib3 2.5.0 Werkzeug 3.1.3 wheel 0.45.1 wrapt 2.0.1 zipp 3.23.0
code: import os os.environ['PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION'] = 'python'
Import mediapipe_model_maker first
from mediapipe_model_maker.python.core.utils import task_model_packager
Import only the specific metadata writer module, avoiding the task imports
import sys sys.path.insert(0, '/mnt/c/Users/BAPS/mediapipe_env/lib/python3.10/site-packages')
Direct import to avoid tflite_support.init which imports task
from tflite_support.metadata_writers import writer_utils from tflite_support.metadata_writers import image_classifier
=== Paths ===
MODEL_TFLITE =r"/mnt/c/mediapipeapi/efnet_b0_uint8.tflite" LABEL_FILE =r"/mnt/c/mediapipeapi/label.txt" OUTPUT_TFLITE =r"/mnt/c/mediapipeapi/eexp_gender_with_metadata.tflite" OUTPUT_TASK =r"/mnt/c/mediapipeapi/eexp_gender.task"
print(" Loading model...") model_buf = writer_utils.load_file(MODEL_TFLITE)
print(" Adding metadata...") writer = image_classifier.MetadataWriter.create_for_inference( model_buf, input_norm_mean=[0.0], input_norm_std=[1.0], label_file_paths=[LABEL_FILE], )
metadata_buf = writer.populate() writer_utils.save_file(metadata_buf, OUTPUT_TFLITE)
print(" Saved TFLite with metadata:", OUTPUT_TFLITE)
print(" Packaging into .task file...") task_model_packager.pack( model_path=OUTPUT_TFLITE, metadata_json_file=None, output_file_path=OUTPUT_TASK )
print(" Successfully created MediaPipe .task file:") print(OUTPUT_TASK)
Error:
(mp_env) shiv0007@DESKTOP-JL2O30E:/mnt/c/mediapipeapi$ python3 linux_test.py File "/mnt/c/mediapipeapi/linux_test.py", line 5 from mediapipe-model-maker.python.core.utils import task_model_packager ^ SyntaxError: invalid syntax
below code created a .task file but the task file did not embed the tflite model into it, it created a empty task file with only label in it, Environment info: pip list:
(mp_env) shiv0007@DESKTOP-JL2O30E:/mnt/c/mediapipeapi$ pip list Package Version
absl-py 2.3.1 astunparse 1.6.3 attrs 25.4.0 cachetools 6.2.1 certifi 2025.10.5 cffi 2.0.0 charset-normalizer 3.4.4 contourpy 1.3.0 cycler 0.12.1 flatbuffers 25.9.23 fonttools 4.60.1 gast 0.4.0 google-auth 2.43.0 google-auth-oauthlib 0.4.6 google-pasta 0.2.0 grpcio 1.76.0 h5py 3.14.0 idna 3.11 importlib_metadata 8.7.0 importlib_resources 6.5.2 keras 2.10.0 Keras-Preprocessing 1.1.2 kiwisolver 1.4.7 libclang 18.1.1 Markdown 3.9 MarkupSafe 3.0.3 matplotlib 3.9.4 mediapipe 0.10.5 numpy 1.24.3 oauthlib 3.3.1 opencv-contrib-python 4.7.0.72 opt_einsum 3.4.0 packaging 25.0 pillow 11.3.0 pip 23.0.1 protobuf 3.19.6 pyasn1 0.6.1 pyasn1_modules 0.4.2 pybind11 3.0.1 pycparser 2.23 pyparsing 3.2.5 python-dateutil 2.9.0.post0 requests 2.32.5 requests-oauthlib 2.0.0 rsa 4.9.1 setuptools 79.0.1 six 1.17.0 sounddevice 0.5.3 tensorboard 2.10.1 tensorboard-data-server 0.6.1 tensorboard-plugin-wit 1.8.1 tensorflow 2.10.0 tensorflow-estimator 2.10.0 tensorflow-io-gcs-filesystem 0.37.1 termcolor 3.1.0 tflite-support 0.4.4 typing_extensions 4.15.0 urllib3 2.5.0 Werkzeug 3.1.3 wheel 0.45.1 wrapt 2.0.1 zipp 3.23.0
Code:
from tflite_support.metadata_writers import image_classifier from tflite_support.metadata_writers import writer_utils
model_path = "/mnt/c/mediapipeapi/efnet_b0_uint8.tflite" with open(model_path, "rb") as f: model_buf = f.read()
labels = ["male", "female"]
label_file = "/mnt/c/mediapipeapi/label.txt"
with open(label_file, "w") as f:
f.write("\n".join(labels))
writer = image_classifier.MetadataWriter.create_for_inference( model_buffer=model_buf, input_norm_mean=[0.0], input_norm_std=[255.0], label_file_paths=[label_file] )
metadata_buf = writer.populate()
from tflite_support.metadata_writers import writer_utils
writer_utils.save_file(metadata_buf, "gender_model_uint8.task")
print("gender_model_uint8.task created successfully!")
Hi @shivpratapsinghpanwar,
Thanks for detailed comment, Give me some time i'll test it from my end and i'll get back to you with updates 🙂
Thanks for detailed comment, Give me some time i'll test it from my end and i'll get back to you with updates 🙂
below is a solution i created to make task files but we need to embed our metadata into the model first.
step1 :
python 3.9 + tflite-support==0.4.4
from tflite_support.metadata_writers import image_classifier from tflite_support.metadata_writers import writer_utils
Paths
MODEL_TFLITE = r"C:\mediapipeapi\exp_gender.tflite" OUTPUT_TASK = r"C:\mediapipeapi\tew3d.task" LABEL_FILE = r"C:\mediapipeapi\label.txt"
Load model bytes
model_buffer = writer_utils.load_file(MODEL_TFLITE)
Create metadata writer for quantized (UINT8) image classifier
writer = image_classifier.MetadataWriter.create_for_inference( model_buffer, input_norm_mean=[], # No normalization for uint8 input_norm_std=[], label_file_paths=[LABEL_FILE] )
This gives you a new model buffer with metadata embedded
metadata_buf = writer.populate()
Save a proper .tflite model with metadata (optional, for inspection)
writer_utils.save_file(metadata_buf, r"C:\mediapipeapi\efnet_b0_uint8_with_metadata.tflite")
Step 2: python 3.10 take the new tflite clone the repo of mediapie go into the cd “mediapipe/tasks/python/metadata/metadata_writers/” and here make a new script and copy the below code and run it
import sys import os
Add this folder (metadata_writers) to sys.path
script_dir = os.path.dirname(os.path.abspath(file)) if script_dir not in sys.path: sys.path.insert(0, script_dir)
Import the bundler directly from local folder
import model_asset_bundle_utils
=== YOUR EXACT PATHS ===
MODEL_PATH = r"C:\Users\BAPS\mediapipe\mediapipe\tasks\python\metadata\metadata_writers\Models_home\2exp_gender_with_metadata.tflite" LABELS_PATH = r"C:\Users\BAPS\mediapipe\mediapipe\tasks\python\metadata\metadata_writers\Models_home\labels.txt" OUTPUT_PATH = r"C:\Users\BAPS\mediapipe\mediapipe\tasks\python\metadata\metadata_writers\Models_home\custom.task"
=========================
Check that files exist
for path, name in [(MODEL_PATH, "MODEL_PATH"), (LABELS_PATH, "LABELS_PATH")]: if not os.path.isfile(path): raise FileNotFoundError(f"{name} not found: {path}")
Read TFLite and labels
with open(MODEL_PATH, "rb") as f: tflite = f.read() with open(LABELS_PATH, "rb") as f: labels = f.read()
Data inside the task bundle
files = { "model.tflite": tflite, "labels.txt": labels, }
Create the .task bundle
model_asset_bundle_utils.create_model_asset_bundle(files, OUTPUT_PATH)
print(" custom.task created successfully!") print(" Location:", OUTPUT_PATH)