Mediapipe: How to remove mediapipe tracking in camera preview

Created on 25 Jul 2020  路  5Comments  路  Source: google/mediapipe

I want to remove these tracking visuals on hand but getting the position of the landmarks at the same time.

Screenshot_2020-07-22-16-22-30-542_com example myfacedetectionapp

Here is the full code of inactivity. How can I remove these tracking and show only a simple preview?

````
public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

private static final String BINARY_GRAPH_NAME = "hand_tracking_mobile_gpu.binarypb";
private static final String INPUT_VIDEO_STREAM_NAME = "input_video";
private static final String OUTPUT_VIDEO_STREAM_NAME = "output_video";
private static final CameraHelper.CameraFacing CAMERA_FACING = CameraHelper.CameraFacing.FRONT;

private static final String OUTPUT_HAND_PRESENCE_STREAM_NAME = "hand_presence";
private static final String OUTPUT_LANDMARKS_STREAM_NAME = "hand_landmarks";
DisplayMetrics metrics = new DisplayMetrics();
public Executor executor = Executors.newSingleThreadExecutor();
ImageCapture imageCapture;
ImageAnalysis imageAnalysis;
Preview preview;


// Flips the camera-preview frames vertically before sending them into FrameProcessor to be
// processed in a MediaPipe graph, and flips the processed frames back when they are displayed.
// This is needed because OpenGL represents images assuming the image origin is at the bottom-left
// corner, whereas MediaPipe in general assumes the image origin is at top-left.
private static final boolean FLIP_FRAMES_VERTICALLY = true;

static {
    // Load all native libraries needed by the app.
    System.loadLibrary("mediapipe_jni");
    System.loadLibrary("opencv_java3");
}

// {@link SurfaceTexture} where the camera-preview frames can be accessed.
private SurfaceTexture previewFrameTexture;
// {@link SurfaceView} that displays the camera-preview frames processed by a MediaPipe graph.
private SurfaceView previewDisplayView;

// Creates and manages an {@link EGLContext}.
private EglManager eglManager;
// Sends camera-preview frames into a MediaPipe graph for processing, and displays the processed
// frames onto a {@link Surface}.
private FrameProcessor processor;
// Converts the GL_TEXTURE_EXTERNAL_OES texture from Android camera into a regular texture to be
// consumed by {@link FrameProcessor} and the underlying MediaPipe graph.
private ExternalTextureConverter converter;

// Handles camera access via the {@link CameraX} Jetpack support library.
private CameraXPreviewHelper cameraHelper;
TextureView textureView;
private CameraX.LensFacing lensFacing = CameraX.LensFacing.FRONT;
Rational aspectRatio;
Size screen;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    previewDisplayView = new SurfaceView(this);
    setupPreviewDisplayView();

    // Initialize asset manager so that MediaPipe native libraries can access the app assets, e.g.,
    // binary graphs.
    AndroidAssetUtil.initializeNativeAssetManager(this);

    eglManager = new EglManager(null);
    processor =
            new FrameProcessor(
                    this,
                    eglManager.getNativeContext(),
                    BINARY_GRAPH_NAME,
                    INPUT_VIDEO_STREAM_NAME,
                    OUTPUT_VIDEO_STREAM_NAME);
    processor.getVideoSurfaceOutput().setFlipY(FLIP_FRAMES_VERTICALLY);

    Log.d(TAG, "onCreate: ");
    processor.addPacketCallback(
            OUTPUT_HAND_PRESENCE_STREAM_NAME,
            (packet) -> {
                Boolean handPresence = PacketGetter.getBool(packet);
                if (!handPresence) {
                    Log.d(
                            TAG,
                            "[TS:" + packet.getTimestamp() + "] Hand presence is false, no hands detected.");
                }
            });

    processor.addPacketCallback(
            OUTPUT_LANDMARKS_STREAM_NAME,
            (packet) -> {
                byte[] landmarksRaw = PacketGetter.getProtoBytes(packet);
                try {
                    Log.d(TAG, "onCreate: inside");
                    LandmarkProto.NormalizedLandmarkList landmarks = LandmarkProto.NormalizedLandmarkList.parseFrom(landmarksRaw);
                    if (landmarks == null) {
                        Log.d(TAG, "[TS:" + packet.getTimestamp() + "] No hand landmarks.");
                        return;
                    }
                    // Note: If hand_presence is false, these landmarks are useless.
                    Log.d(
                            TAG,
                            "[TS:"
                                    + packet.getTimestamp()
                                    + "] #Landmarks for hand: "
                                    + landmarks.getLandmarkCount());
                    Log.d(TAG, getLandmarksDebugString(landmarks));
                } catch (InvalidProtocolBufferException e) {
                    Log.e(TAG, "Couldn't Exception received - " + e);
                    return;
                }
            });

    PermissionHelper.checkAndRequestCameraPermissions(this);
}

@Override
protected void onResume() {
    super.onResume();
    converter = new ExternalTextureConverter(eglManager.getContext());
    converter.setFlipY(FLIP_FRAMES_VERTICALLY);
    converter.setConsumer(processor);
    if (PermissionHelper.cameraPermissionsGranted(this)) {
        startCamera();
    }
}

@Override
protected void onPause() {
    super.onPause();
    converter.close();
}

@Override
public void onRequestPermissionsResult(
        int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    PermissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
}


private void setupPreviewDisplayView() {
    previewDisplayView.setVisibility(View.VISIBLE);
    ViewGroup viewGroup = findViewById(R.id.preview_display_layout);
    viewGroup.addView(previewDisplayView);

    previewDisplayView
            .getHolder()
            .addCallback(
                    new SurfaceHolder.Callback() {
                        @Override
                        public void surfaceCreated(SurfaceHolder holder) {
                            Log.d(TAG, "surfaceCreated: ");
                            processor.getVideoSurfaceOutput().setSurface(holder.getSurface());
                        }

                        @Override
                        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                            // (Re-)Compute the ideal size of the camera-preview display (the area that the
                            // camera-preview frames get rendered onto, potentially with scaling and rotation)
                            // based on the size of the SurfaceView that contains the display.
                            Log.d(TAG, "surfaceChanged: ");
                            Size viewSize = new Size(width, height);
                            Size displaySize = cameraHelper.computeDisplaySizeFromViewSize(viewSize);

                            // Connect the converter to the camera-preview frames as its input (via
                            // previewFrameTexture), and configure the output width and height as the computed
                            // display size.
                            converter.setSurfaceTextureAndAttachToGLContext(
                                    previewFrameTexture, displaySize.getWidth(), displaySize.getHeight());
                        }

                        @Override
                        public void surfaceDestroyed(SurfaceHolder holder) {
                            Log.d(TAG, "surfaceDestroyed: ");
                            processor.getVideoSurfaceOutput().setSurface(null);
                        }
                    });
}

private void startCamera() {
    cameraHelper = new CameraXPreviewHelper();
    cameraHelper.setOnCameraStartedListener(
            surfaceTexture -> {

// added
// To update the SurfaceTexture, we have to remove it and re-add it
ViewGroup viewGroup = (ViewGroup)previewDisplayView.getParent();
viewGroup.removeView(previewDisplayView);
viewGroup.addView(previewDisplayView);
// added
previewFrameTexture = surfaceTexture;
// Make the display view visible to start showing the preview. This triggers the
// SurfaceHolder.Callback added to (the holder of) previewDisplayView.
previewDisplayView.setVisibility(View.VISIBLE);
});
cameraHelper.startCamera(this, CAMERA_FACING, /surfaceTexture=/ null);
}

private static String getLandmarksDebugString(LandmarkProto.NormalizedLandmarkList landmarks) {
    int landmarkIndex = 0;
    String landmarksString = "";
    for (LandmarkProto.NormalizedLandmark landmark : landmarks.getLandmarkList()) {
        landmarksString +=
                "\t\tLandmark["
                        + landmarkIndex
                        + "]: ("
                        + landmark.getX()
                        + ", "
                        + landmark.getY()
                        + ", "
                        + landmark.getZ()
                        + ")\n";
        ++landmarkIndex;
    }
    return landmarksString;
}

}
```

android

Most helpful comment

I've found the solution..
.
Open--->renderer_gpu.pbtxt //OPEN FILE FROM mediapipe/mediapipe/graphs/hand_tracking/subgraphs

Find and make comment these line from file renderer_gpu.pbtxt

input_stream: "detection_render_data"
input_stream: "landmark_render_data"
input_stream: "rect_render_data"

then save the file and run

All 5 comments

In the corresponding MediaPipe graph, you can comment out the calculators that add the annotation overlay https://github.com/google/mediapipe/blob/master/mediapipe/graphs/hand_tracking/subgraphs/multi_hand_renderer_gpu.pbtxt for a quick solution.

@eknight7 Still not able to remove tracking.
I'm using this binarypb graph and using renderer_gpu subgraph. I tried to uncomment many things but still not able to resolve. Please provide me the best solution for it.

@shivammaindola @eknight7 I have same problem.
please provide me the solution for it.

@yeon1216 I've found the solution. You just have to comment out the 4 things inside annotationoverlaycalculator which is present inside the renderersubgraph.

I've found the solution..
.
Open--->renderer_gpu.pbtxt //OPEN FILE FROM mediapipe/mediapipe/graphs/hand_tracking/subgraphs

Find and make comment these line from file renderer_gpu.pbtxt

input_stream: "detection_render_data"
input_stream: "landmark_render_data"
input_stream: "rect_render_data"

then save the file and run

Was this page helpful?
0 / 5 - 0 ratings