android - 如何使用 MLKit firebase FaceDetection 捕获检测到的人脸图像
问题描述
我只想在camerasource上捕获检测到的面部图像而不是整个图像。
我就是这样的落水箱
public void draw(Canvas canvas) {
Face face = mFace;
if (face == null) {
return;
}
// Draws a circle at the position of the detected face, with the face's track id below.
float x = translateX(face.getPosition().x + face.getWidth() / 2);
float y = translateY(face.getPosition().y + face.getHeight() / 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(face.getWidth() / 2.0f);
float yOffset = scaleY(face.getHeight() / 2.0f);
float left = x - xOffset;
float top = y - yOffset;
float right = x + xOffset;
float bottom = y + yOffset;
this.faceTrackingCords.y = top;
this.faceTrackingCords.width = right;
this.faceTrackingCords.x = left-right;
this.faceTrackingCords.height = bottom;
canvas.drawRect(left, top, right, bottom, mBoxPaint);
}
并尝试像这样捕获图像,但裁剪后的图像不如预期。
mCameraSource.takePicture(null, new CameraSource.PictureCallback() {
@Override
public void onPictureTaken(byte[] bytes) {
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Bitmap bitmapCropped = Bitmap.createBitmap(bitmap,(int)faceTrackingCords.x, (int)faceTrackingCords.y,
(int)faceTrackingCords.width, (int)faceTrackingCords.height);
screenImage.setImageBitmap(bitmapCropped);
}
});
请帮忙
解决方案
推荐阅读
- javascript - 我想隐藏表单并显示书面成功消息以及使用哈希值重新加载页面
- excel - 将工作簿代码传输到模块会损坏 UsedRange.Rows 变量
- c# - 从组合框中添加引用名称而不是 ID
- c++ - 如何正确链接 cuda 和 cpp 文件之间创建的函数?
- jquery - 根据值更改表列颜色在剃须刀文件中使用 jquery 不起作用
- django - Django 测试有时会失败,有时会成功,可能的原因是什么?
- php - 如果单元格中的值重复,PHP会合并几行
- javascript - 移动版的下拉导航栏不会掉
- javascript - 将数组合并和相乘成一个数组
- swiftui - SwiftUI TextField 可触摸区域