[英]Crop Detected Face in Android
What I Need: 我需要的:
To Crop out the only exact face from Image. 从Image中裁剪出唯一精确的面部。
What I Have Done: 我做了什么:
https://github.com/blundell/FaceDetectionTutorialWithPreview https://github.com/blundell/FaceDetectionTutorialWithPreview
With This way or with 用这种方式或用
https://github.com/googlesamples/android-vision https://github.com/googlesamples/android-vision
Both way I am getting Face Detected. 两种方式我都得到了面部检测。 But I am unable to crop the detected Face. 但是我无法裁剪检测到的脸部。
I tried with 我试过了
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);
The same code working for drawing it for canvas but while cropping its not working. 相同的代码用于绘制画布,但裁剪不起作用。
For Now, I am taking pictures of the whole view. 现在,我正在拍摄整个视图。 Just need the way how to crop from that image like below. 只需要如何从下面的图像裁剪。
You can crop your image with the Bitmap.createBitmap()
method. 您可以使用Bitmap.createBitmap()
方法裁剪图像。 Instead of passing whole image (0, 0, width, height), you can specified the rectangle you want (start X, start Y, width, height) 您可以指定所需的矩形(开始X,开始Y,宽度,高度),而不是传递整个图像(0,0,宽度,高度)。
Hi Guys Thanks for your time. 嗨大家好,感谢您的时间。 I successfully cropped the images out of source bitmap using opencv. 我使用opencv成功地从源位图中裁剪出图像。
Link i followed OpenCv Code 链接我遵循OpenCv代码
Code To Save cropped Face. 代码保存裁剪的面部。
if ((facesArray.length>0) && (faceState==SEARCHING))
{
Mat m=new Mat();
m=mGray.submat(facesArray[0]);
mBitmap = Bitmap.createBitmap(m.width(),m.height(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(m, mBitmap);
Message msg = new Message();
String textTochange = "IMG";
msg.obj = textTochange;
//mHandler.sendMessage(msg);
textTochange = fr.predict(m);
mLikely=fr.getProb();
msg = new Message();
msg.obj = textTochange;
mHandler.sendMessage(msg);
//for saving added below code
bmpToSave = Bitmap.createBitmap(m.width(), m.height(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(m,bmpToSave);
bmpToSave= Bitmap.createScaledBitmap(bmpToSave, 128, 128, false);
File pictureFile = getOutputMediaFile();
Log.v("path: ", "" + pictureFile.getAbsolutePath());
Log.v("path: ", "" + pictureFile.getPath());
///storage/emulated/0/ABC/MI_04092018_1218.jpg
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
bmpToSave.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.close();
} catch (FileNotFoundException e) {
Log.d("FD", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("FD", "Error accessing file: " + e.getMessage());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.