示例#1
0
 /// <summary>
 /// 确定
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void buttonOK_Click(object sender, EventArgs e)
 {
     if (CheckIfIlleagal())
     {
         //处理图片转换为base64
         string targetpath = FormMain.PicCachePath + "/" + textBoxUid.Text + ".jpg";
         LocalImageHelp.CompressImage(UserFaceSource, targetpath);
         string base64image = LocalImageHelp.Image2Base64str(targetpath);
         FormMain.GetInstance().AddUser(textBoxBelongGroup.Text, textBoxName.Text, textBoxUid.Text, base64image, textBoxuserinfo.Text);
         Dispose();
     }
     else
     {
         MessageBox.Show("请录入完整的数据", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
     }
 }
示例#2
0
        /// <summary>
        /// *****************************核心方法 分类照片************************
        /// </summary>
        public void ClassifyPhotos()
        {
            string classifyGroups = string.Empty;

            //构建分组字符串
            for (int i = 0; i < FaceGroup.GroupList.Count; i++)
            {
                if (i != FaceGroup.GroupList.Count && i <= 10)
                {
                    classifyGroups += FaceGroup.GroupList[i].id + ",";
                }
                else
                {
                    classifyGroups += FaceGroup.GroupList[i].id;
                }
            }

            //人脸查找参数
            var searchoptions = new Dictionary <string, object> {
                { "max_user_num", 5 }
            };

            //人脸检测参数
            var detectoptions = new Dictionary <string, object> {
                { "face_field", "age" },
                { "max_face_num", 10 },
                { "face_type", "LIVE" }
            };

            foreach (var folder in ChoosedFolderList)
            {
                if (!ClassifyCancelTokenSource.IsCancellationRequested)
                {
                    //只寻找常见三种类型图片文件
                    FormProgress.setLabel("开始统计文件数量...");
                    string[] photos = Directory.GetFiles(folder, "*.*", SearchOption.AllDirectories).Where(t =>
                                                                                                           (t.ToLower().EndsWith("jpg") || t.ToLower().EndsWith("png") || t.ToLower().EndsWith("bmp")))
                                      .ToArray();
                    int nowphotocount   = 0;
                    int totalphotocount = photos.Length;
                    foreach (var ppath in photos)
                    {
                        if (ClassifyCancelTokenSource.IsCancellationRequested)
                        {
                            statusLabelchange("任务取消!");
                            return;
                        }

                        FormProgress.setLabel($"开始进行照片分组,第{nowphotocount++}张,共{totalphotocount}张");
                        FormProgress.setProgress(nowphotocount, totalphotocount);
                        //构建缓存文件路径
                        string tmpphoto = PicCachePath + "\\" + ppath.Split('\\').Last();
                        //压缩图片
                        LocalImageHelp.CompressImage(ppath, tmpphoto);
                        //新建分类输出组名列表
                        List <string> classifyResultList = new List <string>();
                        string        img64 = LocalImageHelp.Image2Base64str(tmpphoto);

                        //循环调用人脸检测接口 避免QPS超限
                        JObject detectresult;
                        while (true)
                        {
                            detectresult = BaiduAI.BaiduClient.Detect(img64, "BASE64", detectoptions);
                            if (!detectresult.CheckErrorCodeZero())
                            {
                                //code 222202 pic not has face
                                if (detectresult.GetErrorCode() == 222202)
                                {
                                    //跳出当前照片的处理
                                    goto outthisphoto;
                                }

                                Thread.Sleep(100);
                            }
                            else
                            {
                                break;
                            }
                        }

                        //人脸检测返回人脸信息列表
                        var detectFaceList = (JArray)((JObject)detectresult.GetValue("result")).GetValue("face_list");

                        foreach (var face in detectFaceList)
                        {
                            var    faceJObject     = (JObject)face; //转换为Json对象
                            double faceprobability = (double)faceJObject.GetValue("face_probability");
                            if (faceprobability > 0.7)
                            {
                                //如果人脸可能性大于0.6再进行人脸库查找
                                string facetoken = (string)faceJObject.GetValue("face_token");
                                //循环调用人脸查找接口 避免QPS超限
                                JObject searchresult;
                                while (true)
                                {
                                    searchresult = BaiduAI.BaiduClient.Search(facetoken, "FACE_TOKEN", classifyGroups, searchoptions);
                                    if (!searchresult.CheckErrorCodeZero())
                                    {
                                        Thread.Sleep(100);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }

                                //进行人脸结果判断
                                JArray userlist =
                                    (JArray)((JObject)searchresult.GetValue("result")).GetValue("user_list");


                                foreach (var user in userlist)
                                {
                                    JObject userobj = user.ToObject <JObject>();
                                    //获取得分
                                    double score = userobj.GetValue("score").ToObject <double>();
                                    //开始文件分类
                                    if (score > 80)
                                    {
                                        string group_id = userobj.GetValue("group_id").ToString();
                                        //如果该组不存在 则添加该组的名字
                                        if (!classifyResultList.Contains(group_id))
                                        {
                                            classifyResultList.Add(group_id);
                                        }
                                    }
                                }
                            }
                        }

outthisphoto:
                        //如果没有一个得分在80以上
                        if (classifyResultList.Count == 0)
                        {
                            classifyResultList.Add("none");
                        }
                        //复制文件
                        CopyFile(ppath, classifyResultList);
                    }
                }
                else
                {
                    return;
                }
            }
        }