2

I'm using Google Vision for face detection on Android. Currently my code:

public void onPreviewFrame(byte[] data, Camera camera) {

        // creating Google Vision frame from a camera frame for face recognition
        com.google.android.gms.vision.Frame frame = new com.google.android.gms.vision.Frame.Builder()
                .setImageData(ByteBuffer.wrap(data), previewWidth,
                        previewHeight, ImageFormat.NV21)
                .setId(frameId++)
                .setRotation(com.google.android.gms.vision.Frame.ROTATION_270)
                .setTimestampMillis(lastTimestamp).build();

        // recognize the face in the frame
        SparseArray<Face> faces = detector.detect(frame);

        // wrong coordinates
        float x = faces.valueAt(0).getPosition().x; 
        float y = faces.valueAt(0).getPosition().y; 
}

The problem is that x and y are not correct and even negative sometimes. I know that to get correct coordinates it should be rotated somehow, but how exactly?

bvk256
  • 1,837
  • 3
  • 20
  • 38
  • http://stackoverflow.com/questions/39281320/how-to-detect-the-corners-center-x-y-coordinates-using-googles-face-api This makes sense. – bvk256 Feb 13 '17 at 13:36

1 Answers1

3

These coordinates can be negative if the face extends beyond the top and/or the left edges of the image. Even though the head may not be entirely within the photo, the face detector will estimate the bounding box of the face beyond the image bounds based upon what is visible.

The coordinates should be correct relative to the image. However, if you are drawing on a preview from the front-facing camera, note that this preview is displayed reversed (like a mirror image). In this case, you'd need to reverse the coordinates on order to draw on the preview. See an example of how this is done here:

https://github.com/googlesamples/android-vision/blob/master/visionSamples/FaceTracker/app/src/main/java/com/google/android/gms/samples/vision/face/facetracker/ui/camera/GraphicOverlay.java#L101

pm0733464
  • 2,862
  • 14
  • 16