前面的文章已经把Mediapipe的环境配置好了,已经可以编译成apk文件了
接下来我们介绍下如何供android使用的aar文件
在编译aar之前我先介绍下mediapipe提供了哪些可以编译的aar工程
上面的这个截图对应的目录是\rootfs\home\tian\mediapipe\mediapipe\examples\android\src\java\com\google\mediapipe\apps
basic:基础工程,下面的其他工程对它有依赖
具体什么含义,可以自己英文查询下
我用编译facedetectiongpu为例子
1、在\rootfs\home\tian\mediapipe\mediapipe\examples\android\src\java\com\google\mediapipe\apps
下创建aar_example文件
然后在aar_example,这里面新建文件BUILD.txt,然后把txt去掉,把下面内容复制进去
load("//mediapipe/java/com/google/mediapipe:mediapipe_aar.bzl", "mediapipe_aar") mediapipe_aar( name = "mp_face_detection_aar", calculators = ["//mediapipe/graphs/face_detection:mobile_calculators"], ) |
注意:
load("//mediapipe/java/com/google/mediapipe:mediapipe_aar.bzl", "mediapipe_aar")
是固定的,这个是编译aar的固定编译配置
name:这个是随意的,就是你编译完成后aar的名称
calculators :这个是具体编译aar用到的具体的graphs图资源,路径在/mediapipe/graphs/下面
需要什么,就配置什么,
我们打开看下face_detection中的BUILD看下
# Copyright 2019 The MediaPipe Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. licenses(["notice"]) package(default_visibility = ["//visibility:public"]) cc_library( name = "mobile_calculators", deps = [ "//mediapipe/calculators/core:flow_limiter_calculator", "//mediapipe/calculators/util:annotation_overlay_calculator", "//mediapipe/calculators/util:detections_to_render_data_calculator", "//mediapipe/gpu:gpu_buffer_to_image_frame_calculator", "//mediapipe/gpu:image_frame_to_gpu_buffer_calculator", "//mediapipe/modules/face_detection:face_detection_front_cpu", "//mediapipe/modules/face_detection:face_detection_front_gpu", ], ) cc_library( name = "desktop_live_calculators", deps = [ "//mediapipe/calculators/core:flow_limiter_calculator", "//mediapipe/calculators/util:annotation_overlay_calculator", "//mediapipe/calculators/util:detections_to_render_data_calculator", "//mediapipe/modules/face_detection:face_detection_front_cpu", ], ) cc_library( name = "desktop_live_gpu_calculators", deps = [ "//mediapipe/calculators/core:flow_limiter_calculator", "//mediapipe/calculators/util:annotation_overlay_calculator", "//mediapipe/calculators/util:detections_to_render_data_calculator", "//mediapipe/modules/face_detection:face_detection_front_gpu", ], ) load( "//mediapipe/framework/tool:mediapipe_graph.bzl", "mediapipe_binary_graph", ) mediapipe_binary_graph( name = "face_detection_mobile_cpu_binary_graph", graph = "face_detection_mobile_cpu.pbtxt", output_name = "face_detection_mobile_cpu.binarypb", deps = [":mobile_calculators"], ) mediapipe_binary_graph( name = "face_detection_mobile_gpu_binary_graph", graph = "face_detection_mobile_gpu.pbtxt", output_name = "face_detection_mobile_gpu.binarypb", deps = [":mobile_calculators"], ) |
看到这个我们怎么选择具体的什么name?
这个在官方文档上没有写,我是直接看官方的app里的build是怎么写看下
apps/facedetectiongpu/BUILD
# Copyright 2019 The MediaPipe Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. licenses(["notice"]) package(default_visibility = ["//visibility:private"]) cc_binary( name = "libmediapipe_jni.so", linkshared = 1, linkstatic = 1, deps = [ "//mediapipe/graphs/face_detection:mobile_calculators", "//mediapipe/java/com/google/mediapipe/framework/jni:mediapipe_framework_jni", ], ) cc_library( name = "mediapipe_jni_lib", srcs = [":libmediapipe_jni.so"], alwayslink = 1, ) android_binary( name = "facedetectiongpu", srcs = glob(["*.java"]), assets = [ "//mediapipe/graphs/face_detection:face_detection_mobile_gpu.binarypb", "//mediapipe/modules/face_detection:face_detection_front.tflite", ], assets_dir = "", manifest = "//mediapipe/examples/android/src/java/com/google/mediapipe/apps/basic:AndroidManifest.xml", manifest_values = { "applicationId": "com.google.mediapipe.apps.facedetectiongpu", "appName": "Face Detection", "mainActivity": "com.google.mediapipe.apps.basic.MainActivity", "cameraFacingFront": "True", "binaryGraphName": "face_detection_mobile_gpu.binarypb", "inputVideoStreamName": "input_video", "outputVideoStreamName": "output_video", "flipFramesVertically": "True", "converterNumBuffers": "2", }, multidex = "native", deps = [ ":mediapipe_jni_lib", "//mediapipe/examples/android/src/java/com/google/mediapipe/apps/basic:basic_lib", ], ) |
cc_binary( name = "libmediapipe_jni.so", linkshared = 1, linkstatic = 1, deps = [ "//mediapipe/graphs/face_detection:mobile_calculators", "//mediapipe/java/com/google/mediapipe/framework/jni:mediapipe_framework_jni", ], ) |
有上面那个mobile_calculators,这个是就要编译的具体的图配置,
接下来我们开始执行cmd命令
//给aar_example赋读写权限
1、sudo chmod -R 755 /mediapipe/examples/android/src/java/com/google/mediapipe/apps/aar_example //后面就是具体的aar_example 目录了,根据自己情况下
//下面这个指令就是编译aar文件了,会等很久,估计20分钟-40分钟,根据自己电脑配置决定
2、bazel build -c opt --fat_apk_cpu=arm64-v8a,armeabi-v7a //mediapipe/examples/android/src/java/com/google/mediapipe/apps/aar_example:mp_face_detection_aar
这个中间可能会出现很多的TimeOut的错误,因为编译过程会下载很多国外的资源,所以很慢或者超时了,这个就自己看有没有梯子或者切换个网络试试,我也是弄了很久,才好的
编译过程漫长,耐心等待,遇到问题仔细分析,一般都会编译成功
如果看到这个就代表成功了
去什么地方找这个aar呢,我之前用官方的方式没有找到
cp bazel-bin/mediapipe/examples/android/src/java/com/google/mediapipe/apps/aar_example/mp_face_detection_aar.aar /absolute/path/to/your/preferred/location
这个是官方介绍的,但是bazel-bin/mediapipe/examples/android/src/java/com/google/mediapipe/apps/aar_example/mp_face_detection_aar.aar
这个目录根本没有,执行这个命令会包,找不到文件的错误
最好我发现在这里
C:\Users\Mr.tian\AppData\Local\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs\home\tian\.cache\bazel\_bazel_tian\7fe30ee7bfd1145e7a3fb5f9113d3a01\execroot\mediapipe\bazel-out\k8-opt\bin\mediapipe\examples\android\src\java\com\google\mediapipe\apps\
这个根据自己的目录在找,或者直接在\rootfs\home\tian搜索你要的aar文件
祝你好运~