C#调用Face++ API方法实现图片人脸对比

1. VS2010创建一个Form工程;

   

C#调用Face++ API方法实现图片人脸对比_第1张图片

2. From上创建两个PictureBox和一个对比的Button,两个选择图片的button,两个textbox用于显示结果和对比结果。

3. 选择图片button,选取图片后显示到Picturebox

    private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog file = new OpenFileDialog();
            file.ShowDialog();
            if (file.FileName != string.Empty)
            {
                image1Path = file.FileName;
                listBox1.Items.Add("add image1,path :" + image1Path);
                pictureBox1.Load(image1Path);
            }
           
                 
       }

4.  对比Button:

    步骤一:在Face++网站注册个人Key和Secert,创建一个Distionary变量,参数添加如下;

 Dictionary verifyPostParameters = new Dictionary();
            verifyPostParameters.Add("api_key", key);
            verifyPostParameters.Add("api_secret", secert);

 

   步骤二:

     转换图片格式为application/octet-stream格式,并添加到参数中。

  

//第一张图
            Bitmap bmp = new Bitmap(argImagePath1); // 图片地址
            byte[] fileImage;

            using (Stream stream1 = new MemoryStream())
            {
                bmp.Save(stream1, ImageFormat.Jpeg);
                byte[] arr = new byte[stream1.Length];
                stream1.Position = 0;
                stream1.Read(arr, 0, (int)stream1.Length);
                stream1.Close();
                fileImage = arr;
            }
            //添加图片参数
            verifyPostParameters.Add("image_file1", new HttpHelper4MultipartForm.FileParameter(fileImage, "1.jpg", "application/octet-stream"));
           // verifyPostParameters.Add("image_file2", new HttpHelper4MultipartForm.FileParameter(fileImage, "2.jpg", "application/octet-stream"));

            //第二张图
            Bitmap bmp2 = new Bitmap(argImagePath2); // 图片地址
            byte[] fileImage2;

            using (Stream stream2 = new MemoryStream())
            {
                bmp2.Save(stream2, ImageFormat.Jpeg);
                byte[] arr2 = new byte[stream2.Length];
                stream2.Position = 0;
                stream2.Read(arr2, 0, (int)stream2.Length);
                stream2.Close();
                fileImage2 = arr2;
            }
            //添加图片参数
            verifyPostParameters.Add("image_file2", new HttpHelper4MultipartForm.FileParameter(fileImage2, "2.jpg", "application/octet-stream"))

 

   步骤三:调用http接口并传输参数。

     HttpWebResponse verifyResponse = HttpHelper4MultipartForm.MultipartFormDataPost("https://api-cn.faceplusplus.com/facepp/v3/compare", "", verifyPostParameters);
 

  步骤四,判断返回结果:Json

    JObject jo = JObject.Parse(result);
                    JToken confidence = null;
                    if (jo.TryGetValue("confidence", out confidence))
                    {
                        float persent = float.Parse(confidence.ToString());
                        listBox1.Items.Add("confidence : " + persent.ToString());
                        int c = (int)persent;
                        listBox2.Items.Add("相似度: " + GetResult(c));
                    }

你可能感兴趣的:(Face++,人脸对比,Face,C#,旷世API,AI)