Exemplo n.º 1
0
        /// <summary>
        /// 活体检测器。
        /// <para>
        /// 视频帧图片,由 <paramref name="global"/> 指定是否启用全局检测能力 <br />
        /// </para>
        /// <para>如果返回结果为 <see cref="AntiSpoofingStatus.Detecting"/>,则说明需要继续调用此方法,传入更多的图片</para>
        /// </summary>
        /// <param name="viewFace"></param>
        /// <param name="bitmaps">一组图片,即视频帧的 <see cref="Bitmap"/> 数组</param>
        /// <param name="faceIndex">指定要识别的人脸索引</param>
        /// <param name="global">是否启用全局检测能力</param>
        /// <returns></returns>
        public static AntiSpoofingStatus AntiSpoofingVideo(this ViewFace viewFace, Bitmap[] bitmaps, int faceIndex, bool global)
        {
            var  result   = AntiSpoofingStatus.Detecting;
            bool haveFace = false;

            foreach (var bitmap in bitmaps)
            {
                var infos = viewFace.FaceDetector(bitmap);
                if (faceIndex >= 0 && faceIndex < infos.Length)
                {
                    haveFace = true;
                    var points = viewFace.FaceMark(bitmap, infos[faceIndex]);
                    var status = viewFace.AntiSpoofingVideo(bitmap, infos[faceIndex], points, global);
                    if (status == AntiSpoofingStatus.Detecting)
                    {
                        continue;
                    }
                    else
                    {
                        result = status;
                    }
                }
            }
            if (haveFace)
            {
                return(result);
            }
            else
            {
                return(AntiSpoofingStatus.Error);
            }
        }
Exemplo n.º 2
0
        public void TestAntiSpoofingVideo(bool global)
        {
            if (!(Image.FromFile(images[0]) is Bitmap bitmap))
            {
                throw new ArgumentNullException(nameof(bitmap));
            }

            ViewFace viewFace = new ViewFace((str) => { System.Console.WriteLine(str); });
            var      infos    = viewFace.FaceDetector(bitmap);

            if (infos.Length > 0)
            {
                var points = viewFace.FaceMark(bitmap, infos[0]);
                AntiSpoofingStatus status = AntiSpoofingStatus.Error;
                while (true)
                {
                    status = viewFace.AntiSpoofingVideo(bitmap, infos[0], points, global);
                    if (status != AntiSpoofingStatus.Detecting)
                    {
                        break;
                    }
                }
                Console.WriteLine(status);
            }
            else
            {
                Console.WriteLine("No Face!");
            }

            bitmap.Dispose();
        }
Exemplo n.º 3
0
        /// <summary>
        /// 计算人脸特征值相似度。
        /// </summary>
        /// <param name="viewFace"></param>
        /// <param name="lhs"></param>
        /// <param name="rhs"></param>
        /// <returns></returns>
        public static float Compare(this ViewFace viewFace, float[] lhs, float[] rhs)
        {
            float sum = 0;

            for (int i = 0; i < lhs.Length; i++)
            {
                sum += lhs[i] * rhs[i];
            }
            return(sum);
        }
Exemplo n.º 4
0
        static void Main()
        {
            ViewFace viewFace = new ViewFace((str) => { Debug.WriteLine(str); }); // 初始化人脸识别类,并设置 日志回调函数

            viewFace.DetectorSetting = new DetectorSetting()
            {
                FaceSize = 20, MaxWidth = 2000, MaxHeight = 2000, Threshold = 0.5
            };

            // 系统默认使用的轻量级识别模型。如果对精度有要求,请切换到 Normal 模式;并下载需要模型文件 放入生成目录的 model 文件夹中
            viewFace.FaceType = FaceType.Normal;
            // 系统默认使用5个人脸关键点。//不建议改动,除非是使用口罩模型。
            viewFace.MarkType = MarkType.Light;

            #region 识别老照片
            float[] oldEigenValues;
            Bitmap  oldImg   = (Bitmap)Image.FromFile(@"C:\Users\yangw\OneDrive\图片\Camera Roll\IMG_20181103_142707.jpg" /*老图片路径*/); // 从文件中加载照片 // 或者视频帧等
            var     oldFaces = viewFace.FaceDetector(oldImg);                                                                       // 检测图片中包含的人脸信息。(置信度、位置、大小)
            if (oldFaces.Length > 0)                                                                                                //识别到人脸
            {
                {                                                                                                                   // 打印人脸信息
                    Console.WriteLine($"识别到的人脸数量:{oldFaces.Length} 。人脸信息:\n");
                    Console.WriteLine($"序号\t人脸置信度\t位置X\t位置Y\t宽度\t高度");
                    for (int i = 0; i < oldFaces.Length; i++)
                    {
                        Console.WriteLine($"{i + 1}\t{oldFaces[i].Score}\t{oldFaces[i].Location.X}\t{oldFaces[i].Location.Y}\t{oldFaces[i].Location.Width}\t{oldFaces[i].Location.Height}");
                    }
                    Console.WriteLine();
                }
                var oldPoints = viewFace.FaceMark(oldImg, oldFaces[0]); // 获取 第一个人脸 的识别关键点。(人脸识别的关键点数据)
                oldEigenValues = viewFace.Extract(oldImg, oldPoints);   // 获取 指定的关键点 的特征值。
            }
            else
            {
                oldEigenValues = new float[0];    /*未识别到人脸*/
            }
            #endregion

            #region 识别新照片
            float[] newEigenValues;
            Bitmap  newImg   = (Bitmap)Image.FromFile(@"C:\Users\yangw\OneDrive\图片\Camera Roll\IMG_20181129_224339.jpg" /*新图片路径*/); // 从文件中加载照片 // 或者视频帧等
            var     newFaces = viewFace.FaceDetector(newImg);                                                                       // 检测图片中包含的人脸信息。(置信度、位置、大小)
            if (newFaces.Length > 0)                                                                                                //识别到人脸
            {
                {                                                                                                                   // 打印人脸信息
                    Console.WriteLine($"识别到的人脸数量:{newFaces.Length} 。人脸信息:\n");
                    Console.WriteLine($"序号\t人脸置信度\t位置X\t位置Y\t宽度\t高度");
                    for (int i = 0; i < newFaces.Length; i++)
                    {
                        Console.WriteLine($"{i + 1}\t{newFaces[i].Score}\t{newFaces[i].Location.X}\t{newFaces[i].Location.Y}\t{newFaces[i].Location.Width}\t{newFaces[i].Location.Height}");
                    }
                    Console.WriteLine();
                }
                var newPoints = viewFace.FaceMark(newImg, newFaces[0]); // 获取 第一个人脸 的识别关键点。(人脸识别的关键点数据)
                newEigenValues = viewFace.Extract(newImg, newPoints);   // 获取 指定的关键点 的特征值。
            }
            else
            {
                newEigenValues = new float[0];    /*未识别到人脸*/
            }
            #endregion

            try
            {
                float similarity = viewFace.Similarity(oldEigenValues, newEigenValues); // 对比两张照片上的数据,确认是否是同一个人。
                Console.WriteLine($"阈值 = {Face.Threshold[viewFace.FaceType]}\t相似度 = {similarity}");
                Console.WriteLine($"是否是同一个人:{viewFace.IsSelf(similarity)}");
            }
            catch (Exception e)
            { Console.WriteLine(e); }

            Console.ReadKey();
        }
Exemplo n.º 5
0
        static void Main()
        {
            // 老图片路径
            string oldImgPath = @"C:/Users/yangw/OneDrive/桌面/image/0.jpg";
            string newImgPath = @"C:/Users/yangw/OneDrive/桌面/image/1.jpg";

            ViewFace viewFace = new ViewFace((str) => { Debug.WriteLine(str); }); // 初始化人脸识别类,并设置 日志回调函数

            // 系统默认使用的轻量级识别模型。如果对精度有要求,请切换到 Normal 模式;并下载需要模型文件 放入生成目录的 model 文件夹中
            viewFace.FaceType = FaceType.Normal;
            // 系统默认使用5个人脸关键点。//不建议改动,除非是使用口罩模型。
            viewFace.MarkType = MarkType.Light;

            #region 识别老照片

            float[] oldEigenValues;
            Bitmap  oldImg = (Bitmap)Image.FromFile(oldImgPath);                        // 从文件中加载照片 // 或者视频帧等

            var oldFaceInfos = viewFace.FaceDetector(oldImg);                           // 检测图片中包含的人脸信息。(置信度、位置、大小)
            oldFaceInfos = viewFace.FaceDetectorAsync(oldImg).GetAwaiter().GetResult(); // 异步检测图片中的人脸信息

            if (oldFaceInfos.Length > 0)                                                //识别到人脸
            {
                {                                                                       // 打印人脸信息
                    Console.WriteLine($"识别到的人脸数量:{oldFaceInfos.Length} 。人脸信息:\n");
                    Console.WriteLine($"序号\t人脸置信度\t位置X\t位置Y\t宽度\t高度");
                    for (int i = 0; i < oldFaceInfos.Length; i++)
                    {
                        Console.WriteLine($"{i + 1}\t{oldFaceInfos[i].Score:f8}\t{oldFaceInfos[i].Location.X}\t{oldFaceInfos[i].Location.Y}\t{oldFaceInfos[i].Location.Width}\t{oldFaceInfos[i].Location.Height}");
                    }
                    Console.WriteLine();
                }
                var oldPoints = viewFace.FaceMark(oldImg, oldFaceInfos[0]); // 获取 第一个人脸 的识别关键点。(人脸识别的关键点数据)
                oldEigenValues = viewFace.Extract(oldImg, oldPoints);       // 获取 指定的关键点 的特征值。
                var state = viewFace.AntiSpoofing(oldImg, oldFaceInfos[0], oldPoints);
                Console.WriteLine($"单帧 活体检测结果:{state}");
                state = viewFace.AntiSpoofingVideo(oldImg, oldFaceInfos[0], oldPoints);
                Console.WriteLine($"视频 活体检测结果:{state}");

                Console.WriteLine();
                #region 质量评估
                var qualityBrightness = viewFace.FaceQuality(oldImg, oldFaceInfos[0], oldPoints, QualityType.Brightness);
                Console.WriteLine($"亮度:Level [{qualityBrightness.Level}] - Score [{qualityBrightness.Score}]");
                var qualityClarity = viewFace.FaceQuality(oldImg, oldFaceInfos[0], oldPoints, QualityType.Clarity);
                Console.WriteLine($"清晰度:Level [{qualityClarity.Level}] - Score [{qualityClarity.Score}]");
                var qualityIntegrity = viewFace.FaceQuality(oldImg, oldFaceInfos[0], oldPoints, QualityType.Integrity);
                Console.WriteLine($"完整度:Level [{qualityIntegrity.Level}] - Score [{qualityIntegrity.Score}]");
                var qualityPose = viewFace.FaceQuality(oldImg, oldFaceInfos[0], oldPoints, QualityType.Pose);
                Console.WriteLine($"姿态:Level [{qualityPose.Level}] - Score [{qualityPose.Score}]");
                var qualityPoseEx = viewFace.FaceQuality(oldImg, oldFaceInfos[0], oldPoints, QualityType.PoseEx);
                Console.WriteLine($"姿态 (深度):Level [{qualityPoseEx.Level}] - Score [{qualityPoseEx.Score}]");
                var qualityResolution = viewFace.FaceQuality(oldImg, oldFaceInfos[0], oldPoints, QualityType.Resolution);
                Console.WriteLine($"分辨率:Level [{qualityResolution.Level}] - Score [{qualityResolution.Score}]");
                var qualityStructure = viewFace.FaceQuality(oldImg, oldFaceInfos[0], oldPoints, QualityType.Structure);
                Console.WriteLine($"遮挡:Level [{qualityStructure.Level}] - Score [{qualityStructure.Score}]");
                var qualityClarityEx = viewFace.FaceQuality(oldImg, oldFaceInfos[0], oldPoints, QualityType.ClarityEx);
                Console.WriteLine($"清晰度 (深度):Level [{qualityClarityEx.Level}] - Score [{qualityClarityEx.Score}]");

                #endregion
            }
            else
            {
                oldEigenValues = new float[0];    /*未识别到人脸*/
            }
            #endregion
            Console.WriteLine();
            #region 人脸追踪
            var trackFaceInfos = viewFace.FaceTrack(oldImg); // 人脸追踪
            if (trackFaceInfos.Length > 0)
            {
                { // 打印人脸信息
                    Console.WriteLine($"\r\n跟踪到的人脸数量:{trackFaceInfos.Length} 。人脸信息:\n");
                    Console.WriteLine($"PID\t人脸置信度\t位置X\t位置Y\t宽度\t高度");
                    for (int i = 0; i < trackFaceInfos.Length; i++)
                    {
                        Console.WriteLine($"{trackFaceInfos[i].Pid}\t{trackFaceInfos[i].Score:f8}\t{trackFaceInfos[i].Location.X}\t{trackFaceInfos[i].Location.Y}\t{trackFaceInfos[i].Location.Width}\t{trackFaceInfos[i].Location.Height}");
                    }
                }
            }
            #endregion
            Console.WriteLine();
            #region 识别新照片
            float[] newEigenValues;
            Bitmap  newImg = (Bitmap)Image.FromFile(newImgPath /*新图片路径*/); // 从文件中加载照片 // 或者视频帧等
            newImg = (Bitmap)newImg.ChangeSize(new Size(1024, 768));
            var newFaces = viewFace.FaceDetector(newImg);                  // 检测图片中包含的人脸信息。(置信度、位置、大小)
            if (newFaces.Length > 0)                                       //识别到人脸
            {
                {                                                          // 打印人脸信息
                    Console.WriteLine($"识别到的人脸数量:{newFaces.Length} 。人脸信息:\n");
                    Console.WriteLine($"序号\t人脸置信度\t位置X\t位置Y\t宽度\t高度");
                    for (int i = 0; i < newFaces.Length; i++)
                    {
                        Console.WriteLine($"{i + 1}\t{newFaces[i].Score:f8}\t{newFaces[i].Location.X}\t{newFaces[i].Location.Y}\t{newFaces[i].Location.Width}\t{newFaces[i].Location.Height}");
                    }
                }
                var newPoints = viewFace.FaceMark(newImg, newFaces[0]); // 获取 第一个人脸 的识别关键点。(人脸识别的关键点数据)
                newEigenValues = viewFace.Extract(newImg, newPoints);   // 获取 指定的关键点 的特征值。
            }
            else
            {
                newEigenValues = new float[0];    /*未识别到人脸*/
            }
            #endregion
            Console.WriteLine();
            try
            {
                float similarity = viewFace.Similarity(oldEigenValues, newEigenValues); // 对比两张照片上的数据,确认是否是同一个人。
                Console.WriteLine($"阈值 = {FaceCompareConfig.GetThreshold(viewFace.FaceType)}\t相似度 = {similarity}");
                Console.WriteLine($"是否是同一个人:{viewFace.IsSelf(similarity)}");
            }
            catch (Exception e)
            { Console.WriteLine(e.Message); }

            Console.ReadKey();
        }