最近在做一个和手势识别相关的东西,了解到google的MediaPipe效果不错而且开源,想着学习一下,于是有了下文的安装, MediaPipe官网提供多种操作系统的安装方法,这里我选择ubuntu系统
持续更新中…
$ git clone https://github.com/google/mediapipe.git
# Change directory into MediaPipe root directory
$ cd mediapipe
提供了三种options,选择第一种,安装时可能会网速太慢导致安装失败
Step1: 添加源(一次性操作)
sudo apt install curl
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
Step2:安装更新Bazel
sudo apt update && sudo apt install bazel
sudo apt update && sudo apt full-upgrade
Step3:安装JDK(在Android上运行需要,否则可不安装)
# Ubuntu 16.04 (LTS) uses OpenJDK 8 by default:
sudo apt install openjdk-8-jdk
# Ubuntu 18.04 (LTS) uses OpenJDK 11 by default:
sudo apt install openjdk-11-jdk
$ sudo apt-get install libopencv-core-dev libopencv-highgui-dev \
libopencv-calib3d-dev libopencv-features2d-dev \
libopencv-imgproc-dev libopencv-video-dev
sudo apt-get install mesa-common-dev libegl1-mesa-dev libgles2-mesa-dev
$ export GLOG_logtostderr=1
# if you are running on Linux desktop with CPU only
$ bazel run --define MEDIAPIPE_DISABLE_GPU=1 \
mediapipe/examples/desktop/hello_world:hello_world
# If you are running on Linux desktop with GPU support enabled (via mesa drivers)
$ bazel run --copt -DMESA_EGL_NO_X11_HEADERS --copt -DEGL_NO_X11 \
mediapipe/examples/desktop/hello_world:hello_world
# Should print:
# Hello World!
# Hello World!
# Hello World!
# Hello World!
# Hello World!
# Hello World!
# Hello World!
# Hello World!
# Hello World!
# Hello World!
# 这个也是一次行操作,关闭终端后需重新配置
export ANDROID_HOME=
export ANDROID_NDK_HOME=
例如我的环境变量是
export ANDROID_HOME=/home/zhw/Android/Sdk
export ANDROID_NDK_HOME=/home/zhw/Android/Sdk/ndk/21.1.6352462
#如果想设置全局环境变量
$ sudo vim /etc/profile
#把上面两行export复制到profile最下面,再执行以下操作生效
$ source /etc/profile
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"],
)
bazel build -c opt --host_crosstool_top=@bazel_tools//tools/cpp:toolchain --fat_apk_cpu=arm64-v8a,armeabi-v7a \
//mediapipe/examples/android/src/java/com/google/mediapipe/apps/aar_example:mp_face_detection_aar
# It should print:
# Target //mediapipe/examples/android/src/java/com/google/mediapipe/apps/aar_example:mp_face_detection_aar up-to-date:
# bazel-bin/mediapipe/examples/android/src/java/com/google/mediapipe/apps/aar_example/mp_face_detection_aar.aar
bazel build -c opt mediapipe/examples/android/src/java/com/google/mediapipe/apps/facedetectiongpu:binary_graph
cp bazel-bin/mediapipe/examples/android/src/java/com/google/mediapipe/apps/aar_example/mp_face_detection_aar.aar /path/to/your/app/libs/
cp bazel-bin/mediapipe/examples/android/src/java/com/google/mediapipe/apps/facedetectiongpu/facedetectiongpu.binarypb /path/to/your/app/src/main/assets/
cp mediapipe/models/face_detection_front.tflite /path/to/your/app/src/main/assets/
cp mediapipe/models/face_detection_front_labelmap.txt /path/to/your/app/src/main/assets/
mp_face_detection_aar.aar --------> app/lib/mp_face_detection_aar.aar
facedetectiongpu.binarypb -------->app/src/main/assets/facedetectiongpu.binarypb
face_detection_front.tflite -------->app/src/main/assets/face_detection_front.tflite
face_detection_front_labelmap.txt -------->app/src/main/assets/face_detection_front_labelmap.txt
cp -R ~/Downloads/OpenCV-android-sdk/sdk/native/libs/arm* /path/to/your/app/src/main/jniLibs/
mediapipe/examples/android/src/java/com/google/mediapipe/apps/facedetectioncpu
下的res,AndroidManifest.xml,MainActivity.java
放到项目中,修改AndroidManifest.xml的package="com.google.mediapipe.apps.facedetectioncpu">
为你自己的包,其他报错按提示修改dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
// MediaPipe deps
implementation 'com.google.flogger:flogger:0.3.1'
implementation 'com.google.flogger:flogger-system-backend:0.3.1'
implementation 'com.google.code.findbugs:jsr305:3.0.2'
implementation 'com.google.guava:guava:27.0.1-android'
implementation 'com.google.guava:guava:27.0.1-android'
implementation 'com.google.protobuf:protobuf-java:3.11.4''
// CameraX core library
def camerax_version = "1.0.0-alpha06"
implementation "androidx.camera:camera-core:$camerax_version"
implementation "androidx.camera:camera-camera2:$camerax_version"
}
接着就可以插上你的手机运行下试试吧