コード例 #1
0
        /// <summary>
        /// 做扫描识别
        /// </summary>
        /// <param name="path"></param>
        private static void doScan(String path)
        {
            ResultCode result = EngineActivate.ActivateEngine(APPID, APPKEY);

            if (result != ResultCode.SDK已激活)
            {
                Console.WriteLine(result.ToString());
                return;
            }
            IntPtr hengine = EngineFactory.GetEngineInstance(EngineFactory.Image,
                                                             DetectionOrientPriority.ASF_OP_0_ONLY, 16);
            FaceDetection face = new FaceDetection(hengine);

            DirectoryInfo root = new DirectoryInfo(path);

            //获取所有下载的图片开始识别
            List <FileInfo> Filelist = root.GetFiles("*.jpeg").ToList();
            Bitmap          img      = null;

            foreach (FileInfo file in Filelist) //外层是文件
            {
                try
                {
                    img = new Bitmap(file.FullName);
                    foreach (LocalUserMod mod in allRegUsers) //内层是用户
                    {
                        try
                        {
                            //此人已被识别,不做识别
                            if (resultSend[mod.Name].isScaned) //如果已经查找了这个人
                            {
                                continue;
                            }
                            byte[] f1  = Convert.FromBase64String(mod.Freature);
                            byte[] f2  = face.getFaceFeature(img);
                            float  sim = face.Compare(f1, f2);
                            if (sim >= Similarity)
                            {
                                //获取3D角度
                                FaceDetection.TDAResult tdr = face.GetThreeDAngle(img);
                                Console.WriteLine("Name: {0}\nThreeDAngle: {1}", mod.Name, tdr.ToString());
                                //判断角度
                                //更改状态
                                if (tdr.Pitch <= -13)                //低头角度
                                {
                                    resultSend[mod.Name].Status = 1; //低头
                                }
                                else if (tdr.Yaw <= -26 || tdr.Yaw >= 26)
                                {
                                    resultSend[mod.Name].Status = 2; //左右
                                }
                                else
                                {
                                    resultSend[mod.Name].Status = 3; //认真
                                }
                                //标识已扫描
                                resultSend[mod.Name].isScaned = true;
                                //上传文件到前端
                                HttpUtil.postFile(Url_PostImg, file.FullName);
                                continue;
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error in scanning! {0}", ex.Message);
                            continue;
                        }
                        //未识别出
                        Console.WriteLine("Cannot distinguish : {0}", file.Name);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    continue;
                }
                finally
                {
                    //释放图片资源
                    if (img != null)
                    {
                        img.Dispose();
                    }
                }
            }
            try
            {
                String datas = JsonConvert.SerializeObject(resultSend);
                //post数据到前端
                HttpUtil.postData(Url_PostData, datas);
                //销毁引擎
                EngineFactory.DisposeEngine();
                Console.WriteLine("Scan over......");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return;
            }
        }
コード例 #2
0
        /// <summary>
        /// 注册事件触发函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args">变量参数</param>
        public static void NewUserRegist(object sender, myEventArgs.RegistArgs args)
        {
            /****************************
            * 保存特征值时才区分性别
            * **************************/
            //下载图片到本地
            String url = String.Format("{0}/{1}_{2}.{3}",
                                       Url_DldImg, args.Regist.Id, args.Regist.Name, args.Regist.Extension);
            String pathImg = String.Format("{0}\\{1}_{2}.{3}",
                                           ImgRootPath_Regist, args.Regist.Id, args.Regist.Name, args.Regist.Extension);

            HttpUtil.downFile(url, pathImg);
            //判断性别(假设性别判断 100% 准确)
            try
            {
                ResultCode result = EngineActivate.ActivateEngine(APPID, APPKEY);
                if (result != ResultCode.SDK已激活)
                {
                    Console.WriteLine(result.ToString());
                    return;
                }
                IntPtr hengine = EngineFactory.GetEngineInstance(EngineFactory.Image,
                                                                 DetectionOrientPriority.ASF_OP_0_ONLY, 16); //检测角度指人脸在照片中的角度
                Bitmap img1 = new Bitmap(pathImg);

                var          face  = new FaceDetection(hengine, img1);
                var          r     = face.GetGender();
                LocalUserMod temp1 = new LocalUserMod();
                temp1.Name = args.Regist.Name;
                temp1.Num  = args.Regist.Id;
                //用Base64转码
                temp1.Freature = Convert.ToBase64String(face.getFaceFeature(img1));
                //销毁图片
                img1.Dispose();
                //提取特征值保存
                switch (r)
                {
                case "男":
                    XmlUtil.AddOneData(temp1, "Male");
                    break;

                case "女":
                    XmlUtil.AddOneData(temp1, "Female");
                    break;

                default:
                    Console.WriteLine("获取性别失败!用户:{0} , 学号:{1},注册时间:{2}",
                                      args.Regist.Name, args.Regist.Id, DateTime.Now.ToString());
                    return;
                }
                //提示用户注册
                Console.WriteLine("User registed: {0} at {1}", args.Regist.Name, DateTime.Now.ToString());
                //更新用户信息
                //令写一个函数
                getAllUserInfo();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Regist Error: {0}", ex.Message);
            }
            finally
            {
                EngineFactory.DisposeEngine();
            }
        }