2017-02-10 94 views
0

我在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是不正确的,甚至有时负。我知道为了得到正确的坐标,它应该以某种方式旋转,但究竟是如何?

+0

http://stackoverflow.com/questions/39281320/how-to-detect-the-corners-center-xy-coordinates-using-googles- face-api这是有道理的。 – bvk256

回答

1

如果脸部延伸超出图像的顶部和/或左侧边缘,则这些坐标可能为负值。即使头部可能不完全位于照片中,但人脸检测器将根据可见的情况来估计超出图像边界的面部边界框。

坐标应该相对于图像是正确的。但是,如果您正在从前置摄像头预览,请注意,此预览显示为反转(如镜像)。在这种情况下,您需要反转坐标才能绘制预览图。看到这是如何在这里做一个例子:

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

相关问题