Skip to content

Commit

Permalink
build: .tflite -> .bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Oct 11, 2020
1 parent b0e3edc commit 67f04a7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
4 changes: 2 additions & 2 deletions C/mediapipe_api/BUILD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@rules_pkg//:pkg.bzl", "pkg_zip")
load("//mediapipe_api:import_model.bzl", "pkg_model")

cc_library(
name = "mediapipe_c",
Expand Down Expand Up @@ -84,7 +84,7 @@ cc_library(
alwayslink = True,
)

pkg_zip(
pkg_model(
name = "mediapipe_models",
srcs = [
"@com_google_mediapipe//mediapipe/models:face_detection_front.tflite",
Expand Down
37 changes: 37 additions & 0 deletions C/mediapipe_api/import_model.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
load("@rules_pkg//:pkg.bzl", "pkg_zip")

def pkg_model(name, srcs = [], **kwargs):
"""Package MediaPipe models
This task renames model files so that they can be added to an AssetBundle (e.g. x.tflte -> x.bytes) and zip them.
Args:
name: the name of the output zip file
srcs: files to be packaged
"""

for src in srcs:
_rename_file(src)

pkg_zip(
name = name,
srcs = [_export_file_path(src) for src in srcs],
**kwargs,
)

def _rename_file(src):
export_file = _export_file_path(src)

native.genrule(
name = "export_" + export_file,
srcs = [src],
outs = [export_file],
cmd = "cp $< $@",
)

def _export_file_path(src):
[prefix, base_name] = src.split(":") # src must contain one colon
name_arr = base_name.split(".")
[name, ext] = [base_name, ""] if len(name_arr) == 1 else ["".join(name_arr[:-1]), name_arr[-1]]
export_file_ext = "bytes" if ext == "tflite" else "txt"

return "{}.{}".format(name, export_file_ext)
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ MODE := gpu
builddir := .build
sdkdir := Assets/MediaPipe/SDK
plugindir := $(sdkdir)/Plugins
modeldir := Assets/StreamingAssets
modeldir := $(sdkdir)/Models

bazelflags.default := -c opt
bazelflags.debug := --compilation_mode=dbg
Expand Down

0 comments on commit 67f04a7

Please sign in to comment.