实现背景替换功能,目前搜到的MediaPipe解决方案都不是GPU方式(非GPU背景替换时的性能参数CPU使用20%左右、内存120M左右、FPS20左右、Detect time:30-50ms),下边是编译GPU方式实现的AAR,为了方便编译,我这边使用windows下的子系统wsl。
1,安装 MediaPipe(detail following these
instructions
. )
我这边使用的是Installing on Windows Subsystem for Linux (WSL)
wsl下,
安装一些必要的包
lxs@EM-20220126CUMO:/mnt/e/SubSys$ sudo apt-get update && sudo apt-get install -y build-essential git python zip adb openjdk-8-jdk
安装bazelisk(编译工具)
lxs@EM-20220126CUMO:/mnt/e/SubSys$ sudo npm install -g @bazel/bazelisk
拉取MediaPipe git仓库代码
lxs@EM-20220126CUMO:/mnt/e/SubSys$ git clone https://github.com/google/mediapipe.git
lxs@EM-20220126CUMO:/mnt/e/SubSys$ cd mediapipe
安装OpenCV and FFmpeg
lxs@EM-20220126CUMO:/mnt/e/SubSys/mediapipe$ sudo apt-get install libopencv-core-dev libopencv-highgui-dev \
libopencv-calib3d-dev libopencv-features2d-dev \
libopencv-imgproc-dev libopencv-video-dev
2,设置Android SDK release 30.0.0和 Android NDK version between 18 and 21.
我是直接执行的mediapipe脚本setup_android_sdk_and_ndk.sh
lxs@EM-20220126CUMO:/mnt/e/SubSys/mediapipe$ ./setup_android_sdk_and_ndk.sh
下载的最新的是安卓SDK是31,结果报错找不到文件,所以只能命令行下载SDK30
1,进入sdk目录下的tools/bin/目录下,找到sdkmanager
2,下载
1,下载build-tools
./sdkmanager “build-tools;30.0.2”
2,下载platforms
./sdkmanager “platforms;android-30”
打开mediapipe根目录下WORKSPACE文件最后几行,修改下面斜体部分31为30
…
load(“@libedgetpu//:workspace.bzl”, “libedgetpu_dependencies”)
libedgetpu_dependencies()load(“@coral_crosstool//:configure.bzl”, “cc_crosstool”)
cc_crosstool(name = “crosstool”)
android_sdk_repository(name = “androidsdk”, api_level = 30, build_tools_version = “30.0.0”,path = “/home/lxs/Android/Sdk”)
android_ndk_repository(name = “androidndk”, api_level=21, path = “/home/lxs/Android/Sdk/ndk-bundle/android-ndk-r21”)
3,添加编译脚本,并编译
在/mediapipe/examples/android/src/java/com/google/mediapipe/apps/下创建build_aar文件夹,新建一个BUILD文件,打开编辑以下内容并保存:
##添加如下内容,这是编译selfie_segmentation的aar包,使用的模型为//mediapipe/graphs/selfie_segmentation:selfie_segmentation_gpu_deps
load(“//mediapipe/java/com/google/mediapipe:mediapipe_aar.bzl”, “mediapipe_aar”)mediapipe_aar(
name = “mediapipe_selfie_segmentation”,
calculators = [“//mediapipe/graphs/selfie_segmentation:selfie_segmentation_gpu_deps”],
)
然后执行如下编译命令:
/mnt/e/SubSys/mediapipe$ bazel build -c opt --fat_apk_cpu=arm64-v8a,armeabi-v7a //mediapipe/examples/android/src/java/com/google/mediapipe/apps/build_aar:mediapipe_selfie_segmentation.aar
等待时间比较久,几十分钟后,可以看到如下图表示编译OK。
4,使用demo代码
https://gitee.com/liuxiaoshuan/media-pipe-segment-demo