コード例 #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;
            }
        }