如何正确翻译从前置摄像头检测到的面部坐标

How to properly translate detected face coordinates from front camera

我在 Android 上使用 Google Vision 进行人脸检测。目前我的代码:

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; 
}

问题是xy不正确,有时甚至是负数。我知道要获得正确的坐标,应该以某种方式旋转它,但具体如何旋转?

如果面部超出图像左边缘的顶部 and/or,则这些坐标可以为负值。即使头部可能不完全在照片内,面部检测器也会根据可见的内容估计超出图像边界的面部边界框。

相对于图像的坐标应该是正确的。但是,如果您在 front-facing 相机的预览上绘图,请注意此预览显示为反转(如镜像)。在这种情况下,您需要反转坐标才能在预览中绘制。请在此处查看如何完成此操作的示例:

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