mediapipe教程4:在安卓上运⾏mediapipe的faceDetection ⼀、前⾔
(1)当前教程的环境是在之前的教程之上;
(2)如mediapipe教程3所说,允许两种⽅式编译安卓程序,⽽mediapipe教程3是⽤cmd编译,本节开始都是在Android Stdio中编译;
(3)⽹上⼤部分⼈都是在windows下使⽤Android Stdio,⽽我是在ubuntu下使⽤,也成功了;
⼆、准备
即在mediapipe的根⽬录打开WORKSPACE,找到android_ndk_repository,并在其内部加⼀⾏“api_level = 21,”,然后后⾯的.aar 和.binarypb需要重新编译;
三、在安卓上运⾏mediapipe的faceDetection
参考⽹址:
实际步骤:
(1)在路径mediapipe/examples/android/src/java/com/google/mediapipe/apps/build_aar_face/下新建
BUILD⽂件,并复制下
⾯内容
load("//mediapipe/java/com/google/mediapipe:mediapipe_aar.bzl", "mediapipe_aar")
mediapipe_aar(
name = "mediapipe_face_detection",
旧念calculators = ["//mediapipe/graphs/face_detection:mobile_calculators"],
)
(2)⽣成安卓aar⽂件(⽣成的⽂件在bazel-
bin/mediapipe/examples/android/src/java/com/google/mediapipe/apps/build_aar_face/⽬录下),命令:
bazel build -c opt --strip=ALWAYS --host_crosstool_top=@bazel_tools//tools/cpp:toolchain --fat_apk_cpu=arm64-v8a,armeabi-v7a mediapipe/examples/an
解释:这⾥步骤1和2放在⼀起说,BUILD⽂件的第⼀⾏⼀般不动,下⾯的name可以改成你任意的名称,但是步骤2命令中最后⼀
个“:”后⾯必须对应这个name;
然后是calculators,你应该打开mediapipe/graphs/face_detection/BUILD⽂件查看算⼦,官⽹的算⼦在BUILD⽂件的cc_library中的name对应;
(3)⽣成Mediapipe的⼆进制图(⽣成⽂件路径为bazel-
bin/mediapipe/graphs/face_detection/face_detection_mobile_gpu.binarypb)
bazel build -c opt mediapipe/graphs/face_detection:face_detection_mobile_gpu_binary_graph
(4)新建Android Stdio⼯程,选择“Empty Activity”,这⾥我的⼯程名改为:aar_face1,复制刚刚编译⽣成的aar⽂件到Android Stdio⼯程的app/libs/⽬录下;
办公室风水刚刚编译⽣成的aar⽂件路径为:bazel-人寰是什么意思
bin/mediapipe/examples/android/src/java/com/google/mediapipe/apps/build_aar_face/mediapipe_face_detection.aar
(5)复制以下⽂件到Android Stdio⼯程的app/src/main/asts/⽬录下
(具体需要复制什么,见mediapipe/examples/android/src/java/com/google/mediapipe/apps/facedetectiongpu/BUILD中的asts)
bazel-bin/mediapipe/graphs/face_detection/face_detection_mobile_gpu.binarypb
mediapipe/modules/face_detection/face_detection_front.tflite
幽默故事大全
(6)打开安卓⼯程的adle,修改其中的内容
dependencies改成:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'straintlayout:constraintlayout:1.1.3'
implementation "le.android.material:material:1.3.0"
testImplementation 'junit:junit:4.12'
androidTestImplementation ':junit:1.1.0'
androidTestImplementation 'st.espresso:espresso-core:3.1.1'
// MediaPipe deps
implementation 'le.flogger:flogger:0.3.1'
implementation 'le.flogger:flogger-system-backend:0.3.1'
电子邮箱英文implementation 'de.findbugs:jsr305:3.0.2'
implementation 'le.guava:guava:27.0.1-android'
implementation 'le.guava:guava:27.0.1-android'
implementation 'le.protobuf:protobuf-java:3.11.4'
/红枣糯米糕
/ CameraX core library
def camerax_version = "1.0.0-beta10"
托运行李英文implementation "androidx.camera:camera-core:$camerax_version"
implementation "androidx.camera:camera-camera2:$camerax_version"
implementation "androidx.camera:camera-lifecycle:$camerax_version"
// AutoValue
def auto_value_version = "1.6.4"
implementation "le.auto.value:auto-value-annotations:$auto_value_version"
annotationProcessor "le.auto.value:auto-value:$auto_value_version"
}
后⾯编译过程中如果出现错误“NDK at /home/lzc/Android/Sdk/ndk-bundle did not have a source.prop
村夜
erties file”,这时需要在adle中android内部加⼀⾏:ndkVersion '22.1.7171670',这⾥后⾯的版本灵活对应你⾃⼰的版本,然后sync;
(7)Android Stdio⼯程其他⽂件
1、l,
2、l,
3、MainActivity.java,
4、其他xml⽂件,如colors\strings\style\themes
l在app/src/main/res/layout,是UI;
MainActivity.java在app/src/main/java/com/example/aar_face1⽬录,是执⾏代码;
其他xml⽂件在app/src/main/res/values中的colors\strings\l,是l和l中⽤到的⼀些东西的定义;
然后注意:
1、l中package后⾯的字符是否正确;
2、MainActivity.java中CameraHelper.CameraFacing.FRONT改成CameraHelper.CameraFacing.BACK(这是因为我的安卓设备不是安卓⼿机,没有前置摄像头,运⾏时候应⽤程序会闪退,Android Stdio中Run中报错说找不到可⽤的camera,⽽usb相机算后视);
3、l中可以修改你的APP的名称;
(8)编译,运⾏
结果:在我的破安卓机顶盒上运⾏流畅的检测⼈脸;