我正在使用 MLKit 在 android 上进行人脸检测。我正在关注可以在此处找到的官方文档 - https://developers.google.com/ml-kit/vision/face-detection/android。我可以使用后置摄像头成功检测到人脸,但是当我切换到前置摄像头时没有检测到人脸。根据我从这个论坛的了解,问题可能是旋转补偿。为此,我使用了与文档中所示相同的代码 -
private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
static {
ORIENTATIONS.append(Surface.ROTATION_0, 90);
ORIENTATIONS.append(Surface.ROTATION_90, 0);
ORIENTATIONS.append(Surface.ROTATION_180, 270);
ORIENTATIONS.append(Surface.ROTATION_270, 180);
}
/**
* Get the angle by which an image must be rotated given the device's current
* orientation.
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private int getRotationCompensation(String cameraId, Activity activity, Context context)
throws CameraAccessException {
// Get the device's current rotation relative to its "native" orientation.
// Then, from the ORIENTATIONS table, look up the angle the image must be
// rotated to compensate for the device's rotation.
int deviceRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int rotationCompensation = ORIENTATIONS.get(deviceRotation);
// On most devices, the sensor orientation is 90 degrees, but for some
// devices it is 270 degrees. For devices with a sensor orientation of
// 270, rotate the image an additional 180 ((270 + 270) % 360) degrees.
CameraManager cameraManager = (CameraManager) context.getSystemService(CAMERA_SERVICE);
int sensorOrientation = cameraManager
.getCameraCharacteristics(cameraId)
.get(CameraCharacteristics.SENSOR_ORIENTATION);
rotationCompensation = (rotationCompensation + sensorOrientation + 270) % 360;
return rotationCompensation;
}
为了更改相机,我只更改了一个全局变量 cameraID,代码中没有进行其他更改。
编辑 - 在前置摄像头的情况下,使用旋转 = getRotationCompensation(...) - 180 生成输入图像,如果后置摄像头对我有用,则不减去 180。不知道为什么会这样。我仍然希望对此有任何解释或如何避免这种情况。