Пример #1
0
        public ASF_LivenessInfo GetLivenessScoreIR(Bitmap bitmap, ASF_SingleFaceInfo singleFaceInfo)
        {
            var retCode      = MError.MERR_UNKNOWN;
            var livenessinfo = new ASF_LivenessInfo();

            try
            {
                if (ptrVideoEngine == IntPtr.Zero)
                {
                    retCode = InitialEngineForVideo();
                }
                else
                {
                    retCode = MError.MOK;
                }

                if (retCode == MError.MOK)
                {
                    livenessinfo = FaceProcessHelper.LivenessEstimationIR(ptrVideoEngine, bitmap, singleFaceInfo);
                }

                if (retCode != MError.MOK)
                {
                    Afw.Core.Helper.SimplifiedLogHelper.WriteIntoSystemLog(nameof(Afw.WinForm.EngineContext), $"GetLivenessScore Error:{retCode.GetFieldDescription()}");
                }
            }
            catch (Exception ex)
            {
                Afw.Core.Helper.SimplifiedLogHelper.WriteIntoSystemLog(nameof(Afw.WinForm.EngineContext), $"GetLivenessScore Exception:{ex.ToString()}");
            }
            return(livenessinfo);
        }
        /// <summary>
        /// 获取IR活体结果
        /// </summary>
        /// <param name="livenessInfo">IR活体结果</param>
        /// <returns>返回0表示正常;返回负数请根据ErrorCodeUtil类注释查看;其他值请在官网-帮助中心查询</returns>
        public int ASFGetLivenessScore_IR(out LivenessInfo livenessInfo)
        {
            int retCode = -1;

            livenessInfo = new LivenessInfo();
            IntPtr pLiveness = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_LivenessInfo>());

            //调用SDK接口
            retCode = ASFFunctions.ASFGetLivenessScore_IR(pEngine, pLiveness);
            if (retCode != 0)
            {
                MemoryUtil.Free(pLiveness);
                return(retCode);
            }
            //转化结果
            ASF_LivenessInfo asfLivenessInfo = new ASF_LivenessInfo();

            asfLivenessInfo  = MemoryUtil.PtrToStructure <ASF_LivenessInfo>(pLiveness);
            livenessInfo.num = asfLivenessInfo.num;
            if (asfLivenessInfo.num > 0)
            {
                livenessInfo.isLive = new int[asfLivenessInfo.num];
                Marshal.Copy(asfLivenessInfo.isLive, livenessInfo.isLive, 0, asfLivenessInfo.num);
            }
            MemoryUtil.FreeArray(pLiveness);
            return(retCode);
        }
Пример #3
0
        /// <summary>
        /// RGB活体检测
        /// </summary>
        /// <param name="pEngine"></param>
        /// <param name="imageInfo"></param>
        /// <param name="multiFaceInfo"></param>
        /// <returns></returns>
        public static ASF_LivenessInfo LivenessEstimation(IntPtr pEngine, ImageInfo imageInfo, ASF_MultiFaceInfo multiFaceInfo)
        {
            IntPtr pMultiFaceInfo = MemoryHelper.Malloc(MemoryHelper.SizeOf <ASF_MultiFaceInfo>());

            MemoryHelper.StructureToPtr(multiFaceInfo, pMultiFaceInfo);

            try
            {
                if (multiFaceInfo.faceNum == 0)
                {
                    return(new ASF_LivenessInfo());
                }

                if (multiFaceInfo.faceNum > 1)
                {
                    ASF_SingleFaceInfo singleFaceInfo   = GetMaxFace(multiFaceInfo);
                    ASF_MultiFaceInfo  multiFaceInfoNeo = new ASF_MultiFaceInfo();
                    multiFaceInfoNeo.faceRects = MemoryHelper.Malloc(MemoryHelper.SizeOf <MRECT>());
                    MemoryHelper.StructureToPtr <MRECT>(singleFaceInfo.faceRect, multiFaceInfoNeo.faceRects);
                    multiFaceInfoNeo.faceOrients = MemoryHelper.Malloc(MemoryHelper.SizeOf <int>());
                    MemoryHelper.StructureToPtr <int>(singleFaceInfo.faceOrient, multiFaceInfoNeo.faceOrients);
                    multiFaceInfoNeo.faceNum = 1;
                    MemoryHelper.StructureToPtr(multiFaceInfoNeo, pMultiFaceInfo);
                }
                //活体信息检测
                int retCode = ASFWrapper.ASFProcess(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pMultiFaceInfo, FaceEngineMask.ASF_LIVENESS);
                if (retCode == 0)
                {
                    //获取活体信息
                    IntPtr pLivenessInfo = MemoryHelper.Malloc(MemoryHelper.SizeOf <ASF_LivenessInfo>());

                    retCode = ASFWrapper.ASFGetLivenessScore(pEngine, pLivenessInfo);

                    SimplifiedLogHelper.WriteIntoSystemLog(nameof(Afw.Core.Helper.FaceProcessHelper), $"Get Liveness Result:{retCode}");
                    ASF_LivenessInfo livenessInfo = MemoryHelper.PtrToStructure <ASF_LivenessInfo>(pLivenessInfo);

                    //释放内存

                    MemoryHelper.Free(pLivenessInfo);
                    return(livenessInfo);
                }
                else
                {
                    return(new ASF_LivenessInfo());
                }
            }
            catch (Exception ex)
            {
                Afw.Core.Helper.SimplifiedLogHelper.WriteIntoSystemLog(nameof(FaceProcessHelper), $"LivenessEstimation Exception => {ex.ToString()}");
            }
            finally
            {
                //释放内存
                MemoryHelper.Free(pMultiFaceInfo);
                MemoryHelper.Free(imageInfo.imgData);
            }
            return(new ASF_LivenessInfo());
        }
Пример #4
0
        public static ASF_LivenessInfo LivenessEstimationIR(IntPtr pEngine, ImageInfo imageInfo, ASF_SingleFaceInfo singleFaceInfo)
        {
            ASF_MultiFaceInfo multiFaceInfoNeo = new ASF_MultiFaceInfo();

            multiFaceInfoNeo.faceRects = MemoryHelper.Malloc(MemoryHelper.SizeOf <MRECT>());
            MemoryHelper.StructureToPtr <MRECT>(singleFaceInfo.faceRect, multiFaceInfoNeo.faceRects);
            multiFaceInfoNeo.faceOrients = MemoryHelper.Malloc(MemoryHelper.SizeOf <int>());
            MemoryHelper.StructureToPtr <int>(singleFaceInfo.faceOrient, multiFaceInfoNeo.faceOrients);
            multiFaceInfoNeo.faceNum = 1;
            ASF_LivenessInfo livenessinfo = LivenessEstimationIR(pEngine, imageInfo, multiFaceInfoNeo);

            return(livenessinfo);
        }
Пример #5
0
        /// <summary>
        /// 红外活体检测
        /// </summary>
        /// <param name="pEngine">引擎Handle</param>
        /// <param name="imageInfo">图像数据</param>
        /// <param name="multiFaceInfo">活体检测结果</param>
        /// <returns>保存活体检测结果结构体</returns>
        public static ASF_LivenessInfo LivenessInfo_IR(IntPtr pEngine, ImageInfo imageInfo, ASF_MultiFaceInfo multiFaceInfo, out int retCode)
        {
            IntPtr pMultiFaceInfo = UtilToolMemory.Malloc(UtilToolMemory.SizeOf <ASF_MultiFaceInfo>());

            UtilToolMemory.StructureToPtr(multiFaceInfo, pMultiFaceInfo);

            if (multiFaceInfo.faceNum == 0)
            {
                retCode = -1;
                //释放内存
                UtilToolMemory.Free(pMultiFaceInfo);
                return(new ASF_LivenessInfo());
            }

            try
            {
                //人脸信息处理
                retCode = ASFFunctions.ASFProcess_IR(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pMultiFaceInfo, FaceEngineMask.ASF_IR_LIVENESS);
                if (retCode == 0)
                {
                    //获取活体检测结果
                    IntPtr pLivenessInfo = UtilToolMemory.Malloc(UtilToolMemory.SizeOf <ASF_LivenessInfo>());
                    retCode = ASFFunctions.ASFGetLivenessScore_IR(pEngine, pLivenessInfo);
                    Console.WriteLine("Get Liveness Result:" + retCode);
                    ASF_LivenessInfo livenessInfo = UtilToolMemory.PtrToStructure <ASF_LivenessInfo>(pLivenessInfo);

                    //释放内存
                    UtilToolMemory.Free(pMultiFaceInfo);
                    UtilToolMemory.Free(pLivenessInfo);
                    return(livenessInfo);
                }
                else
                {
                    //释放内存
                    UtilToolMemory.Free(pMultiFaceInfo);
                    return(new ASF_LivenessInfo());
                }
            }
            catch
            {
                retCode = -1;
                //释放内存
                UtilToolMemory.Free(pMultiFaceInfo);
                return(new ASF_LivenessInfo());
            }
        }
Пример #6
0
        /// <summary>
        /// Infrared in vivo detection
        /// </summary>
        /// <param name="pEngine">Engine Handle</param>
        /// <param name="imageInfo">Image data</param>
        /// <param name="multiFaceInfo">In vivo test results</param>
        /// <returns>Preserve the structure of in vivo detection results</returns>
        public static ASF_LivenessInfo LivenessInfo_IR(IntPtr pEngine, ImageInfo imageInfo, ASF_MultiFaceInfo multiFaceInfo, out int retCode)
        {
            IntPtr pMultiFaceInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_MultiFaceInfo>());

            MemoryUtil.StructureToPtr(multiFaceInfo, pMultiFaceInfo);

            if (multiFaceInfo.faceNum == 0)
            {
                retCode = -1;
                //Release memory
                MemoryUtil.Free(pMultiFaceInfo);
                return(new ASF_LivenessInfo());
            }

            try
            {
                //Face information processing
                retCode = ASFFunctions.ASFProcess_IR(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, pMultiFaceInfo, FaceEngineMask.ASF_IR_LIVENESS);
                if (retCode == 0)
                {
                    //Obtain in vivo test results
                    IntPtr pLivenessInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_LivenessInfo>());
                    retCode = ASFFunctions.ASFGetLivenessScore_IR(pEngine, pLivenessInfo);
                    Console.WriteLine("Get Liveness Result:" + retCode);
                    ASF_LivenessInfo livenessInfo = MemoryUtil.PtrToStructure <ASF_LivenessInfo>(pLivenessInfo);

                    //Release memory
                    MemoryUtil.Free(pMultiFaceInfo);
                    MemoryUtil.Free(pLivenessInfo);
                    return(livenessInfo);
                }
                else
                {
                    //Release memory
                    MemoryUtil.Free(pMultiFaceInfo);
                    return(new ASF_LivenessInfo());
                }
            }
            catch
            {
                retCode = -1;
                //Release memory
                MemoryUtil.Free(pMultiFaceInfo);
                return(new ASF_LivenessInfo());
            }
        }
 /// <summary>
 ///
 /// </summary>
 /// <param name="pEngine"></param>
 /// <param name="needFaceInfo"></param>
 /// <param name="needRgbLive"></param>
 /// <param name="multiFaceInfo"></param>
 /// <param name="hadFaceInfo"></param>
 /// <param name="ageInfo"></param>
 /// <param name="genderInfo"></param>
 /// <param name="face3DAngleInfo"></param>
 /// <param name="rgbLiveInfo"></param>
 /// <param name="hadRgbLive"></param>
 /// <param name="pMultiFaceInfo"></param>
 /// <param name="aSF_MaskInfo"></param>
 /// <param name="lanMaskInfo"></param>
 /// <returns></returns>
 private static int[] ReadyFaceinStruct(IntPtr pEngine, bool needFaceInfo, bool needRgbLive, ASF_MultiFaceInfo multiFaceInfo,
                                        ref bool hadFaceInfo, ref ASF_AgeInfo ageInfo, ref ASF_GenderInfo genderInfo, ref ASF_Face3DAngle face3DAngleInfo,
                                        ref ASF_LivenessInfo rgbLiveInfo, ref bool hadRgbLive, IntPtr pMultiFaceInfo, ref ASF_MaskInfo aSF_MaskInfo, ref ASF_LandMarkInfo lanMaskInfo)
 {
     if (pMultiFaceInfo != IntPtr.Zero)
     {
         if (needFaceInfo &&
             multiFaceInfo.FaceNum > 0)
         {
             hadFaceInfo     = true;
             ageInfo         = AgeEstimation(pEngine);
             genderInfo      = GenderEstimation(pEngine);
             face3DAngleInfo = Face3DAngleDetection(pEngine);
             aSF_MaskInfo    = MaskEstimation(pEngine);
             lanMaskInfo     = FaceLandEstimation(pEngine);
         }
         if (needRgbLive &&
             multiFaceInfo.FaceNum == 1)
         {
             hadRgbLive  = true;
             rgbLiveInfo = LivenessInfo_RGB(pEngine);
         }
         MemoryUtil.Free(ref pMultiFaceInfo);
     }
     int[] orienArry;
     if (multiFaceInfo.FaceNum > 0)
     {
         orienArry = new int[multiFaceInfo.FaceNum];
         Marshal.Copy(multiFaceInfo.FaceOrients, orienArry, 0, multiFaceInfo.FaceNum);
     }
     else
     {
         orienArry = new int[] { };
     }
     return(orienArry);
 }
Пример #8
0
        /// <summary>
        /// RGB摄像头Paint事件,同步RGB人脸框,对比人脸框后进行IR活体检测
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void irVideoSource_Paint(object sender, PaintEventArgs e)
        {
            if (isDoubleShot && irVideoSource.IsRunning)
            {
                //如果双摄,且IR摄像头工作,获取IR摄像头图片
                Bitmap irBitmap = irVideoSource.GetCurrentVideoFrame();
                if (irBitmap == null)
                {
                    return;
                }
                //得到Rect
                MRECT rect = new MRECT();
                lock (rectLock)
                {
                    rect = allRect;
                }
                float irOffsetX = irVideoSource.Width * 1f / irBitmap.Width;
                float irOffsetY = irVideoSource.Height * 1f / irBitmap.Height;
                float offsetX   = irVideoSource.Width * 1f / rgbVideoSource.Width;
                float offsetY   = irVideoSource.Height * 1f / rgbVideoSource.Height;
                //检测IR摄像头下最大人脸
                Graphics g = e.Graphics;

                float x      = rect.left * offsetX;
                float width  = rect.right * offsetX - x;
                float y      = rect.top * offsetY;
                float height = rect.bottom * offsetY - y;
                //根据Rect进行画框
                g.DrawRectangle(Pens.Red, x, y, width, height);
                if (trackIRUnit.message != "" && x > 0 && y > 0)
                {
                    //将上一帧检测结果显示到页面上
                    g.DrawString(trackIRUnit.message, font, trackIRUnit.message.Contains("活体") ? blueBrush : yellowBrush, x, y - 15);
                }

                //保证只检测一帧,防止页面卡顿以及出现其他内存被占用情况
                if (isIRLock == false)
                {
                    isIRLock = true;
                    //异步处理提取特征值和比对,不然页面会比较卡
                    ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
                    {
                        if (rect.left != 0 && rect.right != 0 && rect.top != 0 && rect.bottom != 0)
                        {
                            bool isLiveness = false;
                            try
                            {
                                //得到当前摄像头下的图片
                                if (irBitmap != null)
                                {
                                    //检测人脸,得到Rect框
                                    ASF_MultiFaceInfo irMultiFaceInfo = FaceUtil.DetectFace(pVideoIRImageEngine, irBitmap);
                                    if (irMultiFaceInfo.faceNum <= 0)
                                    {
                                        return;
                                    }
                                    //得到最大人脸
                                    ASF_SingleFaceInfo irMaxFace = FaceUtil.GetMaxFace(irMultiFaceInfo);
                                    //得到Rect
                                    MRECT irRect = irMaxFace.faceRect;
                                    //判断RGB图片检测的人脸框与IR摄像头检测的人脸框偏移量是否在误差允许范围内
                                    if (isInAllowErrorRange(rect.left * offsetX / irOffsetX, irRect.left) && isInAllowErrorRange(rect.right * offsetX / irOffsetX, irRect.right) &&
                                        isInAllowErrorRange(rect.top * offsetY / irOffsetY, irRect.top) && isInAllowErrorRange(rect.bottom * offsetY / irOffsetY, irRect.bottom))
                                    {
                                        int retCode_Liveness = -1;
                                        //将图片进行灰度转换,然后获取图片数据
                                        ImageInfo irImageInfo = ImageUtil.ReadBMP_IR(irBitmap);
                                        if (irImageInfo == null)
                                        {
                                            return;
                                        }
                                        //IR活体检测
                                        ASF_LivenessInfo liveInfo = FaceUtil.LivenessInfo_IR(pVideoIRImageEngine, irImageInfo, irMultiFaceInfo, out retCode_Liveness);
                                        //判断检测结果
                                        if (retCode_Liveness == 0 && liveInfo.num > 0)
                                        {
                                            int isLive = MemoryUtil.PtrToStructure <int>(liveInfo.isLive);
                                            isLiveness = (isLive == 1) ? true : false;
                                        }
                                        if (irImageInfo != null)
                                        {
                                            MemoryUtil.Free(irImageInfo.imgData);
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.Message);
                            }
                            finally
                            {
                                trackIRUnit.message = string.Format("IR{0}", isLiveness ? "活体" : "假体");
                                if (irBitmap != null)
                                {
                                    irBitmap.Dispose();
                                }
                                isIRLock = false;
                            }
                        }
                        else
                        {
                            trackIRUnit.message = string.Empty;
                        }
                        isIRLock = false;
                    }));
                }
            }
        }
Пример #9
0
        /// <summary>
        /// RGB摄像头Paint事件,图像显示到窗体上,得到每一帧图像,并进行处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void videoSource_Paint(object sender, PaintEventArgs e)
        {
            if (rgbVideoSource.IsRunning)
            {
                //得到当前RGB摄像头下的图片
                Bitmap bitmap = rgbVideoSource.GetCurrentVideoFrame();
                if (bitmap == null)
                {
                    return;
                }
                //检测人脸,得到Rect框
                ASF_MultiFaceInfo multiFaceInfo = FaceUtil.DetectFace(pVideoEngine, bitmap);
                //得到最大人脸
                ASF_SingleFaceInfo maxFace = FaceUtil.GetMaxFace(multiFaceInfo);
                //得到Rect
                MRECT rect = maxFace.faceRect;
                //检测RGB摄像头下最大人脸
                Graphics g       = e.Graphics;
                float    offsetX = rgbVideoSource.Width * 1f / bitmap.Width;
                float    offsetY = rgbVideoSource.Height * 1f / bitmap.Height;
                float    x       = rect.left * offsetX;
                float    width   = rect.right * offsetX - x;
                float    y       = rect.top * offsetY;
                float    height  = rect.bottom * offsetY - y;
                //根据Rect进行画框
                g.DrawRectangle(Pens.Red, x, y, width, height);
                if (trackRGBUnit.message != "" && x > 0 && y > 0)
                {
                    //将上一帧检测结果显示到页面上
                    g.DrawString(trackRGBUnit.message, font, trackRGBUnit.message.Contains("活体") ? blueBrush : yellowBrush, x, y - 15);
                }

                //保证只检测一帧,防止页面卡顿以及出现其他内存被占用情况
                if (isRGBLock == false)
                {
                    isRGBLock = true;
                    //异步处理提取特征值和比对,不然页面会比较卡
                    ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
                    {
                        if (rect.left != 0 && rect.right != 0 && rect.top != 0 && rect.bottom != 0)
                        {
                            try
                            {
                                lock (rectLock)
                                {
                                    allRect.left   = (int)(rect.left * offsetX);
                                    allRect.top    = (int)(rect.top * offsetY);
                                    allRect.right  = (int)(rect.right * offsetX);
                                    allRect.bottom = (int)(rect.bottom * offsetY);
                                }

                                bool isLiveness = false;

                                //调整图片数据,非常重要
                                ImageInfo imageInfo = ImageUtil.ReadBMP(bitmap);
                                if (imageInfo == null)
                                {
                                    return;
                                }
                                int retCode_Liveness = -1;
                                //RGB活体检测
                                ASF_LivenessInfo liveInfo = FaceUtil.LivenessInfo_RGB(pVideoRGBImageEngine, imageInfo, multiFaceInfo, out retCode_Liveness);
                                //判断检测结果
                                if (retCode_Liveness == 0 && liveInfo.num > 0)
                                {
                                    int isLive = MemoryUtil.PtrToStructure <int>(liveInfo.isLive);
                                    isLiveness = (isLive == 1) ? true : false;
                                }
                                if (imageInfo != null)
                                {
                                    MemoryUtil.Free(imageInfo.imgData);
                                }
                                if (isLiveness)
                                {
                                    //提取人脸特征
                                    IntPtr feature   = FaceUtil.ExtractFeature(pVideoRGBImageEngine, bitmap, maxFace);
                                    float similarity = 0f;
                                    //得到比对结果
                                    int result = compareFeature(feature, out similarity);
                                    MemoryUtil.Free(feature);
                                    if (result > -1)
                                    {
                                        //将比对结果放到显示消息中,用于最新显示
                                        trackRGBUnit.message = string.Format(" {0}号 {1},{2}", result, similarity, string.Format("RGB{0}", isLiveness ? "活体" : "假体"));
                                    }
                                    else
                                    {
                                        //显示消息
                                        trackRGBUnit.message = string.Format("RGB{0}", isLiveness ? "活体" : "假体");
                                    }
                                }
                                else
                                {
                                    //显示消息
                                    trackRGBUnit.message = string.Format("RGB{0}", isLiveness ? "活体" : "假体");
                                }
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.Message);
                            }
                            finally
                            {
                                if (bitmap != null)
                                {
                                    bitmap.Dispose();
                                }
                                isRGBLock = false;
                            }
                        }
                        else
                        {
                            lock (rectLock)
                            {
                                allRect.left   = 0;
                                allRect.top    = 0;
                                allRect.right  = 0;
                                allRect.bottom = 0;
                            }
                        }
                        isRGBLock = false;
                    }));
                }
            }
        }
Пример #10
0
        /// <summary>
        /// 比对函数,将每一帧抓拍的照片和身份证照片进行比对
        /// </summary>
        /// <param name="bitmap"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private bool CompareImgWithIDImg(Bitmap bitmap, PaintEventArgs e)
        {
            recTimes--;
            if (bitmap == null)
            {
                return(false);
            }
            Graphics g       = e.Graphics;
            float    offsetX = videoSource.Width * 1f / bitmap.Width;
            float    offsetY = videoSource.Height * 1f / bitmap.Height;
            //检测人脸,得到Rect框
            ASF_MultiFaceInfo multiFaceInfo = FaceUtil.DetectFace(pVideoEngine, bitmap);
            //得到最大人脸
            ASF_SingleFaceInfo maxFace = FaceUtil.GetMaxFace(multiFaceInfo);
            //得到Rect
            MRECT rect   = maxFace.faceRect;
            float x      = rect.left * offsetX;
            float width  = rect.right * offsetX - x;
            float y      = rect.top * offsetY;
            float height = rect.bottom * offsetY - y;

            //根据Rect进行画框
            g.DrawRectangle(pen, x, y, width, height);
            //将上一帧检测结果显示到页面上
            g.DrawString(trackUnit.message, font, brush, x, y + 5);
            //保证只检测一帧,防止页面卡顿以及出现其他内存被占用情况
            if (isLock == false)
            {
                isLock = true;
                //异步处理提取特征值和比对,不然页面会比较卡
                ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
                {
                    if (rect.left != 0 && rect.right != 0 && rect.top != 0 && rect.bottom != 0)
                    {
                        try
                        {
                            //提取人脸特征
                            IntPtr feature            = FaceUtil.ExtractFeature(pVideoImageEngine, bitmap, maxFace);
                            float similarity          = CompareTwoFeatures(feature, idCardHelper.idInfo.imageFeature);
                            this.similarity.Text      = ("相似度为: " + similarity.ToString("P"));; //显示在界面上
                            this.similarity.ForeColor = similarity > threshold ? Color.Green : Color.Red;
                            //得到比对结果
                            int result = (CompareTwoFeatures(feature, idCardHelper.idInfo.imageFeature) >= threshold) ? 1 : -1;
                            if (result > -1)
                            {
                                bool isLiveness     = false;
                                ImageInfo imageInfo = ImageUtil.ReadBMP(bitmap); //调整图片数据
                                if (imageInfo == null)
                                {
                                    return;
                                }
                                int retCode_Liveness = -1;
                                //RGB活体检测
                                ASF_LivenessInfo liveInfo = FaceUtil.LivenessInfo_RGB(pVideoImageEngine, imageInfo, multiFaceInfo, out retCode_Liveness);
                                //判断检测结果
                                if (retCode_Liveness == 0 && liveInfo.num > 0)
                                {
                                    int isLive = MemoryUtil.PtrToStructure <int>(liveInfo.isLive);
                                    isLiveness = (isLive == 1) ? true : false;
                                }
                                if (isLiveness)//活体检测成功
                                {
                                    //存放当前人脸识别的相似度
                                    idCardHelper.idInfo.similarity = similarity;
                                    //记录下当前的摄像头的人脸抓拍照
                                    idCardHelper.idInfo.capImage = bitmap;
                                    //验证通过则不再是当前身份证,等待下一次身份证
                                    idCardHelper.idInfo.isRight = false;
                                    //在子线程中输出信息到messageBox
                                    AppendText p = new AppendText(AddTextToMessBox);
                                    lbl_msg.Invoke(p, "人脸验证成功,请取卡...\n");
                                    pass = 1;
                                    idCardHelper.idInfo.isPass = 1;
                                    //将比对结果放到显示消息中,用于最新显示
                                    trackUnit.message = string.Format("通过验证,相似度为{0}", similarity);
                                    FileHelper.DeleteFile(m_strPath); //删除验证过的本地文件
                                    Thread.Sleep(1000);               //延时1秒
                                    this.IDPbox.Image = defaultImage; //照片恢复默认照片
                                    trackUnit.message = "";           //人脸识别框文字置空
                                    setFormResultValue(true);
                                }
                                else
                                {
                                    pass = 0;//标志未通过
                                    trackUnit.message = "未通过,系统识别为照片";
                                    AppendText p      = new AppendText(AddTextToMessBox);
                                    lbl_msg.Invoke(p, "抱歉,您未通过人脸验证...\n");
                                    FileHelper.DeleteFile(m_strPath);//删除验证过的本地文件
                                }
                            }
                            else
                            {
                                pass = 0;//标志未通过
                                trackUnit.message = "未通过人脸验证";
                                AppendText p      = new AppendText(AddTextToMessBox);
                                lbl_msg.Invoke(p, "抱歉,您未通过人脸验证...\n");
                                FileHelper.DeleteFile(m_strPath);//删除验证过的本地文件
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                            FileHelper.DeleteFile(m_strPath);//删除验证过的本地文件
                        }
                        finally
                        {
                            isLock = false;
                        }
                    }
                    isLock = false;
                }));
            }
            return(false);
        }
Пример #11
0
 public static extern MResult ASFGetLivenessScore_IR(IntPtr hEngine, out ASF_LivenessInfo livenessInfo);
Пример #12
0
        private void Huoti(string sfzImg, string paiZhaoImg)
        {
            MessageBox.Show(paiZhaoImg);

            Bitmap bitmap = new Bitmap(paiZhaoImg);

            ASF_MultiFaceInfo multiFaceInfo = IDCardUtil.DetectFace(pVideoEngine, bitmap);

            ASF_SingleFaceInfo2 maxFace = IDCardUtil.GetMaxFace(multiFaceInfo);

            MRECT rect = maxFace.faceRect;

            if (isRGBLock == false)
            {
                isRGBLock = true;

                ThreadPool.QueueUserWorkItem(new WaitCallback(delegate
                {
                    if (rect.left != 0 && rect.right != 0 && rect.top != 0 && rect.bottom != 0)
                    {
                        try
                        {
                            bool isLiveness = false;


                            ImageInfo imageInfo = IDCardUtil.ReadBMP(bitmap);
                            if (imageInfo == null)
                            {
                                return;
                            }
                            int retCode_Liveness = -1;
                            //RGB活体检测
                            ASF_LivenessInfo liveInfo = IDCardUtil.LivenessInfo_RGB(pVideoRGBImageEngine, imageInfo, multiFaceInfo, out retCode_Liveness);

                            if (retCode_Liveness == 0 && liveInfo.num > 0)
                            {
                                int isLive = MemoryUtil.PtrToStructure <int>(liveInfo.isLive);
                                isLiveness = (isLive == 1) ? true : false;
                            }
                            if (imageInfo != null)
                            {
                                MemoryUtil.Free(imageInfo.imgData);
                            }
                            if (isLiveness)
                            {
                                CompareTest(sfzImg, paiZhaoImg);
                            }
                            else
                            {
                                //显示消息
                                MessageBox.Show("假体!");
                                timer.Start();
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                        finally
                        {
                            if (bitmap != null)
                            {
                                bitmap.Dispose();
                            }
                            isRGBLock = false;
                        }
                    }
                    else
                    {
                        lock (rectLock)
                        {
                            allRect.left   = 0;
                            allRect.top    = 0;
                            allRect.right  = 0;
                            allRect.bottom = 0;
                        }
                    }
                    isRGBLock = false;
                }));
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="hadFaceInfo"></param>
        /// <param name="ageInfo"></param>
        /// <param name="genderInfo"></param>
        /// <param name="face3DAngleInfo"></param>
        /// <param name="rgbLiveInfo"></param>
        /// <param name="irLiveInfo"></param>
        /// <param name="maskInfo"></param>
        /// <param name="lanMaskInfo"></param>
        /// <param name="hadRgbLive"></param>
        /// <param name="hadRIrLive"></param>
        /// <param name="i"></param>
        /// <param name="faceInfo"></param>
        private static void SetFaceInfo(bool hadFaceInfo, ASF_AgeInfo ageInfo, ASF_GenderInfo genderInfo, ASF_Face3DAngle face3DAngleInfo, ASF_LivenessInfo rgbLiveInfo
                                        , ASF_LivenessInfo irLiveInfo, ASF_MaskInfo maskInfo, ASF_LandMarkInfo lanMaskInfo, bool hadRgbLive, bool hadRIrLive, int i, FaceInfo faceInfo)
        {
            if (hadFaceInfo)
            {
                if (ageInfo.Num > i)
                {
                    faceInfo.Age = MemoryUtil.PtrToStructure <int>(ageInfo.AgeArray + MemoryUtil.SizeOf <int>() * i);
                }
                else
                {
                    faceInfo.Age = -1;
                }
                if (genderInfo.Num > i)
                {
                    faceInfo.Gender = MemoryUtil.PtrToStructure <int>(genderInfo.GenderArray + MemoryUtil.SizeOf <int>() * i);
                }
                else
                {
                    faceInfo.Gender = -1;
                }

                if (maskInfo.Num > i)
                {
                    faceInfo.Mask = MemoryUtil.PtrToStructure <int>(maskInfo.MaskArray + MemoryUtil.SizeOf <int>() * i);
                }
                else
                {
                    faceInfo.Mask = -1;
                }
                if (lanMaskInfo.Num > i)
                {
                    var faceLandmark = MemoryUtil.PtrToStructure <ASF_FaceLandmark>(lanMaskInfo.Point + MemoryUtil.SizeOf <ASF_FaceLandmark>() * i);
                    faceInfo.FaceLandPoint = new PointF(faceLandmark.X, faceLandmark.Y);
                }
                else
                {
                    faceInfo.FaceLandPoint = new PointF(-1f, -1f);
                }
                if (face3DAngleInfo.Num > i)
                {
                    faceInfo.Face3DAngle.Status = 0;
                    //roll为侧倾角,pitch为俯仰角,yaw为偏航角
                    faceInfo.Face3DAngle.Roll  = MemoryUtil.PtrToStructure <float>(face3DAngleInfo.Roll + MemoryUtil.SizeOf <float>() * i);
                    faceInfo.Face3DAngle.Pitch = MemoryUtil.PtrToStructure <float>(face3DAngleInfo.Pitch + MemoryUtil.SizeOf <float>() * i);
                    faceInfo.Face3DAngle.Yaw   = MemoryUtil.PtrToStructure <float>(face3DAngleInfo.Yaw + MemoryUtil.SizeOf <float>() * i);
                }
                else
                {
                    faceInfo.Face3DAngle.Status = -1;
                }
            }
            else
            {
                faceInfo.Age                = -1;
                faceInfo.Gender             = -1;
                faceInfo.Face3DAngle.Status = -1;
                faceInfo.Mask               = -1;
                faceInfo.FaceLandPoint      = new PointF(-1f, -1f);
            }
            if (hadRgbLive &&
                rgbLiveInfo.Num == 1)
            {
                faceInfo.RgbLive = MemoryUtil.PtrToStructure <int>(rgbLiveInfo.IsLive);
            }
            else
            {
                faceInfo.RgbLive = -2;
            }
            if (hadRIrLive &&
                irLiveInfo.Num == 1)
            {
                faceInfo.IrLive = MemoryUtil.PtrToStructure <int>(irLiveInfo.IsLive);
            }
            else
            {
                faceInfo.IrLive = -2;
            }
        }