执行 setup_android_sdk_and_ndk.sh,安装andorid sdk, ndk,并做好环境变量的配置。
#cd mediapipe 根目录
#sudo apt install curl openjdk-11-jdk g++ unzip zip
#chmod +x ./setup_android_skd_and_ndk.sh
安装命令在setup_android_skd_and_ndk.sh文件里有说明。
老版本是:
#bash ./setup_android_sdk_and_ndk.sh ~/Android/Sdk ~/Android/Ndk r19c
新版是:
#bash ./setup_android_sdk_and_ndk.sh ~/Android/Sdk ~/Android/Ndk r21
#vim ~/.bashrc 最后添加:.bashrc 和 .profile都是环境变量
export ANDROID_HOME=$PATH:/root/Android/Sdk
export ANDROID_NDK_HOME=$PATH:/root/Android/Ndk/android-ndk-r21
#source ~/.bashrc
必须执行了第一步才出现下面的配置项。
#vi /home/medipapi/WORKSPACE
1.修改SDK和NDK的路径
# You may run setup_android.sh to install Android SDK and NDK.
android_ndk_repository(
name = "androidndk",
path = "/root/Android/Ndk/android-ndk-r21",
)
android_sdk_repository(
name = "androidsdk",
path = "/root/Android/Sdk",
)
# iOS basic build deps.
2.修改maven的下载仓库,repositories 替换下面的内容,google自己带的国内没法用:
repositories = [
"https://maven.google.com",
"https://dl.google.com/dl/android/maven2",
"https://repo1.maven.org/maven2",
"https://jcenter.bintray.com",
],
替换为
repositories =
[
"https://maven.aliyun.com/repository/google",
"https://maven.aliyun.com/repository/central",
"https://maven.aliyun.com/repository/public",
],
在gradle中加入aliyun
#vi /home/mediapipe/mediapipe/examples/android/solutions/build.gradle
allprojects {
repositories {
maven { url 'https://maven.aliyun.com/repository/google' }
maven { url 'https://maven.aliyun.com/repository/central' }
maven { url 'http://maven.aliyun.com/repository/public' }
}
}
使用Opencv-3.4.3镜像地址sourceforge,github的地址都太慢。使用github文件加速https://gh.api.99988866.xyz/。在链接里面输入zip文件的下载链接,它会生成一个新的下载链接。
http_archive(
name = "android_opencv",
build_file = "@//third_party:opencv_android.BUILD",
strip_prefix = "OpenCV-android-sdk",
type = "zip",
# url = "https://github.com/opencv/opencv/releases/download/3.4.3/opencv-3.4.3-android-sdk.zip",
url="https://gh.api.99988866.xyz/https://github.com/opencv/opencv/releases/download/3.4.3/opencv-3.4.3-android-sdk.zip",
)
#sudo apt-get install python3-setuptools
执行
#python setup.py install
这里依据你的andorid sdk安装目录做调整,我是用sudo 安装的,默认安装到了root账号下,
以上环境配置好,可以尝试编译,android aar包,
首先,mediapipe\mediapipe\examples\android\src\java\com\google\mediapipe\apps目录下,创建build_aar文件夹,里面新建文件BUILD,文件内容:
load("//mediapipe/java/com/google/mediapipe:mediapipe_aar.bzl", "mediapipe_aar")
mediapipe_aar(
name = "mediapipe_hand_tracking",
calculators = ["//mediapipe/graphs/hand_tracking:mobile_calculators"],
)
#load("//mediapipe/java/com/google/mediapipe:mediapipe_aar.bzl", "mediapipe_aar")
#
#mediapipe_aar(
# name = "mediapipe_instant_motion_tracking",
# calculators = ["//mediapipe/graphs/instant_motion_tracking:instant_motion_tracking_deps"],
#)
这里是编手势检测的aar包,使用的模型是mediapipe\mediapipe\graphs\hand_tracking,
如果要编译别的,参考mediapipe\mediapipe\graphs\下的模型,修改BUILD文件,
然后,执行命令:
#bazel build -c opt --strip=ALWAYS --host_crosstool_top=@bazel_tools//tools/cpp:toolchain --fat_apk_cpu=arm64-v8a,armeabi-v7a --action_env PYTHON_BIN_PATH="usr/bin/python3" mediapipe/examples/android/src/java/com/google/mediapipe/apps/build_aar:mediapipe_hand_tracking.aar
需要提示的是,这里要指定python的路径, --action_env PYTHON_BIN_PATH=“usr/bin/python3” 不然编译会报Repository command failed。
编译成功,你将看到如下输出信息:
#./build_android_examples.sh -d out_dir --nostrip
省略到-d生成的APK将在当前目录。
打包handtrackinggpu
#bazel build -c opt --config=android_arm64 mediapipe/examples/android/src/java/com/google/mediapipe/apps/handtrackinggpu:handtrackinggpu
打包posetrackinggpu
#bazel build -c opt --config=android_arm64 mediapipe/examples/android/src/java/com/google/mediapipe/apps/posetrackinggpu:posetrackinggpu