在Android中裁剪检测到的脸部

Tap*_*tro 6 android crop face-detection

我需要的:

从图像中裁剪出唯一的面孔。

我做了什么:

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);
Run Code Online (Sandbox Code Playgroud)

相同的代码适用于在画布上绘制它,但是裁剪时不起作用。

现在,我正在为整个视图拍照。只是需要如何从该图像进行裁剪,如下所示。 在此处输入图片说明

Tap*_*tro 2

大家好,感谢您的宝贵时间。我使用 opencv 成功地将图像从源位图中裁剪出来。

链接我关注了 OpenCv 代码

保存裁剪后的脸部的代码。

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());
            }
Run Code Online (Sandbox Code Playgroud)

归功于麻省理工学院