人臉裁剪 人臉識別圖片裁剪 多人臉裁剪

最近用到人臉識別技術,有個需求是多人臉圖片的時候須要一個一個把人臉裁剪出來,以便獲取人臉的圖片,從而用其獲取單人臉的特徵:效果如如:(有連接demo)

https://download.csdn.net/download/qq_38355313/10438863

裁剪前
這裏寫圖片描述
裁剪後
這裏寫圖片描述web

上班時間關係,很少作解釋,直接貼代碼

private static BitmapFactory.Options BitmapFactoryOptionsbfo;
    private static ByteArrayOutputStream out;
    private static byte[] data;
    private static FaceDetector.Face[] myFace;
    private static FaceDetector myFaceDetect;
    private static int tx = 0;
    private static int ty = 0;
    private static int bx = 0;
    private static int by = 0;
    private static int width = 0;
    private static int height = 0;
    private static float wuchax = 0;
    private static float wuchay = 0;
    private static FaceDetector.Face face;
    private static PointF myMidPoint;
    private static float myEyesDistance;
    private static List<String> facePaths;
    private static String facePath;

    public static List<String> cutFace(Bitmap bitmap, Context context) {
        facePaths = null;
        BitmapFactoryOptionsbfo = new BitmapFactory.Options();
        BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565; // 構造位圖生成的參數,必須爲565。類名+enum
        out = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        data = out.toByteArray();
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
                BitmapFactoryOptionsbfo);
        try {
            out.flush();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        width = bitmap.getWidth();
        height = bitmap.getHeight();
        myFace = new FaceDetector.Face[5]; // 分配人臉數組空間
        myFaceDetect = new FaceDetector(bitmap.getWidth(), bitmap.getHeight(), 5);
        int numberOfFaceDetected = myFaceDetect.findFaces(bitmap, myFace);
        if (numberOfFaceDetected <= 0) {// FaceDetector構造實例並解析人臉
            bitmap.recycle();
            return null;
        }
        facePaths = new ArrayList<String>();
        for (int i = 0; i < numberOfFaceDetected; i++) {
            face = myFace[i];
            myMidPoint = new PointF();
            face.getMidPoint(myMidPoint);
            myEyesDistance = face.eyesDistance();   //獲得人臉中心點和眼間距離參數,並對每一個人臉進行畫框
            wuchax = myEyesDistance / 2 + myEyesDistance;
            wuchay = myEyesDistance * 2 / 3 + myEyesDistance;

            if (myMidPoint.x - wuchax < 0) {//判斷左邊是否出界
                tx = 0;
            } else {
                tx = (int) (myMidPoint.x - wuchax);
            }
            if (myMidPoint.x + wuchax > width) {//判斷右邊是否出界
                bx = width;
            } else {
                bx = (int) (myMidPoint.x + wuchax);
            }
            if (myMidPoint.y - wuchay < 0) {//判斷上邊是否出界
                ty = 0;
            } else {
                ty = (int) (myMidPoint.y - wuchay);
            }
            if (myMidPoint.y + wuchay > height) {//判斷下邊是否出界
                by = height;
            } else {
                by = (int) (myMidPoint.y + wuchay);
            }

            try {
                facePath = saveBitmapN(Bitmap.createBitmap(bitmap, tx, ty, bx - tx, by - ty));
            } catch (Exception e) {
                e.printStackTrace();
                facePath = null;
            }
            if (facePath != null) {
                facePaths.add(facePath);
            }
        }
        bitmap.recycle();
        return facePaths;
    }

下載連接

https://download.csdn.net/download/qq_38355313/10438863