Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 25, 2022 05:34 pm GMT

Mediapipe | Implementing custom feature on Android

project link: https://github.com/google/mediapipe

Structure of Mediapipe project repo

Image description

Compile and try out already implemented example projects on android

  1. build docker image using command docker build . -t mediapipe
  2. run docker container over image docker run -i -t IMAGE_ID /bin/bash
  3. inside the container set up Android using command bash ./setup_android_sdk_and_ndk.sh ~/Android/Sdk ~/Android/Ndk r21
  4. then build android binary using bazel. bazel build -c opt mediapipe/examples/android/src/java/com/google/mediapipe/apps/facedetectioncpu:facedetectioncpu this command is for building facedetectioncpu project. replace project_name (facedetectioncpu here) with the name of the project you want to build. Name of the folders under path mediapipe/mediapipe/examples/android/src/java/com/google/mediapipe/apps/ is name of the inbuild projects.this command would take long for first build. for any subsequent builds, it would use the cached data and shouldn't take long.
  5. apk will be stored at /mediapipe/bazel-bin/mediapipe/examples/android/src/java/com/google/mediapipe/apps/project_name. copy the apk file sudo docker cp CONTAINER_ID:mediapipe/mediapipe/bazel-bin/mediapipe/examples/android/src/java/com/google/mediapipe/apps/project_name /home/username/downloads/.
  6. copy this apk file to your android device and install.
  7. or directly install from terminal using commands adb install project.apk
  8. keep the phone connected and logs could be viewed using adb logcat

about mediapipe structure

  1. Image at the top has general structure of the mediapipe project.
  2. we call bazel build on BUILD file present at mediapipe/examples/android/src/java/com/google/mediapipe/apps/facemeshgpu
  3. This build file refers to graph present at /mediapipe/graphs/facemeshgpu/facemeshgpu.pbtxt
  4. This graph at /mediapipe/graphs/facemeshgpu/facemeshgpu.pbtxt refers to one or more calculators present at 'mediapipe/calculators/folder_name/calculator.cc`
  5. both folders mediapipe/calculators/folder_name/. and mediapipe/graphs/project_name/. has BUILD files that contains information about dependencies.

about graphs

  1. graphs could be visualised here https://viz.mediapipe.dev/
  2. copy paste content from any of your .pbtxt file e.g https://github.com/google/mediapipe/blob/master/mediapipe/graphs/face_detection/face_detection_full_range_mobile_gpu.pbtxt to editor on visualiser. It would show the graph on the left side.
  3. Each box is calculator and has input and output.
  4. This input output relations are specified in .pbtxt graph files.

about calculators

  1. each box in graph visualiser is calculator. (or subgraph made of calculators)
  2. calculators has process function defined that performes calculations. e.g render text on frame or render assets like spectacles at specific position on frame. e.g https://github.com/google/mediapipe/blob/6cdc6443b6a7ed662744e2a2ce2d58d9c83e6d6f/mediapipe/calculators/util/landmark_visibility_calculator.cc#L66
  3. other than process, calculator have getcontract and open function. Open function is called once at the start and process function is called for each input again and again. see below for example.
about open vs process function in calculators

consider we need to display frame count on the camera preview. We start with 0 (for the first frame as the camera starts) and increment count for every subsequent frame. For this, we would define variable count in open function of calculator and assign value 0 to it.
We will add increment logic count = count + 1 in process function and display it on frame.
count will be incremented for every frame because process function will be called for each input, i.e frame. Open is called once at the beginning and value 0 will be assigned to count.

Modify current project

  1. add new calculator in project_folder inside calculator folder
  2. add entry in BUILD file of the project calculator folder
  3. create project folder inside graph folder and create .pbtxt graph for new project.
  4. add entry in BUILD file of project graph folder
  5. modify BUILD mediapipe/examples/android/src/java/com/google/mediapipe/apps/BUILD
  6. compile using command bazel build PATH TO BUILD

Original Link: https://dev.to/janhavidadhania/mediapipe-implementing-custom-feature-on-android-255i

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To