winfrom人脸识别Api调用

界面展示

该代码只是调用人脸识别,主要代码在“对比按钮”中,如需要展示图片,请设置 pictureBox1的值。

 

第一,首先注册百度al进行登入,点击控制台,在左上方的三条杠上找到产品服务中找到人脸识别功能,然后操作如下;

 

 第二,根据API文档,获取Access_token

//获取access_token方法
private string GetAccessToken()
        {
            string grant_type = "client_credentials";
            string client_id = "你的Api key";
            string client_secret = "你的Sercet Key";
            string url = "https://aip.baidubce.com/oauth/2.0/token";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json";
            string content = "grant_type=" + grant_type + "&client_id=" + client_id + "&client_secret=" + client_secret;
            byte[] data = Encoding.UTF8.GetBytes(content.ToString());
            request.ContentLength = data.Length;
            request.GetRequestStream().Write(data, 0, data.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string token = sr.ReadToEnd();
            sr.Close();

            JObject cc = JsonConvert.DeserializeObject<JObject>(token);
            string AccessToken = cc["access_token"].ToString();
            return AccessToken;
            //string AccessToken = token["access_token"];
        }

第三,根据请求参数,把参数流写入http请求中

//人脸识别方法
private void Contrast()
        {
            string access_token = GetAccessToken();//调用GetAccessToken()方法获取access_token
            string url = "https://aip.baidubce.com/rest/2.0/face/v3/match?access_token=" + access_token + "";
            //Bitmap image=(Bitmap)pictureBox1.Image;
            string base64image = ImageToBase64("第一张Debug目录下的照片");//把图片转base64
            string base64image1 = ImageToBase64("第二张Debug目录下的照片");

//这里需要上传两张照片,格式为json
            string str = "\r\n[\r\n    {\r\n        \"image\": \"" + base64image + "\",\r\n        \"image_type\": \"BASE64\",\r\n        \"face_type\": \"LIVE\",\r\n        \"quality_control\": \"LOW\",\r\n        \"liveness_control\": \"HIGH\"\r\n     },\r\n     {\r\n         \"image\": \"" + base64image1 + "\",\r\n         \"image_type\": \"BASE64\",\r\n         \"face_type\": \"IDCARD\",\r\n         \"quality_control\": \"LOW\",\r\n         \"liveness_control\": \"HIGH\"\r\n     }\r\n ]";
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.ContentLength = bytes.Length;
            request.GetRequestStream().Write(bytes, 0, bytes.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string content = sr.ReadToEnd();
            sr.Close();

            //反序列化content
            JObject date = JsonConvert.DeserializeObject<JObject>(content);
            string datestr = date["result"].ToString();

            //再次反序列化datestr
            JObject da = JsonConvert.DeserializeObject<JObject>(datestr);
            string datejson = da["score"].ToString();//获取闽值

            try
            {
                if (Convert.ToDouble(datejson.ToString()) >= 80)
                {
                    MessageBox.Show("人脸识别成功!");
                    return;
                }
                else
                {
                    MessageBox.Show("人脸识别失败,请重试!");
                    return;
                }

            }
            catch (Exception)
            {

                throw;
            }
        }
//图片转base64方法
private string ImageToBase64(string fileFullName)
        {
            Bitmap bmp = new Bitmap(fileFullName);
            MemoryStream ms = new MemoryStream();
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] arr = new byte[ms.Length]; ms.Position = 0;
            ms.Read(arr, 0, (int)ms.Length); ms.Close();

            return Convert.ToBase64String(arr);

        }

第四,调

 private void btncontrast_Click(object sender, EventArgs e)
        {
            Contrast();
        }

用人脸识别方法Contrast()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值