Exemple #1
0
        /// <summary>
        /// 单人脸年龄检测
        /// </summary>
        /// <param name="pEngine">人脸识别引擎</param>
        /// <param name="image">图片</param>
        /// <param name="singleFaceInfo">单人脸信息</param>
        /// <returns>年龄检测结果</returns>
        public static ASF_AgeInfo AgeEstimation(IntPtr pEngine, Image image, ASF_SingleFaceInfo singleFaceInfo)
        {
            ImageInfo         imageInfo     = ImageUtil.ReadBMP(image);
            ASF_MultiFaceInfo multiFaceInfo = new ASF_MultiFaceInfo();

            multiFaceInfo.faceRects = MemoryUtil.Malloc(MemoryUtil.SizeOf <MRECT>());
            MemoryUtil.StructureToPtr <MRECT>(singleFaceInfo.faceRect, multiFaceInfo.faceRects);
            multiFaceInfo.faceOrients = MemoryUtil.Malloc(MemoryUtil.SizeOf <int>());
            MemoryUtil.StructureToPtr <int>(singleFaceInfo.faceOrient, multiFaceInfo.faceOrients);
            multiFaceInfo.faceNum = 1;
            ASF_AgeInfo ageInfo = AgeEstimation(pEngine, imageInfo, multiFaceInfo);

            MemoryUtil.Free(imageInfo.imgData);
            return(ageInfo);
        }
        /// <summary>
        /// 单人脸年龄检测
        /// </summary>
        /// <param name="pEngine">人脸识别引擎</param>
        /// <param name="image">图片</param>
        /// <param name="singleFaceInfo">单人脸信息</param>
        /// <returns>年龄检测结果</returns>
        public static ASF_AgeInfo DetectAge(IntPtr pEngine, Image image, ASF_SingleFaceInfo singleFaceInfo)
        {
            ImageInfo imageInfo = UtilTool.ReadImage(image);

            if (imageInfo == null)
            {
                return(new ASF_AgeInfo());
            }
            ASF_MultiFaceInfo multiFaceInfo = new ASF_MultiFaceInfo();

            multiFaceInfo.faceRects = UtilToolMemory.Malloc(UtilToolMemory.SizeOf <MRECT>());
            UtilToolMemory.StructureToPtr <MRECT>(singleFaceInfo.faceRect, multiFaceInfo.faceRects);
            multiFaceInfo.faceOrients = UtilToolMemory.Malloc(UtilToolMemory.SizeOf <int>());
            UtilToolMemory.StructureToPtr <int>(singleFaceInfo.faceOrient, multiFaceInfo.faceOrients);
            multiFaceInfo.faceNum = 1;
            ASF_AgeInfo ageInfo = DetectAge(pEngine, imageInfo, multiFaceInfo);

            UtilToolMemory.Free(imageInfo.imgData);
            return(ageInfo);
        }
Exemple #3
0
        /// <summary>
        /// 获取人脸信息列表
        /// </summary>
        /// <param name="pEngine"></param>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        public static List <FaceInfoModel> GetFaceInfos(IntPtr pEngine, Image bitmap)
        {
            List <FaceInfoModel> listRet = new List <FaceInfoModel>();

            try
            {
                List <int> AgeList    = new List <int>();
                List <int> GenderList = new List <int>();
                //检测人脸,得到Rect框
                ASF_MultiFaceInfo multiFaceInfo  = FaceUtil.DetectFace(pEngine, bitmap);
                MultiFaceModel    multiFaceModel = new MultiFaceModel(multiFaceInfo);
                //人脸信息处理
                ImageInfo imageInfo = ImageUtil.ReadBMP(bitmap);
                int       retCode   = ASFFunctions.ASFProcess(pEngine, imageInfo.width, imageInfo.height, imageInfo.format, imageInfo.imgData, ref multiFaceInfo, FaceEngineMask.ASF_AGE | FaceEngineMask.ASF_GENDER);
                //获取年龄信息
                ASF_AgeInfo ageInfo = new ASF_AgeInfo();
                retCode = ASFFunctions.ASFGetAge(pEngine, ref ageInfo);
                AgeList = ageInfo.PtrToAgeArray(ageInfo.ageArray, ageInfo.num);
                //获取性别信息
                ASF_GenderInfo genderInfo = new ASF_GenderInfo();
                retCode    = ASFFunctions.ASFGetGender(pEngine, ref genderInfo);
                GenderList = genderInfo.PtrToGenderArray(genderInfo.genderArray, genderInfo.num);

                for (int i = 0; i < multiFaceInfo.faceNum; i++)
                {
                    FaceInfoModel faceInfo = new FaceInfoModel();
                    faceInfo.age        = AgeList[i];
                    faceInfo.gender     = GenderList[i];
                    faceInfo.faceRect   = multiFaceModel.FaceInfoList[i].faceRect;
                    faceInfo.feature    = ExtractFeature(pEngine, bitmap, multiFaceModel.FaceInfoList[i]);//提取单人脸特征
                    faceInfo.faceOrient = multiFaceModel.FaceInfoList[i].faceOrient;
                    listRet.Add(faceInfo);
                }
                return(listRet);//返回多人脸信息
            }
            catch {
                return(listRet);
            }
        }
        /// <summary>
        /// 获取年龄结果
        /// </summary>
        /// <param name="pEngine">引擎handle</param>
        /// <param name="ageInfo">out 年龄结果</param>
        /// <returns>返回0表示正常;返回负数请根据ErrorCodeUtil类注释查看;其他值请在官网-帮助中心查询</returns>
        public int ASFGetAge(out AgeInfo ageInfo)
        {
            ageInfo = new AgeInfo();
            IntPtr pAgeInfo = MemoryUtil.Malloc(MemoryUtil.SizeOf <ASF_AgeInfo>());
            //调用SDK接口
            int retCode = ASFFunctions.ASFGetAge(pEngine, pAgeInfo);

            if (retCode != 0)
            {
                MemoryUtil.Free(pAgeInfo);
                return(retCode);
            }
            //转化结果
            ASF_AgeInfo asfAgeInfo = MemoryUtil.PtrToStructure <ASF_AgeInfo>(pAgeInfo);

            ageInfo.num = asfAgeInfo.num;
            if (ageInfo.num > 0)
            {
                ageInfo.ageArray = new int[ageInfo.num];
                Marshal.Copy(asfAgeInfo.ageArray, ageInfo.ageArray, 0, ageInfo.num);
            }
            MemoryUtil.FreeArray(pAgeInfo);
            return(retCode);
        }
 /// <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);
 }
        /// <summary>
        /// “选择识别图片”按钮事件
        /// </summary>
        private void ChooseImg(object sender, EventArgs e)
        {
            lblCompareInfo.Text = "";
            if (pEngine == IntPtr.Zero)
            {
                chooseMultiImgBtn.Enabled = false;
                matchBtn.Enabled          = false;
                btnClearFaceList.Enabled  = false;
                chooseImgBtn.Enabled      = false;
                MessageBox.Show("请先初始化引擎!");
                return;
            }
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Title       = "选择图片";
            openFileDialog.Filter      = "图片文件|*.bmp;*.jpg;*.jpeg;*.png";
            openFileDialog.Multiselect = false;
            openFileDialog.FileName    = string.Empty;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                DateTime detectStartTime = DateTime.Now;
                AppendText(string.Format("------------------------------开始检测,时间:{0}------------------------------\n", detectStartTime.ToString("yyyy-MM-dd HH:mm:ss:ms")));
                image1Path = openFileDialog.FileName;

                //获取文件,拒绝过大的图片
                FileInfo fileInfo = new FileInfo(image1Path);
                long     maxSize  = 1024 * 1024 * 2;
                if (fileInfo.Length > maxSize)
                {
                    MessageBox.Show("图像文件最大为2MB,请压缩后再导入!");
                    AppendText(string.Format("------------------------------检测结束,时间:{0}------------------------------\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms")));
                    AppendText("\n");
                    return;
                }

                Image srcImage = Image.FromFile(image1Path);
                //调整图像宽度,需要宽度为4的倍数
                srcImage = ImageUtil.ScaleImage(srcImage, picImageCompare.Width, picImageCompare.Height);
                //调整图片数据,非常重要
                ImageInfo imageInfo = ImageUtil.ReadBMP(srcImage);
                //人脸检测
                ASF_MultiFaceInfo multiFaceInfo = FaceUtil.DetectFace(pEngine, imageInfo);
                //年龄检测
                int         retCode_Age = -1;
                ASF_AgeInfo ageInfo     = FaceUtil.AgeEstimation(pEngine, imageInfo, multiFaceInfo, out retCode_Age);
                //性别检测
                int            retCode_Gender = -1;
                ASF_GenderInfo genderInfo     = FaceUtil.GenderEstimation(pEngine, imageInfo, multiFaceInfo, out retCode_Gender);

                //3DAngle检测
                int             retCode_3DAngle = -1;
                ASF_Face3DAngle face3DAngleInfo = FaceUtil.Face3DAngleDetection(pEngine, imageInfo, multiFaceInfo, out retCode_3DAngle);

                MemoryUtil.Free(imageInfo.imgData);

                if (multiFaceInfo.faceNum < 1)
                {
                    image1Feature         = IntPtr.Zero;
                    picImageCompare.Image = srcImage;
                    AppendText(string.Format("{0} - 未检测出人脸!\n\n", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
                    AppendText(string.Format("------------------------------检测结束,时间:{0}------------------------------\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms")));
                    AppendText("\n");
                    return;
                }

                MRECT temp       = new MRECT();
                int   ageTemp    = 0;
                int   genderTemp = 0;
                int   rectTemp   = 0;

                //标记出检测到的人脸
                for (int i = 0; i < multiFaceInfo.faceNum; i++)
                {
                    MRECT rect   = MemoryUtil.PtrToStructure <MRECT>(multiFaceInfo.faceRects + MemoryUtil.SizeOf <MRECT>() * i);
                    int   orient = MemoryUtil.PtrToStructure <int>(multiFaceInfo.faceOrients + MemoryUtil.SizeOf <int>() * i);
                    int   age    = 0;

                    if (retCode_Age != 0)
                    {
                        AppendText(string.Format("年龄检测失败,返回{0}!\n\n", retCode_Age));
                    }
                    else
                    {
                        age = MemoryUtil.PtrToStructure <int>(ageInfo.ageArray + MemoryUtil.SizeOf <int>() * i);
                    }

                    int gender = -1;
                    if (retCode_Gender != 0)
                    {
                        AppendText(string.Format("性别检测失败,返回{0}!\n\n", retCode_Gender));
                    }
                    else
                    {
                        gender = MemoryUtil.PtrToStructure <int>(genderInfo.genderArray + MemoryUtil.SizeOf <int>() * i);
                    }


                    int   face3DStatus = -1;
                    float roll         = 0f;
                    float pitch        = 0f;
                    float yaw          = 0f;
                    if (retCode_3DAngle != 0)
                    {
                        AppendText(string.Format("3DAngle检测失败,返回{0}!\n\n", retCode_3DAngle));
                    }
                    else
                    {
                        //角度状态 非0表示人脸不可信
                        face3DStatus = MemoryUtil.PtrToStructure <int>(face3DAngleInfo.status + MemoryUtil.SizeOf <int>() * i);
                        //roll为侧倾角,pitch为俯仰角,yaw为偏航角
                        roll  = MemoryUtil.PtrToStructure <float>(face3DAngleInfo.roll + MemoryUtil.SizeOf <float>() * i);
                        pitch = MemoryUtil.PtrToStructure <float>(face3DAngleInfo.pitch + MemoryUtil.SizeOf <float>() * i);
                        yaw   = MemoryUtil.PtrToStructure <float>(face3DAngleInfo.yaw + MemoryUtil.SizeOf <float>() * i);
                    }


                    int rectWidth  = rect.right - rect.left;
                    int rectHeight = rect.bottom - rect.top;

                    //查找最大人脸
                    if (rectWidth * rectHeight > rectTemp)
                    {
                        rectTemp   = rectWidth * rectHeight;
                        temp       = rect;
                        ageTemp    = age;
                        genderTemp = gender;
                    }

                    //srcImage = ImageUtil.MarkRectAndString(srcImage, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, age, gender);
                    AppendText(string.Format("{0} - 人脸坐标:[left:{1},top:{2},right:{3},bottom:{4},orient:{5},roll:{6},pitch:{7},yaw:{8},status:{11}] Age:{9} Gender:{10}\n", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), rect.left, rect.top, rect.right, rect.bottom, orient, roll, pitch, yaw, age, (gender >= 0 ? gender.ToString() : ""), face3DStatus));
                }

                srcImage = ImageUtil.MarkRectAndString(srcImage, temp.left, temp.top, temp.right - temp.left, temp.bottom - temp.top, ageTemp, genderTemp);


                AppendText(string.Format("{0} - 人脸数量:{1}\n\n", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), multiFaceInfo.faceNum));

                //显示标记后的图像
                picImageCompare.Image = srcImage;
                DateTime detectEndTime = DateTime.Now;
                AppendText(string.Format("------------------------------检测结束,时间:{0}------------------------------\n", detectEndTime.ToString("yyyy-MM-dd HH:mm:ss:ms")));
                AppendText("\n");
                ASF_SingleFaceInfo singleFaceInfo = new ASF_SingleFaceInfo();
                //提取人脸特征
                image1Feature = FaceUtil.ExtractFeature(pEngine, srcImage, out singleFaceInfo);

                //清空上次的匹配结果
                for (int i = 0; i < imagesFeatureList.Count; i++)
                {
                    imageList.Items[i].Text = string.Format("{0}号", i);
                }
            }
        }
Exemple #7
0
 public static extern int ASFGetAge(IntPtr pEngine, ref ASF_AgeInfo ageInfo);
Exemple #8
0
        /// <summary>
        /// 为注册会员检测人脸
        /// </summary>
        /// <param name="transImageInfo"></param>
        /// <returns></returns>
        public MError DeteceForMemberEnroll(
            out string errMsg,
            out IntPtr ptrImgFeature,
            out ASF_SingleFaceInfo singleFaceInfo,
            out int face3DStatus,
            out int faceNum,
            out string faceDesc,
            ref Image srcImage,
            int imageContainerWidth,
            int imageContainerHeight)
        {
            faceNum       = 0;
            face3DStatus  = -1;
            faceDesc      = string.Empty;
            errMsg        = string.Empty;
            ptrImgFeature = IntPtr.Zero;
            StringBuilder sbErrMsg   = new StringBuilder();
            var           retCode    = MError.MERR_UNKNOWN;
            int           retAge     = -1;
            int           retGender  = -1;
            int           ret3DAngle = -1;

            singleFaceInfo = new ASF_SingleFaceInfo();
            try
            {
                if (ptrImageEngine == IntPtr.Zero)
                {
                    retCode = InitialEngineForImage();
                }
                else
                {
                    retCode = MError.MOK;
                }

                if (retCode == MError.MOK)
                {
                    //调整图像宽度,需要宽度为4的倍数
                    if (srcImage.Width % 4 != 0)
                    {
                        //srcImage = ImageHelper.ScaleImage(srcImage, picImageCompare.Width, picImageCompare.Height);
                        srcImage = ImageHelper.ScaleImage(srcImage, srcImage.Width - (srcImage.Width % 4), srcImage.Height);
                    }
                    //调整图片数据,非常重要
                    ImageInfo transImageInfo = ImageHelper.ReadBMP(srcImage);

                    //人脸检测
                    ASF_MultiFaceInfo multiFaceInfo = FaceProcessHelper.DetectFace(ptrImageEngine, transImageInfo);
                    faceNum = multiFaceInfo.faceNum;
                    if (multiFaceInfo.faceNum != 1)
                    {
                        retCode = MError.MERR_ASF_EX_INVALID_FACE_INFO;
                        sbErrMsg.Append(multiFaceInfo.faceNum > 1 ? "检测到多张人脸" : "未检测到人脸");
                    }
                    else
                    {
                        ASF_AgeInfo ageInfo = FaceProcessHelper.AgeEstimation(ptrImageEngine, transImageInfo, multiFaceInfo, out retAge);

                        ASF_GenderInfo genderInfo = FaceProcessHelper.GenderEstimation(ptrImageEngine, transImageInfo, multiFaceInfo, out retGender);

                        ASF_Face3DAngle face3DAngleInfo = FaceProcessHelper.Face3DAngleDetection(ptrImageEngine, transImageInfo, multiFaceInfo, out ret3DAngle);

                        #region 标记出检测到的人脸

                        MRECT temp       = new MRECT();
                        int   ageTemp    = 0;
                        int   genderTemp = 0;
                        int   rectTemp   = 0;

                        //标记出检测到的人脸
                        for (int i = 0; i < multiFaceInfo.faceNum; i++)
                        {
                            MRECT rect   = MemoryHelper.PtrToStructure <MRECT>(multiFaceInfo.faceRects + MemoryHelper.SizeOf <MRECT>() * i);
                            int   orient = MemoryHelper.PtrToStructure <int>(multiFaceInfo.faceOrients + MemoryHelper.SizeOf <int>() * i);
                            int   age    = 0;

                            if (retAge != 0)
                            {
                                sbErrMsg.AppendLine($"年龄检测失败,返回{retAge}!");
                            }
                            else
                            {
                                age = MemoryHelper.PtrToStructure <int>(ageInfo.ageArray + MemoryHelper.SizeOf <int>() * i);
                            }

                            int gender = -1;
                            if (retGender != 0)
                            {
                                sbErrMsg.AppendLine($"性别检测失败,返回{retGender}!");
                            }
                            else
                            {
                                gender = MemoryHelper.PtrToStructure <int>(genderInfo.genderArray + MemoryHelper.SizeOf <int>() * i);
                            }
                            //int face3DStatus = -1;
                            float roll  = 0f;
                            float pitch = 0f;
                            float yaw   = 0f;
                            if (ret3DAngle != 0)
                            {
                                sbErrMsg.AppendLine($"3DAngle检测失败,返回{ret3DAngle}!");
                            }
                            else
                            {
                                //角度状态 非0表示人脸不可信
                                face3DStatus = MemoryHelper.PtrToStructure <int>(face3DAngleInfo.status + MemoryHelper.SizeOf <int>() * i);
                                //roll为侧倾角,pitch为俯仰角,yaw为偏航角
                                roll  = MemoryHelper.PtrToStructure <float>(face3DAngleInfo.roll + MemoryHelper.SizeOf <float>() * i);
                                pitch = MemoryHelper.PtrToStructure <float>(face3DAngleInfo.pitch + MemoryHelper.SizeOf <float>() * i);
                                yaw   = MemoryHelper.PtrToStructure <float>(face3DAngleInfo.yaw + MemoryHelper.SizeOf <float>() * i);
                            }

                            int rectWidth  = rect.right - rect.left;
                            int rectHeight = rect.bottom - rect.top;

                            //查找最大人脸
                            if (rectWidth * rectHeight > rectTemp)
                            {
                                rectTemp   = rectWidth * rectHeight;
                                temp       = rect;
                                ageTemp    = age;
                                genderTemp = gender;
                            }

                            //srcImage = ImageHelper.MarkRectAndString(srcImage, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, age, gender);
                            faceDesc = $"{DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")} - 人脸坐标:[left:{rect.left},top:{rect.top},right:{rect.right},bottom:{rect.bottom},orient:{orient},roll:{roll},pitch:{pitch},yaw:{yaw},status:{face3DStatus}] Age:{age} Gender:{(gender >= 0 ? gender.ToString() : "")}\n";
                        }
                        //提取人脸特征
                        ptrImgFeature = FaceProcessHelper.ExtractFeature(ptrImageEngine, srcImage, out singleFaceInfo);

                        float scaleRate = ImageHelper.GetWidthAndHeight(srcImage.Width, srcImage.Height, imageContainerWidth, imageContainerHeight);
                        srcImage = ImageHelper.ScaleImage(srcImage, imageContainerWidth, imageContainerHeight);
                        srcImage = ImageHelper.MarkRectAndString(srcImage, (int)(temp.left * scaleRate), (int)(temp.top * scaleRate), (int)(temp.right * scaleRate) - (int)(temp.left * scaleRate), (int)(temp.bottom * scaleRate) - (int)(temp.top * scaleRate), ageTemp, genderTemp, imageContainerWidth);

                        #endregion
                    }
                    MemoryHelper.Free(transImageInfo.imgData);
                }

                errMsg = sbErrMsg.ToString();
            }
            catch (Exception ex)
            {
                Afw.Core.Helper.SimplifiedLogHelper.WriteIntoSystemLog(nameof(Afw.WinForm.EngineContext), $"DeteceForMemberEnroll Exception:{ex.ToString()}");
            }

            return(retCode);
        }
Exemple #9
0
 public static extern MResult ASFGetAge(IntPtr hEngine, out ASF_AgeInfo ageInfo);
Exemple #10
0
        /// <summary>
        /// “选择图片识别”
        /// </summary>
        public void ChooseImgeDistinguish(bool IsOffline)
        {
            //判断引擎是否初始化成功
            if (pImageEngine == IntPtr.Zero)
            {
                //禁用相关功能按钮
                FRC.MainVM.IsSIIBtnEnable = false;
                FRC.MainVM.IsPGIBtnEnable = false;
                //ControlsEnable(false, chooseMultiImgBtn, matchBtn, btnClearFaceList, chooseImgBtn);
                MessageBox.Show("请先初始化引擎!");
                return;
            }

            List <string> imagePathList = ImportImages(IsOffline, false);//单张照片

            if (imagePathList.Count <= 0)
            {
                return;
            }
            string selectedImagePath = imagePathList[0];

            DateTime detectStartTime = DateTime.Now;

            AppendText(string.Format("------------------------------开始检测,时间:{0}------------------------------\n", detectStartTime.ToString("yyyy-MM-dd HH:mm:ss:ms")));

            //获取文件,拒绝过大的图片
            FileInfo fileInfo = new FileInfo(selectedImagePath);

            if (fileInfo.Length > MaxSize)
            {
                MessageBox.Show("图像文件最大为2MB,请压缩后再导入!");
                AppendText(string.Format("------------------------------检测结束,时间:{0}------------------------------\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms")));
                AppendText("\n");
                return;
            }

            Image srcImage = UtilTool.ReadImageFromFile(selectedImagePath);

            if (srcImage == null)
            {
                MessageBox.Show("图像数据获取失败,请稍后重试!");
                AppendText(string.Format("------------------------------检测结束,时间:{0}------------------------------\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms")));
                AppendText("\n");
                return;
            }
            if (srcImage.Width > 1536 || srcImage.Height > 1536)
            {
                srcImage = UtilTool.ScaleImage(srcImage, 1536, 1536);
            }
            if (srcImage == null)
            {
                MessageBox.Show("图像数据获取失败,请稍后重试!");
                AppendText(string.Format("------------------------------检测结束,时间:{0}------------------------------\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms")));
                AppendText("\n");
                return;
            }
            //调整图像宽度,需要宽度为4的倍数
            if (srcImage.Width % 4 != 0)
            {
                srcImage = UtilTool.ScaleImage(srcImage, srcImage.Width - (srcImage.Width % 4), srcImage.Height);
            }
            //调整图片数据,非常重要
            ImageInfo imageInfo = UtilTool.ReadImage(srcImage);

            if (imageInfo == null)
            {
                MessageBox.Show("图像数据获取失败,请稍后重试!");
                AppendText(string.Format("------------------------------检测结束,时间:{0}------------------------------\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms")));
                AppendText("\n");
                return;
            }
            //人脸检测
            ASF_MultiFaceInfo multiFaceInfo = UtilToolFace.DetectFace(pImageEngine, imageInfo);
            //年龄检测
            int         retCode_Age = -1;
            ASF_AgeInfo ageInfo     = UtilToolFace.DetectAge(pImageEngine, imageInfo, multiFaceInfo, out retCode_Age);
            //性别检测
            int            retCode_Gender = -1;
            ASF_GenderInfo genderInfo     = UtilToolFace.DetectGender(pImageEngine, imageInfo, multiFaceInfo, out retCode_Gender);

            //3DAngle检测
            int             retCode_3DAngle = -1;
            ASF_Face3DAngle face3DAngleInfo = UtilToolFace.DetectFace3DAngle(pImageEngine, imageInfo, multiFaceInfo, out retCode_3DAngle);

            UtilToolMemory.Free(imageInfo.imgData);

            if (multiFaceInfo.faceNum < 1)
            {
                srcImage     = UtilTool.ScaleImage(srcImage, (int)FRC.FIView._faceIdentificationImageBox.Width, (int)FRC.FIView._faceIdentificationImageBox.Height);
                ImageFeature = IntPtr.Zero;
                FRC.FIView._faceIdentificationImageBox.Source = UtilTool.BitmapToBitmapImage(srcImage);
                AppendText(string.Format("{0} - 未检测出人脸!\n\n", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
                AppendText(string.Format("------------------------------检测结束,时间:{0}------------------------------\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms")));
                AppendText("\n");
                return;
            }

            MRECT temp       = new MRECT();
            int   ageTemp    = 0;
            int   genderTemp = 0;
            int   rectTemp   = 0;

            //标记出检测到的人脸
            for (int i = 0; i < multiFaceInfo.faceNum; i++)
            {
                MRECT rect   = UtilToolMemory.PtrToStructure <MRECT>(multiFaceInfo.faceRects + UtilToolMemory.SizeOf <MRECT>() * i);
                int   orient = UtilToolMemory.PtrToStructure <int>(multiFaceInfo.faceOrients + UtilToolMemory.SizeOf <int>() * i);
                int   age    = 0;

                if (retCode_Age != 0)
                {
                    AppendText(string.Format("年龄检测失败,返回{0}!\n\n", retCode_Age));
                }
                else
                {
                    age = UtilToolMemory.PtrToStructure <int>(ageInfo.ageArray + UtilToolMemory.SizeOf <int>() * i);
                }

                int gender = -1;
                if (retCode_Gender != 0)
                {
                    AppendText(string.Format("性别检测失败,返回{0}!\n\n", retCode_Gender));
                }
                else
                {
                    gender = UtilToolMemory.PtrToStructure <int>(genderInfo.genderArray + UtilToolMemory.SizeOf <int>() * i);
                }

                int   face3DStatus = -1;
                float roll         = 0f;
                float pitch        = 0f;
                float yaw          = 0f;
                if (retCode_3DAngle != 0)
                {
                    AppendText(string.Format("3DAngle检测失败,返回{0}!\n\n", retCode_3DAngle));
                }
                else
                {
                    //角度状态 非0表示人脸不可信
                    face3DStatus = UtilToolMemory.PtrToStructure <int>(face3DAngleInfo.status + UtilToolMemory.SizeOf <int>() * i);
                    //roll为侧倾角,pitch为俯仰角,yaw为偏航角
                    roll  = UtilToolMemory.PtrToStructure <float>(face3DAngleInfo.roll + UtilToolMemory.SizeOf <float>() * i);
                    pitch = UtilToolMemory.PtrToStructure <float>(face3DAngleInfo.pitch + UtilToolMemory.SizeOf <float>() * i);
                    yaw   = UtilToolMemory.PtrToStructure <float>(face3DAngleInfo.yaw + UtilToolMemory.SizeOf <float>() * i);
                }

                int rectWidth  = rect.right - rect.left;
                int rectHeight = rect.bottom - rect.top;

                //查找最大人脸
                if (rectWidth * rectHeight > rectTemp)
                {
                    rectTemp   = rectWidth * rectHeight;
                    temp       = rect;
                    ageTemp    = age;
                    genderTemp = gender;
                }
                AppendText(string.Format("{0} - 人脸坐标:[left:{1},top:{2},right:{3},bottom:{4},orient:{5},roll:{6},pitch:{7},yaw:{8},status:{11}] Age:{9} Gender:{10}\n", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), rect.left, rect.top, rect.right, rect.bottom, orient, roll, pitch, yaw, age, (gender >= 0 ? gender.ToString() : ""), face3DStatus));
            }

            AppendText(string.Format("{0} - 人脸数量:{1}\n\n", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), multiFaceInfo.faceNum));

            DateTime detectEndTime = DateTime.Now;

            AppendText(string.Format("------------------------------检测结束,时间:{0}------------------------------\n", detectEndTime.ToString("yyyy-MM-dd HH:mm:ss:ms")));
            AppendText("\n");
            ASF_SingleFaceInfo singleFaceInfo = new ASF_SingleFaceInfo();

            //提取人脸特征
            ImageFeature = UtilToolFace.ExtractFaceFeature(pImageEngine, srcImage, out singleFaceInfo);

            //清空上次的匹配结果
            for (int i = 0; i < ImagesFeatureList.Count; i++)
            {
                //_imageList.Items[i].Text = string.Format("{0}号", i);
            }
            //获取缩放比例
            float scaleRate = UtilTool.GetWidthAndHeight(srcImage.Width, srcImage.Height, (int)FRC.FIView._faceIdentificationImageBox.Width, (int)FRC.FIView._faceIdentificationImageBox.Height);

            //缩放图片
            srcImage = UtilTool.ScaleImage(srcImage, (int)FRC.FIView._faceIdentificationImageBox.Width, (int)FRC.FIView._faceIdentificationImageBox.Height);
            //添加标记
            srcImage = UtilTool.MarkRectAndString(srcImage,
                                                  (int)(temp.left * scaleRate),
                                                  (int)(temp.top * scaleRate),
                                                  (int)(temp.right * scaleRate) - (int)(temp.left * scaleRate),
                                                  (int)(temp.bottom * scaleRate) - (int)(temp.top * scaleRate),
                                                  ageTemp, genderTemp,
                                                  (int)FRC.FIView._faceIdentificationImageBox.Width);

            //显示标记后的图像
            FRC.FIView._faceIdentificationImageBox.Source = UtilTool.BitmapToBitmapImage(srcImage);

            MatchImage();
        }
        /// <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;
            }
        }
Exemple #12
0
        private void FaceIdentify(Image srcImage)
        {
            //调整图像宽度,需要宽度为4的倍数
            //srcImage = ImageUtil.ScaleImage(srcImage, picImageCompare.Width, picImageCompare.Height);
            //调整图片数据,非常重要
            ImageInfo imageInfo = ImageUtil.ReadBMP(srcImage);
            //人脸检测
            ASF_MultiFaceInfo multiFaceInfo = FaceUtil.DetectFace(pEngine, imageInfo);
            //年龄检测
            int         retCode_Age = -1;
            ASF_AgeInfo ageInfo     = FaceUtil.AgeEstimation(pEngine, imageInfo, multiFaceInfo, out retCode_Age);
            //性别检测
            int            retCode_Gender = -1;
            ASF_GenderInfo genderInfo     = FaceUtil.GenderEstimation(pEngine, imageInfo, multiFaceInfo, out retCode_Gender);

            ////3DAngle检测
            //int retCode_3DAngle = -1;
            //ASF_Face3DAngle face3DAngleInfo = FaceUtil.Face3DAngleDetection(pEngine, imageInfo, multiFaceInfo, out retCode_3DAngle);

            MemoryUtil.Free(imageInfo.imgData);

            if (multiFaceInfo.faceNum < 1)
            {
                image1Feature         = IntPtr.Zero;
                picImageCompare.Image = srcImage;
                //AppendText(string.Format("{0} - 未检测出人脸!\n\n", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")));
                //AppendText(string.Format("------------------------------检测结束,时间:{0}------------------------------\n", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ms")));
                //AppendText("\n");
                return;
            }

            MRECT temp       = new MRECT();
            int   ageTemp    = 0;
            int   genderTemp = 0;
            int   rectTemp   = 0;

            //标记出检测到的人脸
            for (int i = 0; i < multiFaceInfo.faceNum; i++)
            {
                MRECT rect = MemoryUtil.PtrToStructure <MRECT>(multiFaceInfo.faceRects + MemoryUtil.SizeOf <MRECT>() * i);
                //int orient = MemoryUtil.PtrToStructure<int>(multiFaceInfo.faceOrients + MemoryUtil.SizeOf<int>() * i);
                int age = 0;

                if (retCode_Age != 0)
                {
                    //AppendText(string.Format("年龄检测失败,返回{0}!\n\n", retCode_Age));
                }
                else
                {
                    age = MemoryUtil.PtrToStructure <int>(ageInfo.ageArray + MemoryUtil.SizeOf <int>() * i);
                }

                int gender = -1;
                if (retCode_Gender != 0)
                {
                    AppendText(string.Format("性别检测失败,返回{0}!\n\n", retCode_Gender));
                }
                else
                {
                    gender = MemoryUtil.PtrToStructure <int>(genderInfo.genderArray + MemoryUtil.SizeOf <int>() * i);
                }


                //int face3DStatus = -1;
                //float roll = 0f;
                //float pitch = 0f;
                //float yaw = 0f;
                //if (retCode_3DAngle != 0)
                //{
                //    AppendText(string.Format("3DAngle检测失败,返回{0}!\n\n", retCode_3DAngle));
                //}
                //else
                //{
                //    //角度状态 非0表示人脸不可信
                //    face3DStatus = MemoryUtil.PtrToStructure<int>(face3DAngleInfo.status + MemoryUtil.SizeOf<int>() * i);
                //    //roll为侧倾角,pitch为俯仰角,yaw为偏航角
                //    roll = MemoryUtil.PtrToStructure<float>(face3DAngleInfo.roll + MemoryUtil.SizeOf<float>() * i);
                //    pitch = MemoryUtil.PtrToStructure<float>(face3DAngleInfo.pitch + MemoryUtil.SizeOf<float>() * i);
                //    yaw = MemoryUtil.PtrToStructure<float>(face3DAngleInfo.yaw + MemoryUtil.SizeOf<float>() * i);
                //}


                int rectWidth  = rect.right - rect.left;
                int rectHeight = rect.bottom - rect.top;

                //查找最大人脸
                //if (rectWidth * rectHeight > rectTemp)
                //{
                rectTemp   = rectWidth * rectHeight;
                temp       = rect;
                ageTemp    = age;
                genderTemp = gender;
                // }

                //srcImage = ImageUtil.MarkRectAndString(srcImage, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, age, gender);
                //AppendText(string.Format("{0} - 人脸坐标:[left:{1},top:{2},right:{3},bottom:{4},orient:{5},roll:{6},pitch:{7},yaw:{8},status:{11}] Age:{9} Gender:{10}\n", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), rect.left, rect.top, rect.right, rect.bottom, orient, roll, pitch, yaw, age, (gender >= 0 ? gender.ToString() : ""), face3DStatus));
                srcImage = ImageUtil.MarkRectAndString(srcImage, temp.left, temp.top, temp.right - temp.left, temp.bottom - temp.top, ageTemp, genderTemp);
            }



            ////AppendText(string.Format("{0} - 人脸数量:{1}\n\n", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), multiFaceInfo.faceNum));

            ////显示标记后的图像
            picImageCompare.Image = srcImage;
            ////DateTime detectEndTime = DateTime.Now;
            ////AppendText(string.Format("------------------------------检测结束,时间:{0}------------------------------\n", detectEndTime.ToString("yyyy-MM-dd HH:mm:ss:ms")));
            ////AppendText("\n");
            //ASF_SingleFaceInfo singleFaceInfo = new ASF_SingleFaceInfo();
            //////提取人脸特征
            //image1Feature = FaceUtil.ExtractFeature(pEngine, srcImage, out singleFaceInfo);

            //////清空上次的匹配结果
            ////for (int i = 0; i < imagesFeatureList.Count; i++)
            ////{
            ////    imageList.Items[i].Text = string.Format("{0}号", i);
            ////}
        }