1

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

问题是,x有时y是不正确的,甚至是消极的。我知道要获得正确的坐标,它应该以某种方式旋转,但究竟如何呢?

4

1 回答 1

2

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

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

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

于 2017-02-13T15:52:18.567 回答