目前我正在尝试将Camera2.Face转换为实际视图的矩形,以便在Camera2 API检测到的脸部上绘制圆圈。
我可以通过以下代码将面孔及其数据的数量转换为Callback:
private CameraCaptureSession.CaptureCallback mCaptureCallback
= new CameraCaptureSession.CaptureCallback() {
private void process(CaptureResult result) {
Integer mode = result.get(CaptureResult.STATISTICS_FACE_DETECT_MODE);
Face [] faces = result.get(CaptureResult.STATISTICS_FACES);
if(faces != null && mode != null)
Log.e("tag", "faces : " + faces.length + " , mode : " + mode );
}
@Override
public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request, CaptureResult partialResult) {
process(partialResult);
}
@Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
process(result);
}
}
到目前为止,我尝试使用下面的代码将Face rect转换为实际的视图坐标(似乎它不起作用):
/**
* Callback from the CameraCaptureSession.CaptureCallback
*/
@Override
public void onFaceDetection(Face[] faces) {
if (mCameraView != null) {
setFaceDetectionMatrix();
setFaceDetectionLayout(faces);
}
}
/**
* This method gets the scaling values of the face in matrix
*/
private void setFaceDetectionMatrix() {
// Face Detection Matrix
mFaceDetectionMatrix = new Matrix();
// Need mirror for front camera.
boolean mirror = mCameraView.getFacing() == CameraView.FACING_FRONT;
mFaceDetectionMatrix.setScale(mirror ? -1 : 1, 1);
mFaceDetectionMatrix.postRotate(mCameraDisplayOrientation);
Rect activeArraySizeRect = mCameraView.getCameraCharacteristics().get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
Log.i("Test", "activeArraySizeRect1: (" + activeArraySizeRect + ") -> " + activeArraySizeRect.width() + ", " + activeArraySizeRect.height());
Log.i("Test", "activeArraySizeRect2: " + cameraOverlayDrawingView.getWidth() + ", " + cameraOverlayDrawingView.getHeight());
float s1 = cameraOverlayDrawingView.getWidth() / activeArraySizeRect.width();
float s2 = cameraOverlayDrawingView.getHeight() / activeArraySizeRect.height();
mFaceDetectionMatrix.postScale(s1, s2);
mFaceDetectionMatrix.postTranslate(cameraOverlayDrawingView.getWidth() / 2, cameraOverlayDrawingView.getHeight() / 2);
}
/**
* This method set the matrix for translating rect
*/
private void setFaceDetectionLayout(Face[] faces) {
if (faces.length == 0) {
cameraOverlayDrawingView.setHaveFaces(false, null);
} else if (faces.length > 0) {
List<Rect> faceRects;
faceRects = new ArrayList<>();
for (int i = 0; i < faces.length; i++) {
Log.i("Test", "Activity face" + i + " bounds: " + faces[i].getBounds());
if (faces[i].getScore() > 50) {
int left = faces[i].getBounds().left;
int top = faces[i].getBounds().top;
int right = faces[i].getBounds().right;
int bottom = faces[i].getBounds().bottom;
Rect uRect = new Rect(left, top, right, bottom);
RectF rectF = new RectF(uRect);
mFaceDetectionMatrix.mapRect(rectF);
uRect.set((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom);
Log.i("Test", "Activity rect" + i + " bounds: " + uRect);
faceRects.add(uRect);
}
}
cameraOverlayDrawingView.setHaveFaces(true, faceRects);
}
}
答案 0 :(得分:6)
新强> 我管理了所有手机轮换。我估计offsetDxDy取决于我的布局,但是如果我要告诉你实话我不知道为什么我把它的值设为100.它在我的华为P9上工作得很好并且我以经验的方式找到它。我仍然没有试图找出是否取决于我的手机或我的XML布局或两者。
无论如何 Matrices现在已找到,因此您可以根据自己的需要调整它们。
注意:我的setRotation
不是那么一般,因为我没有在
int orientationOffset = mCameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
您可以尝试这样做,以便使用SENSOR_ORIENTATION
的完整通用代码与此示例中的代码不同。
因此,此代码适用于带有方向为270的硬件相机传感器的手机。
华为P9有它。
只是为了让您了解旋转绑定到硬件传感器方向,这也适用于我的P9(但我没有任何其他硬件可以测试)
if (mSwappedDimensions) {
// Display Rotation 0
mFaceDetectionMatrix.setRotate(orientationOffset);
mFaceDetectionMatrix.postScale(mirror ? -s1 : s1, s2);
mFaceDetectionMatrix.postTranslate(mPreviewSize.getHeight() + offsetDxDy, mPreviewSize.getWidth() + offsetDxDy);
} else {
// Display Rotation 90 e 270
if (displayRotation == Surface.ROTATION_90) {
mFaceDetectionMatrix.setRotate(orientationOffset + 90);
mFaceDetectionMatrix.postScale(mirror ? -s1 : s1, s2);
mFaceDetectionMatrix.postTranslate(mPreviewSize.getWidth() + offsetDxDy, -offsetDxDy);
} else if (displayRotation == Surface.ROTATION_270) {
mFaceDetectionMatrix.setRotate(orientationOffset + 270);
mFaceDetectionMatrix.postScale(mirror ? -s1 : s1, s2);
mFaceDetectionMatrix.postTranslate(-offsetDxDy, mPreviewSize.getHeight() + offsetDxDy);
}
}
这是我的最终代码(也可在GitHub上找到)
int orientationOffset = mCameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
Rect activeArraySizeRect = mCameraCharacteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
// Face Detection Matrix
mFaceDetectionMatrix = new Matrix();
Log.i("Test", "activeArraySizeRect1: (" + activeArraySizeRect + ") -> " + activeArraySizeRect.width() + ", " + activeArraySizeRect.height());
Log.i("Test", "activeArraySizeRect2: " + mPreviewSize.getWidth() + ", " + mPreviewSize.getHeight());
float s1 = mPreviewSize.getWidth() / (float)activeArraySizeRect.width();
float s2 = mPreviewSize.getHeight() / (float)activeArraySizeRect.height();
//float s1 = mOverlayView.getWidth();
//float s2 = mOverlayView.getHeight();
boolean mirror = (facing == CameraCharacteristics.LENS_FACING_FRONT); // we always use front face camera
boolean weAreinPortrait = true;
int offsetDxDy = 100;
if (mSwappedDimensions) {
// Display Rotation 0
mFaceDetectionMatrix.setRotate(270);
mFaceDetectionMatrix.postScale(mirror ? -s1 : s1, s2);
mFaceDetectionMatrix.postTranslate(mPreviewSize.getHeight() + offsetDxDy, mPreviewSize.getWidth() + offsetDxDy);
} else {
// Display Rotation 90 e 270
if (displayRotation == Surface.ROTATION_90) {
mFaceDetectionMatrix.setRotate(0);
mFaceDetectionMatrix.postScale(mirror ? -s1 : s1, s2);
mFaceDetectionMatrix.postTranslate(mPreviewSize.getWidth() + offsetDxDy, -offsetDxDy);
} else if (displayRotation == Surface.ROTATION_270) {
mFaceDetectionMatrix.setRotate(180);
mFaceDetectionMatrix.postScale(mirror ? -s1 : s1, s2);
mFaceDetectionMatrix.postTranslate(-offsetDxDy, mPreviewSize.getHeight() + offsetDxDy);
}
}
这是你所在的公共github回购 可以找到代码: https://github.com/shadowsheep1/android-camera2-api-face-recon。希望它可以 帮你。
无论如何只是为了给你一些理论,你正在做的是2D平面变换。我的意思是你有一个平面(硬件传感器),你必须在预览平面上重新映射该平面上的对象。
所以你必须照顾:
数学理论
我前段时间也在我的博客上写了一些技术帖子,但它们都是意大利语。