【问题标题】:Crop Detected Face in Android在 Android 中裁剪检测到的人脸
【发布时间】: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


    【解决方案1】:

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

    我关注的链接 OpenCv Code

    保存裁剪面的代码。

    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());
                }
    

    归功于MIT

    【讨论】:

      【解决方案2】:

      您可以使用Bitmap.createBitmap() 方法裁剪图像。您可以指定所需的矩形(开始 X,开始 Y,宽度,高度),而不是传递整个图像(0、0、宽度、高度)

      【讨论】:

      • 你能用一个例子详细说明它(开始X,开始Y,宽度,高度)吗?
      • destRect = new RectF(left, top, right, bottom); 这是你想要的裁剪图像的大小,对吧?所以,startX=left,startY=top,width=right-top 和 height=top-bottom
      • 没有兄弟不工作。你能建议任何其他解决方案吗?对我来说 left 和 top 有时会出现 -ve 值。这就是原因。
      • 对不起,这是裁剪的方式...如果你有负值,这意味着边界在图像之外?或者你可以取绝对值吗? linkedin.com/pulse/…
      • 你能告诉我一件事,我得到的这些值如下所示,用于绘制检测到的矩形圆脸:顶部:9.6910095 左侧:492.4464 右侧:973.1604 底部:460.36035 高度:400.59497 宽度:320.47598 相同我我正在尝试裁剪,但没有得到它。
      猜你喜欢
      • 1970-01-01
      • 2012-10-25
      • 2014-05-30
      • 2014-04-28
      • 2015-03-11
      • 2016-01-01
      • 1970-01-01
      • 2011-04-27
      • 2011-11-14
      相关资源
      最近更新 更多