2020-06-06 01:49:27 +02:00
|
|
|
---
|
|
|
|
layout: default
|
|
|
|
title: MediaPipe Android Archive
|
2020-12-10 04:13:05 +01:00
|
|
|
parent: MediaPipe on Android
|
|
|
|
grand_parent: Getting Started
|
|
|
|
nav_order: 2
|
2020-06-06 01:49:27 +02:00
|
|
|
---
|
|
|
|
|
|
|
|
# MediaPipe Android Archive
|
|
|
|
{: .no_toc }
|
|
|
|
|
|
|
|
1. TOC
|
|
|
|
{:toc}
|
|
|
|
---
|
2019-10-25 23:12:58 +02:00
|
|
|
|
|
|
|
***Experimental Only***
|
|
|
|
|
2020-05-21 18:46:31 +02:00
|
|
|
The MediaPipe Android Archive (AAR) library is a convenient way to use MediaPipe
|
|
|
|
with Android Studio and Gradle. MediaPipe doesn't publish a general AAR that can
|
|
|
|
be used by all projects. Instead, developers need to add a mediapipe_aar()
|
|
|
|
target to generate a custom AAR file for their own projects. This is necessary
|
|
|
|
in order to include specific resources such as MediaPipe calculators needed for
|
|
|
|
each project.
|
2019-10-25 23:12:58 +02:00
|
|
|
|
2020-06-06 01:49:27 +02:00
|
|
|
## Steps to build a MediaPipe AAR
|
2019-10-25 23:12:58 +02:00
|
|
|
|
|
|
|
1. Create a mediapipe_aar() target.
|
|
|
|
|
|
|
|
In the MediaPipe directory, create a new mediapipe_aar() target in a BUILD
|
|
|
|
file. You need to figure out what calculators are used in the graph and
|
|
|
|
provide the calculator dependencies to the mediapipe_aar(). For example, to
|
2020-06-06 01:49:27 +02:00
|
|
|
build an AAR for [MediaPipe Face Detection](../solutions/face_detection.md),
|
|
|
|
you can put the following code into
|
2019-10-25 23:12:58 +02:00
|
|
|
mediapipe/examples/android/src/java/com/google/mediapipe/apps/aar_example/BUILD.
|
|
|
|
|
|
|
|
```
|
|
|
|
load("//mediapipe/java/com/google/mediapipe:mediapipe_aar.bzl", "mediapipe_aar")
|
|
|
|
|
|
|
|
mediapipe_aar(
|
2021-05-05 03:30:15 +02:00
|
|
|
name = "mediapipe_face_detection",
|
2019-10-25 23:12:58 +02:00
|
|
|
calculators = ["//mediapipe/graphs/face_detection:mobile_calculators"],
|
|
|
|
)
|
|
|
|
```
|
|
|
|
|
|
|
|
2. Run the Bazel build command to generate the AAR.
|
|
|
|
|
|
|
|
```bash
|
2021-05-05 03:30:15 +02:00
|
|
|
bazel build -c opt --strip=ALWAYS \
|
|
|
|
--host_crosstool_top=@bazel_tools//tools/cpp:toolchain \
|
|
|
|
--fat_apk_cpu=arm64-v8a,armeabi-v7a \
|
|
|
|
//path/to/the/aar/build/file:aar_name.aar
|
2019-10-25 23:12:58 +02:00
|
|
|
```
|
|
|
|
|
2021-05-05 03:30:15 +02:00
|
|
|
For the face detection AAR target we made in step 1, run:
|
2019-10-25 23:12:58 +02:00
|
|
|
|
|
|
|
```bash
|
2021-05-05 03:30:15 +02:00
|
|
|
bazel build -c opt --strip=ALWAYS \
|
|
|
|
--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:mediapipe_face_detection.aar
|
2019-10-25 23:12:58 +02:00
|
|
|
|
|
|
|
# It should print:
|
2021-05-05 03:30:15 +02:00
|
|
|
# Target //mediapipe/examples/android/src/java/com/google/mediapipe/apps/aar_example:mediapipe_face_detection.aar up-to-date:
|
|
|
|
# bazel-bin/mediapipe/examples/android/src/java/com/google/mediapipe/apps/aar_example/mediapipe_face_detection.aar
|
2019-10-25 23:12:58 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
3. (Optional) Save the AAR to your preferred location.
|
|
|
|
|
|
|
|
```bash
|
2021-05-05 03:30:15 +02:00
|
|
|
cp bazel-bin/mediapipe/examples/android/src/java/com/google/mediapipe/apps/aar_example/mediapipe_face_detection.aar
|
2019-10-25 23:12:58 +02:00
|
|
|
/absolute/path/to/your/preferred/location
|
|
|
|
```
|
|
|
|
|
2020-06-06 01:49:27 +02:00
|
|
|
## Steps to use a MediaPipe AAR in Android Studio with Gradle
|
2019-10-25 23:12:58 +02:00
|
|
|
|
|
|
|
1. Start Android Studio and go to your project.
|
|
|
|
|
|
|
|
2. Copy the AAR into app/libs.
|
|
|
|
|
|
|
|
```bash
|
2021-05-05 03:30:15 +02:00
|
|
|
cp bazel-bin/mediapipe/examples/android/src/java/com/google/mediapipe/apps/aar_example/mediapipe_face_detection.aar
|
2019-10-25 23:12:58 +02:00
|
|
|
/path/to/your/app/libs/
|
|
|
|
```
|
|
|
|
|
2020-06-06 01:49:27 +02:00
|
|
|
![Screenshot](../images/mobile/aar_location.png)
|
2019-10-25 23:12:58 +02:00
|
|
|
|
|
|
|
3. Make app/src/main/assets and copy assets (graph, model, and etc) into
|
|
|
|
app/src/main/assets.
|
|
|
|
|
|
|
|
Build the MediaPipe binary graph and copy the assets into
|
|
|
|
app/src/main/assets, e.g., for the face detection graph, you need to build
|
|
|
|
and copy
|
2021-02-27 09:21:16 +01:00
|
|
|
[the binary graph](https://github.com/google/mediapipe/blob/master/mediapipe/examples/android/src/java/com/google/mediapipe/apps/facedetectiongpu/BUILD#L41)
|
2019-10-25 23:12:58 +02:00
|
|
|
and
|
2021-02-27 09:21:16 +01:00
|
|
|
[the face detection tflite model](https://github.com/google/mediapipe/tree/master/mediapipe/modules/face_detection/face_detection_front.tflite).
|
2019-10-25 23:12:58 +02:00
|
|
|
|
|
|
|
```bash
|
2021-05-05 03:30:15 +02:00
|
|
|
bazel build -c opt mediapipe/graphs/face_detection:face_detection_mobile_gpu_binary_graph
|
|
|
|
cp bazel-bin/mediapipe/graphs/face_detection/face_detection_mobile_gpu.binarypb /path/to/your/app/src/main/assets/
|
2021-02-27 09:21:16 +01:00
|
|
|
cp mediapipe/modules/face_detection/face_detection_front.tflite /path/to/your/app/src/main/assets/
|
2019-10-25 23:12:58 +02:00
|
|
|
```
|
|
|
|
|
2020-06-06 01:49:27 +02:00
|
|
|
![Screenshot](../images/mobile/assets_location.png)
|
2019-10-25 23:12:58 +02:00
|
|
|
|
2021-05-05 03:30:15 +02:00
|
|
|
4. Modify app/build.gradle to add MediaPipe dependencies and MediaPipe AAR.
|
2019-10-25 23:12:58 +02:00
|
|
|
|
|
|
|
```
|
|
|
|
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'
|
2020-06-08 18:33:26 +02:00
|
|
|
implementation 'com.google.protobuf:protobuf-java:3.11.4'
|
2019-10-25 23:12:58 +02:00
|
|
|
// CameraX core library
|
2020-10-21 05:15:23 +02:00
|
|
|
def camerax_version = "1.0.0-beta10"
|
2019-10-25 23:12:58 +02:00
|
|
|
implementation "androidx.camera:camera-core:$camerax_version"
|
|
|
|
implementation "androidx.camera:camera-camera2:$camerax_version"
|
2020-11-05 01:02:35 +01:00
|
|
|
implementation "androidx.camera:camera-lifecycle:$camerax_version"
|
2021-05-05 03:30:15 +02:00
|
|
|
// AutoValue
|
|
|
|
def auto_value_version = "1.6.4"
|
|
|
|
implementation "com.google.auto.value:auto-value-annotations:$auto_value_version"
|
|
|
|
annotationProcessor "com.google.auto.value:auto-value:$auto_value_version"
|
2019-10-25 23:12:58 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-05-05 03:30:15 +02:00
|
|
|
5. Follow our Android app examples to use MediaPipe in Android Studio for your
|
2020-06-06 01:49:27 +02:00
|
|
|
use case. If you are looking for an example, a face detection example can be
|
|
|
|
found
|
|
|
|
[here](https://github.com/jiuqiant/mediapipe_face_detection_aar_example) and
|
|
|
|
a multi-hand tracking example can be found
|
|
|
|
[here](https://github.com/jiuqiant/mediapipe_multi_hands_tracking_aar_example).
|