【发布时间】:2019-02-21 08:16:10
【问题描述】:
我需要什么:
从图像中裁剪出唯一准确的人脸。
我做了什么:
https://github.com/blundell/FaceDetectionTutorialWithPreview
用这种方式或用
https://github.com/googlesamples/android-vision
两种方式都检测到人脸。但我无法裁剪检测到的人脸。
我试过了
Matrix matrix = new Matrix();
RectF sourceRect = null , destRect = null;
for (Camera.Face f : mFaces) {
// Draws a circle at the position of the detected face, with the face's track id below.
float x = translateX(f.rect.centerX() + f.rect.width() / 2);
float y = translateY(f.rect.centerY() + f.rect.height() / 2);
//canvas.drawCircle(x, y, FACE_POSITION_RADIUS, mFacePositionPaint);
// canvas.drawText("id: " + mFaceId, x + ID_X_OFFSET, y + ID_Y_OFFSET, mIdPaint);
// canvas.drawText("happiness: " + String.format("%.2f", face.getIsSmilingProbability()), x - ID_X_OFFSET, y - ID_Y_OFFSET, mIdPaint);
// canvas.drawText("right eye: " + String.format("%.2f", face.getIsRightEyeOpenProbability()), x + ID_X_OFFSET * 2, y + ID_Y_OFFSET * 2, mIdPaint);
//canvas.drawText("left eye: " + String.format("%.2f", face.getIsLeftEyeOpenProbability()), x - ID_X_OFFSET*2, y - ID_Y_OFFSET*2, mIdPaint);
// Draws a bounding box around the face.
float xOffset = scaleX(f.rect.width() / 2.0f);
float yOffset = scaleY(f.rect.height() / 2.0f);
float left = x - xOffset;
float top = y - yOffset;
float right = x + xOffset;
float bottom = y + yOffset;
sourceRect = new RectF(0, 0, source.getWidth(), source.getHeight());
destRect = new RectF(left, top, right, bottom);
Log.v("Margins: ","top: "+top+"\n"+"left: "+left+"\n"+"right: "+right+"\n"+"bottom: "+bottom+"\n");
}
matrix.setRectToRect(sourceRect, destRect, Matrix.ScaleToFit.CENTER);
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
matrix, true);
为画布绘制相同的代码,但在裁剪时不起作用。
【问题讨论】:
标签: android crop face-detection