Beispiel #1
0
        public int faceFeature(ARGBImg argbImg, byte[] feature)
        {
            FaceSDKManager.getInstance().getFaceDetector().clearTrackedFaces();
            int ret = FaceSDKManager.getInstance().getFaceDetector().detect(argbImg.data, argbImg.width, argbImg.height);

            // Log.i("wtf", "feature detect from image->" + ret + " " + argbImg.width + " " + argbImg.height);

            FaceInfo[] faceInfos = FaceSDKManager.getInstance().getFaceDetector().TrackedFaces;
            // Log.i("wtf", "feature detect from image faceInfos ->" + faceInfos);
            if (faceInfos != null && faceInfos.Length > 0)
            {
                FaceInfo faceInfo = faceInfos[0];
                // Log.i("wtf", "feature detect from image faceInfos ->" + faceInfo);
                // 可以ret FaceDetector.DETECT_CODE_OK和 FaceDetector.DETECT_CODE_HIT_LAST才进行特征抽取
                // if (faceInfo != null && (ret == FaceDetector.DETECT_CODE_OK || ret == FaceDetector
                // .DETECT_CODE_HIT_LAST)) {
                if (faceInfo != null && (ret != FaceDetector.NO_FACE_DETECTED && ret != FaceDetector.UNKNOW_TYPE))
                {
                    return(faceRecognize.ExtractFeature(argbImg.data, argbImg.height, argbImg.width, FaceSDK.ImgType.Argb,
                                                        feature, PerformQuery <int>(faceInfo.Landmarks), FaceSDK.RecognizeType.RecognizeLive));
                }
            }
            FaceSDKManager.getInstance().getFaceDetector().clearTrackedFaces();
            return(ret);
        }
Beispiel #2
0
        // 活体检测
        private bool onLivenessCheck(int width, int height, int type)
        {
            bool isLiveness = false;
            // 判断当前是否有人脸
            long startTime = DateTime.Now.Millisecond;
            int  errorCode = FaceSDKManager.getInstance().getFaceDetector().detect(mRgbArray, width, height);

            LivenessModel livenessModel = new LivenessModel();

            livenessModel.setRgbDetectDuration(DateTime.Now.Millisecond - startTime);
            livenessModel.getImageFrame().setArgb(mRgbArray);
            livenessModel.getImageFrame().setWidth(width);
            livenessModel.getImageFrame().setHeight(height);
            livenessModel.setLiveType(type);
            livenessModel.setFaceDetectCode(errorCode);
            Log.Debug(TAG, "max_face_verification: " + errorCode + " duration:" + (DateTime.Now.Millisecond - startTime));

            if (errorCode == FaceTracker.ErrCode.Ok.Ordinal() || errorCode == FaceTracker.ErrCode.DataHitLast.Ordinal())
            {
                FaceInfo[] trackedface = FaceSDKManager.getInstance().getFaceDetector().TrackedFaces;
                livenessModel.setTrackFaceInfo(trackedface);
                if (trackedface != null && trackedface.Length > 0)
                {
                    FaceInfo faceInfo = trackedface[0];
                    livenessModel.setFaceInfo(faceInfo);

                    // 塞选人脸,可以调节距离、角度
                    //                if (!filter(faceInfo, width, height)) {
                    //                    livenessCallBack.onCallback(null);
                    //                    return isLiveness;
                    //                }
                    if (livenessCallBack != null)
                    {
                        livenessCallBack.onTip(0, "活体判断中");
                    }
                    float rgbScore = 0;
                    if ((type & MASK_RGB) == MASK_RGB)
                    {
                        startTime = DateTime.Now.Millisecond;
                        rgbScore  = rgbLiveness(mRgbArray, width, height, FaceFeature.PerformQuery <int>(trackedface[0].Landmarks));
                        livenessModel.setRgbLivenessScore(rgbScore);
                        livenessModel.setRgbLivenessDuration(DateTime.Now.Millisecond - startTime);
                    }
                    float irScore = 0;
                    if ((type & MASK_IR) == MASK_IR)
                    {
                        float maxWidth    = 0;
                        int   maxId       = 0;
                        float detectScore = 0;
                        if (trackedface != null && trackedface.Length > 0)
                        {
                            for (int i = 0; i < trackedface.Length; i++)
                            {
                                if (trackedface[i].MWidth > maxWidth)
                                {
                                    maxId       = i;
                                    maxWidth    = trackedface[i].MWidth;
                                    detectScore = trackedface[i].MConf;
                                }
                            }
                        }
                        if (trackedface != null)
                        {
                            float[] faceT = new float[] { trackedface[maxId].MCenterX,
                                                          trackedface[maxId].MCenterY, trackedface[maxId].MWidth,
                                                          trackedface[maxId].MAngle };
                            int[]   shape  = new int[144];
                            int[]   nPoint = new int[] { 0 };
                            float[] score  = new float[] { 0.0F };
                            FaceSDK.Run_align(nirRgbArray, height, width, FaceSDK.ImgType.Argb,
                                              FaceSDK.AlignMethodType.Cdnn, faceT, shape, nPoint, score, detectScore);
                            livenessModel.setShape(shape);
                            startTime = DateTime.Now.Millisecond;
                            //                    irScore = irLiveness(mIrByte, width, height, trackedfaces[0].landmarks);
                            irScore = irLiveness(mIrByte, width, height, shape);
                            livenessModel.setIrLivenessDuration(DateTime.Now.Millisecond - startTime);
                            livenessModel.setIrLivenessScore(irScore);
                        }
                    }
                    float depthScore = 0;
                    if ((type & MASK_DEPTH) == MASK_DEPTH)
                    {
                        startTime = DateTime.Now.Millisecond;
                        if (trackedface != null)
                        {
                            depthScore = depthLiveness(mDepthArray, width, height, FaceFeature.PerformQuery <int>(trackedface[0].Landmarks));
                            livenessModel.setDetphtLivenessDuration(DateTime.Now.Millisecond - startTime);
                            livenessModel.setDepthLivenessScore(depthScore);
                        }
                    }
                    if (livenessCallBack != null)
                    {
                        livenessCallBack.onCallback(livenessModel);
                    }

                    //                long time = System.currentTimeMillis();
                    //                saveRgbImage(String.valueOf(time), rgbScore, mRgbArray, width, height);
                    //                saveFile(String.valueOf(time), "nir", irScore, mIrByte);
                    //                saveFile(String.valueOf(time), "depth", depthScore, mDepthArray);
                }
            }
            else
            {
                checkFaceCode(errorCode);
                if (livenessCallBack != null)
                {
                    livenessCallBack.onCallback(null);
                }
            }
            // clearInfo();
            FaceInfo[] trackedfaces = FaceSDKManager.getInstance().getFaceDetector().TrackedFaces;
            livenessModel.setTrackFaceInfo(trackedfaces);
            if (livenessCallBack != null)
            {
                livenessCallBack.onCanvasRectCallback(livenessModel);
            }
            return(isLiveness);
        }