美文网首页
安卓OpenCV开发(四)人脸识别

安卓OpenCV开发(四)人脸识别

作者: motosheep | 来源:发表于2021-06-14 06:44 被阅读0次

    人脸识别,顾名思义,就是通过人脸对比的方式,得出人脸相识度的过程。区别于人脸检测。
    对于OpenCV的人脸检测,实现流程,请看我之前写的博客:
    OpenCV导入
    OpenCV人脸检测
    OpenCV竖屏检测

    本次人脸识别,实现思路如下:
    (一)读取本地数据源作为对比凭证源
    (二)动态读取视频捕获的人脸数据,于对比凭证源进行对比
    开始发车:

    (一)读取本地数据源作为对比凭证

    本次做法,为了方便演示,首先,准备了一些100px*100px像素的数据源图片,放到了指定的目录,然后通过File类的listFile()操作,在使用前,把目录下的的文件放到到内存中,这里使用OpenCV的bitmap转mat的方法。具体代码实现如下图:

     /**
         * 初始化
         */
        private void init() {
            try {
                File path = new File(FACE_FILE_PATH);
                path.mkdirs();
                File[] allFile = path.listFiles();
                for (File cache : allFile) {
                    Bitmap cacheBitmap = BitmapFactory.decodeFile(cache.getPath());
                    Mat mat1 = new Mat();
                    Utils.bitmapToMat(cacheBitmap, mat1);
                    Mat result = new Mat();
                    Imgproc.cvtColor(mat1, result, Imgproc.COLOR_BGR2GRAY);
                    Moments mom = Imgproc.moments(result, false);
                    putLocal(mom.toString(), mat1);
                }
            } catch (Exception e) {
    
            }
        }
    

    上图代码,就把本地所有的数据集合,放到了内存中。

    (二)动态读取视频捕获的人脸数据

    通过前面章节,已经可以动态监测到人脸数据了,这里直接使用即可,代码如下图:

      @Override
        public Mat onCameraFrame(Mat aInputFrame) {
            Imgproc.cvtColor(aInputFrame, grayscaleImage, Imgproc.COLOR_RGBA2RGB);
            MatOfRect faces = new MatOfRect();
            if (cascadeClassifier != null) {
                cascadeClassifier.detectMultiScale(grayscaleImage, faces, 1.1, 3, 2,
                        new Size(absoluteFaceSize, absoluteFaceSize), new Size());
            }
            Rect[] facesArray = faces.toArray();
            for (int i = 0; i < facesArray.length; i++) {
                detectFace(aInputFrame, facesArray[i]);
                Imgproc.rectangle(aInputFrame, facesArray[i].tl(), facesArray[i].br(), new Scalar(0, 255, 0, 255), 3);
            }
            return aInputFrame;
        }
    

    主要是调用detectFace()方法,把人脸帧和人脸数据,传递给外部进行相关处理。下图为监测人脸后的具体实现代码:

        @Override
        public void detectFace(final Mat source, final Rect face) {
            super.detectFace(source, face);
            ThreadManager.getImgExecutors().execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        Mat sub = source.submat(face);
                        Mat mat = new Mat();
                        Size size = new Size(100, 100);
                        Imgproc.resize(sub, mat, size);
                        Mat result = new Mat();
                        Imgproc.cvtColor(mat, result, Imgproc.COLOR_BGR2GRAY);
                        Moments mom = Imgproc.moments(result, false);
                        FaceDetectManager.getInstance().putFace(mom.m00 + "", mat);
                        FaceDetectManager.getInstance().check();
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.d("error2", "error set:" + e.getMessage());
                    }
                }
            });
        }
    

    可以看到也是保存了裁剪后的人脸图片,像素为100px*100px。
    然后就调用FaceDetectManager的相关方法。这里要注意的是,开了一个线程,为什么要开线程,请自行脑补。
    最后放上对比的核心代码:

    /**
         * 检测特征值
         */
        public void check() {
            if (System.currentTimeMillis() - mCheckTime > 2000) {
                mCheckTime = System.currentTimeMillis();
            } else {
                return;
            }
            //开始检测
            ThreadManager.getImgExecutors().execute(new Runnable() {
                @Override
                public void run() {
                    Log.d("check", "mFaceLocalMap大小:" + mFaceLocalMap.size() + "\tmFaceMemoryMap大小:" + mFaceMemoryMap.size());
                    for (Map.Entry<String, Mat> local : mFaceLocalMap.entrySet()) {
                        for (Map.Entry<String, Mat> memory : mFaceMemoryMap.entrySet()) {
    
                            Mat src1 = new Mat();
                            Imgproc.cvtColor(local.getValue(), src1, Imgproc.COLOR_BGR2GRAY);
                            Mat target1 = new Mat();
                            Imgproc.cvtColor(memory.getValue(), target1, Imgproc.COLOR_BGR2GRAY);
    
                            src1.convertTo(src1, CvType.CV_32F);
                            target1.convertTo(target1, CvType.CV_32F);
                            double similar = Imgproc.compareHist(src1, target1, Imgproc.CV_COMP_CORREL);
                            Log.e("相识度", "相似度 : ==" + similar);
                            src1.release();
                            target1.release();
                            if (similar > 0.6) {
                                if (mListener != null) {
                                    mListener.result(similar, local.getValue());
                                }
                            }
    
                        }
                    }
                    mFaceMemoryMap.clear();
                }
            });
        }
    

    主要还是通过Imgproc.compareHist()方法对mat之间进行对比,最后得出相似度。然后回调出去。

    注意:mat与bitmap之间的转换,如果使用了OpenCV的转换函数的话,记得分辨率保持一致,而且mat要求传入的是RGB格式的数据源,而bitmap的格式要求则要是ARGB_8888或者RGB_565。实现如下图:

    FaceDetectManager.getInstance().setSimilarResultListener(new FaceDetectManager.SimilarResultListener() {
                    @Override
                    public void result(final double similar, final Mat result) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    if (similar > 0.8) {
                                        Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);
                                        Utils.matToBitmap(result, bitmap);
                                        getTargetView().setImageBitmap(bitmap);
                                    }
                                } catch (Exception e) {
                                    Log.d("error", "error set:" + e.getMessage() + "\t\t" + (getTargetView() == null));
                                }
                            }
                        });
                    }
                });
    

    OpenCV转换源码如下图:

     /**
         * Converts OpenCV Mat to Android Bitmap.
         * <p>
         * <br>This function converts an image in the OpenCV Mat representation to the Android Bitmap.
         * <br>The input Mat object has to be of the types 'CV_8UC1' (gray-scale), 'CV_8UC3' (RGB) or 'CV_8UC4' (RGBA).
         * <br>The output Bitmap object has to be of the same size as the input Mat and of the types 'ARGB_8888' or 'RGB_565'.
         * <br>This function throws an exception if the conversion fails.
         *
         * @param mat is a valid input Mat object of types 'CV_8UC1', 'CV_8UC3' or 'CV_8UC4'.
         * @param bmp is a valid Bitmap object of the same size as the Mat and of type 'ARGB_8888' or 'RGB_565'.
         * @param premultiplyAlpha is a flag, that determines, whether the Mat needs to be converted to alpha premultiplied format (like Android keeps 'ARGB_8888' bitmaps); the flag is ignored for 'RGB_565' bitmaps.
         */
        public static void matToBitmap(Mat mat, Bitmap bmp, boolean premultiplyAlpha) {
            if (mat == null)
                throw new IllegalArgumentException("mat == null");
            if (bmp == null)
                throw new IllegalArgumentException("bmp == null");
            nMatToBitmap2(mat.nativeObj, bmp, premultiplyAlpha);
        }
    
        /**
         * Short form of the <b>matToBitmap(mat, bmp, premultiplyAlpha=false)</b>
         * @param mat is a valid input Mat object of the types 'CV_8UC1', 'CV_8UC3' or 'CV_8UC4'.
         * @param bmp is a valid Bitmap object of the same size as the Mat and of type 'ARGB_8888' or 'RGB_565'.
         */
        public static void matToBitmap(Mat mat, Bitmap bmp) {
            matToBitmap(mat, bmp, false);
        }
    

    相关要求请看英文描述。

    至此,完成,是不是非常简单。
    that's all------------------------------------------------------------------------------

    相关文章

      网友评论

          本文标题:安卓OpenCV开发(四)人脸识别

          本文链接:https://www.haomeiwen.com/subject/kgugeltx.html