From 605afe392c20096f01d311282493798b76da516e Mon Sep 17 00:00:00 2001 From: Cheng Yang Date: Fri, 19 Mar 2021 19:57:25 +0800 Subject: [PATCH] fix posedectioncpu --- .../mediapipe/apps/posetrackingcpu/BUILD | 65 ++++++++++++++++ .../apps/posetrackingcpu/MainActivity.java | 76 +++++++++++++++++++ .../pose_tracking/pose_tracking_cpu.pbtxt | 25 +++++- 3 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 mediapipe/examples/android/src/java/com/google/mediapipe/apps/posetrackingcpu/BUILD create mode 100644 mediapipe/examples/android/src/java/com/google/mediapipe/apps/posetrackingcpu/MainActivity.java diff --git a/mediapipe/examples/android/src/java/com/google/mediapipe/apps/posetrackingcpu/BUILD b/mediapipe/examples/android/src/java/com/google/mediapipe/apps/posetrackingcpu/BUILD new file mode 100644 index 000000000..274097fe4 --- /dev/null +++ b/mediapipe/examples/android/src/java/com/google/mediapipe/apps/posetrackingcpu/BUILD @@ -0,0 +1,65 @@ +# Copyright 2020 The MediaPipe Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +licenses(["notice"]) + +package(default_visibility = ["//visibility:private"]) + +cc_binary( + name = "libmediapipe_jni.so", + linkshared = 1, + linkstatic = 1, + deps = [ + "//mediapipe/graphs/object_detection:mobile_calculators", + "//mediapipe/graphs/pose_tracking:pose_tracking_cpu_deps", + "//mediapipe/java/com/google/mediapipe/framework/jni:mediapipe_framework_jni", + ], +) + +cc_library( + name = "mediapipe_jni_lib", + srcs = [":libmediapipe_jni.so"], + alwayslink = 1, +) + +android_binary( + name = "posetrackingcpu", + srcs = glob(["*.java"]), + assets = [ + "//mediapipe/graphs/pose_tracking:pose_tracking_cpu.binarypb", + "//mediapipe/modules/pose_landmark:pose_landmark_full_body.tflite", + "//mediapipe/modules/pose_detection:pose_detection.tflite", + ], + assets_dir = "", + manifest = "//mediapipe/examples/android/src/java/com/google/mediapipe/apps/basic:AndroidManifest.xml", + manifest_values = { + "applicationId": "com.google.mediapipe.apps.posetrackingcpu", + "appName": "Pose Tracking CPU", + "mainActivity": ".MainActivity", + "cameraFacingFront": "False", + "binaryGraphName": "pose_tracking_cpu.binarypb", + "inputVideoStreamName": "input_video", + "outputVideoStreamName": "output_video", + "flipFramesVertically": "True", + "converterNumBuffers": "2", + }, + multidex = "native", + deps = [ + ":mediapipe_jni_lib", + "//mediapipe/examples/android/src/java/com/google/mediapipe/apps/basic:basic_lib", + "//mediapipe/framework/formats:landmark_java_proto_lite", + "//mediapipe/java/com/google/mediapipe/framework:android_framework", + "@com_google_protobuf//:protobuf_javalite", + ], +) diff --git a/mediapipe/examples/android/src/java/com/google/mediapipe/apps/posetrackingcpu/MainActivity.java b/mediapipe/examples/android/src/java/com/google/mediapipe/apps/posetrackingcpu/MainActivity.java new file mode 100644 index 000000000..1d9d00078 --- /dev/null +++ b/mediapipe/examples/android/src/java/com/google/mediapipe/apps/posetrackingcpu/MainActivity.java @@ -0,0 +1,76 @@ +// Copyright 2020 The MediaPipe Authors. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.mediapipe.apps.posetrackingcpu; + +import android.os.Bundle; +import android.util.Log; + +import com.google.mediapipe.formats.proto.LandmarkProto.NormalizedLandmark; +import com.google.mediapipe.formats.proto.LandmarkProto.NormalizedLandmarkList; +import com.google.mediapipe.framework.PacketGetter; +import com.google.protobuf.InvalidProtocolBufferException; + +/** Main activity of MediaPipe pose tracking app. */ +public class MainActivity extends com.google.mediapipe.apps.basic.MainActivity { + private static final String TAG = "MainActivity"; + + private static final String OUTPUT_LANDMARKS_STREAM_NAME = "pose_landmarks"; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // To show verbose logging, run: + // adb shell setprop log.tag.MainActivity VERBOSE + if (Log.isLoggable(TAG, Log.VERBOSE)) { + processor.addPacketCallback( + OUTPUT_LANDMARKS_STREAM_NAME, + (packet) -> { + Log.v(TAG, "Received pose landmarks packet."); + try { + NormalizedLandmarkList poseLandmarks = + PacketGetter.getProto(packet, NormalizedLandmarkList.class); + Log.v( + TAG, + "[TS:" + + packet.getTimestamp() + + "] " + + getPoseLandmarksDebugString(poseLandmarks)); + } catch (InvalidProtocolBufferException exception) { + Log.e(TAG, "Failed to get proto.", exception); + } + }); + } + } + + private static String getPoseLandmarksDebugString(NormalizedLandmarkList poseLandmarks) { + String poseLandmarkStr = "Pose landmarks: " + poseLandmarks.getLandmarkCount() + "\n"; + int landmarkIndex = 0; + for (NormalizedLandmark landmark : poseLandmarks.getLandmarkList()) { + poseLandmarkStr += + "\tLandmark [" + + landmarkIndex + + "]: (" + + landmark.getX() + + ", " + + landmark.getY() + + ", " + + landmark.getZ() + + ")\n"; + ++landmarkIndex; + } + return poseLandmarkStr; + } +} diff --git a/mediapipe/graphs/pose_tracking/pose_tracking_cpu.pbtxt b/mediapipe/graphs/pose_tracking/pose_tracking_cpu.pbtxt index 441fc67a6..c7adc6a83 100644 --- a/mediapipe/graphs/pose_tracking/pose_tracking_cpu.pbtxt +++ b/mediapipe/graphs/pose_tracking/pose_tracking_cpu.pbtxt @@ -3,6 +3,18 @@ # CPU buffer. (ImageFrame) input_stream: "input_video" + +# Transfers the input image from GPU to CPU memory for the purpose of +# demonstrating a CPU-based pipeline. Note that the input image on GPU has the +# origin defined at the bottom-left corner (OpenGL convention). As a result, +# the transferred image on CPU also shares the same representation. +node: { + calculator: "GpuBufferToImageFrameCalculator" + input_stream: "input_video" + output_stream: "input_video_cpu" +} + + # Output image with rendered results. (ImageFrame) output_stream: "output_video" # Pose landmarks. (NormalizedLandmarkList) @@ -20,7 +32,7 @@ output_stream: "pose_landmarks" # subsequent nodes are still busy processing previous inputs. node { calculator: "FlowLimiterCalculator" - input_stream: "input_video" + input_stream: "input_video_cpu" input_stream: "FINISHED:output_video" input_stream_info: { tag_index: "FINISHED" @@ -68,5 +80,14 @@ node { input_stream: "LANDMARKS:pose_landmarks_smoothed" input_stream: "ROI:roi_from_landmarks" input_stream: "DETECTION:pose_detection" - output_stream: "IMAGE:output_video" + output_stream: "IMAGE:output_video_cpu" +} + + +# Transfers the annotated image from CPU back to GPU memory, to be sent out of +# the graph. +node: { + calculator: "ImageFrameToGpuBufferCalculator" + input_stream: "output_video_cpu" + output_stream: "output_video" }