【发布时间】:2020-11-12 23:16:03
【问题描述】:
我正在使用 MLKit 在 android 上进行人脸检测。我正在关注可以在此处找到的官方文档 - https://developers.google.com/ml-kit/vision/face-detection/android。我可以使用后置摄像头成功检测到人脸,但是当我切换到前置摄像头时没有检测到人脸。根据我从this论坛了解到的,问题可能是旋转补偿。为此,我使用了与文档中所示相同的代码-
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。不知道为什么会这样。我仍然希望对此或如何避免这种情况的任何解释。
【问题讨论】:
-
您使用的是 CameraX 还是 Camera2?
-
我正在使用 Camera2
标签: android face-detection google-mlkit