Example #1
0
 /// <summary>
 /// 绑定检测信息
 /// </summary>
 public void FaceTestBind(FaceCheckModel model)
 {
     //清空文本框
     TestTextBoxAdd(textBox1, "", "", true);
     if (model.Face_num > 0)
     {
         if (model.Face_list != null && model.Face_list.AsEnumerable() != null && model.Face_list.AsEnumerable().Any())
         {
             TestTextBoxAdd(textBox1, model.Face_num, "人脸数量为:");
             TestTextBoxAdd(textBox1, "", "--------------------------------------");
             int count = 0;
             foreach (var item in model.Face_list)
             {
                 count++;
                 TestTextBoxAdd(textBox1, "", $"当前检测第{count}张人脸{item.Face_probability * 100}%为一张真实人脸");
                 TestTextBoxAdd(textBox1, $"{item.Quality.Blur * 100}%", "人脸模糊程度 :");
                 TestTextBoxAdd(textBox1, item.Age, "年龄 :");
                 TestTextBoxAdd(textBox1, item.Beauty, "美丑打分 :");
                 TestTextBoxAdd(textBox1, item.Gender.Type.GetEnumDescription() + $"  概率为{item.Expression.Probability * 100}%", "性别 :");
                 TestTextBoxAdd(textBox1, item.Expression.Type.GetEnumDescription() + $"  概率为{item.Expression.Probability * 100}%", "表情 :");
                 TestTextBoxAdd(textBox1, item.Emotion.Type.GetEnumDescription() + $"  概率为{item.Emotion.Probability * 100}%", "情绪 :");
                 TestTextBoxAdd(textBox1, item.Glasses.Type.GetEnumDescription() + $"  概率为{item.Glasses.Probability * 100}%", "是否带眼镜 :");
                 TestTextBoxAdd(textBox1, $"左眼未闭合概率为{item.Eye_status.Left_eye*100}%,\r\n\r\n右眼未闭合概率为{item.Eye_status.Right_eye * 100}%", "右眼闭合状态 :");
                 TestTextBoxAdd(textBox1, item.Race.Type.GetEnumDescription() + $"  概率为{item.Race.Probability * 100}%", "人种 :");
                 TestTextBoxAdd(textBox1, item.Face_shape.Type.GetEnumDescription() + $"  概率为{item.Face_shape.Probability * 100}%", "脸型 :");
                 TestTextBoxAdd(textBox1, item.Face_Type.Type.GetEnumDescription() + $"  概率为{item.Face_Type.Probability * 100}%", "真实人脸/卡通人脸 :");
                 TestTextBoxAdd(textBox1, "", "--------------------------------------");
             }
         }
     }
     else
     {
         TestTextBoxAdd(textBox1, "", "未获取到人脸信息,或者网络异常!");
     }
 }
Example #2
0
        /// <summary>
        /// 人脸检测
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void skinButton1_Click(object sender, EventArgs e)
        {
            if (picbPreview.Image is null)
            {
                MessageBox.Show("请先拍照或者选取一张图片,再进行检测操作");
                return;
            }
            // var APP_ID = "17894506";
            Thread threadadd = new Thread(() =>
            {
                try
                {
                    var client = new Baidu.Aip.Face.Face(API_KEY, SECRET_KEY)
                    {
                        Timeout = 60000  // 修改超时时间
                    };
                    var image     = ImgToBase64String((Bitmap)this.picbPreview.Image);
                    var imageType = "BASE64";

                    // 调用人脸检测,可能会抛出网络等异常,请使用try/catch捕获
                    var result = client.Detect(image, imageType);
                    // 如果有可选参数
                    var options = new Dictionary <string, object>
                    {
                        { "face_field", "age,beauty,expression,face_shape,gender,glasses," +
                          ",race,quality,eye_status,emotion,face_type,eye_status" },
                        { "max_face_num", Max_face_num },
                        { "face_type", "LIVE" },
                        { "liveness_control", "LOW" }
                    };
                    // 带参数调用人脸检测
                    result = client.Detect(image, imageType, options);
                    if (result != null && result.ToString() != null && result.ToString().Length > 0)
                    {
                        var json             = JsonConvert.SerializeObject(result);
                        FaceCheckModel model = DeserializeJsonToObject <GetApiJson <FaceCheckModel> >(json)?.Result ?? new FaceCheckModel();
                        FaceTestBind(model);
                    }
                    ;


                    var picclient = new Baidu.Aip.ImageClassify.ImageClassify("NpBGfUR6qBGtFo5bIFbiPCO9", "S0L7LXAewfW7BBKmbXd0EQ8iRzEYRGqc")
                    {
                        Timeout = 60000  // 修改超时时间
                    };
                    Image img        = this.picbPreview.Image;
                    MemoryStream ms  = new MemoryStream();
                    byte[] imagedata = null;
                    img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    imagedata = ms.GetBuffer();

                    var picoptions = new Dictionary <string, object> {
                        { "baike_num", Result_num }
                    };

                    var results = picclient.AdvancedGeneral(imagedata, picoptions);
                    if (results != null && results.ToString() != null && results.ToString().Length > 0)
                    {
                        var json = JsonConvert.SerializeObject(results);
                        ImageRecognitionModel model = DeserializeJsonToObject <ImageRecognitionModel>(json) ?? new ImageRecognitionModel();
                        ImageRecognitionBind(model);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("网络错误!错误信息:" + ex.Message);
                }
            });

            threadadd.Start();
        }