当前位置:   article > 正文

winfrom窗体调用人脸识别Api_winform人脸登录

winform人脸登录

第一步,注册百度al进行登入  

第二部,找到人脸识别,并创建公有云获取API Key和Secret Key

 

第三步,找到人脸识别API文档

第四步,获取 access_token

第五步 ,把对应的参数流写入http请求中 (注意这是方法,还调用了上面GetAccessToken()方法,这里需要上传两张照片,以json格式)

  1. string access_token = GetAccessToken();
  2. string url = "https://aip.baidubce.com/rest/2.0/face/v3/match?access_token=" + access_token + "";
  3. //Bitmap image=(Bitmap)pictureBox1.Image;
  4. string base64image = ImageToBase64(a+".jpg");//以自己情况,把图片放在Debag目录下的照片
  5. string base64image1 = ImageToBase64("kl.jpeg");//以自己情况,把图片放在Debag目录下的照片
  6. //这里需要上传两张照片
  7. 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 ]";
  8. byte[] bytes = Encoding.UTF8.GetBytes(str);
  9. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  10. request.Method = "POST";
  11. request.ContentType = "application/json";
  12. request.ContentLength = bytes.Length;
  13. request.GetRequestStream().Write(bytes, 0, bytes.Length);
  14. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  15. StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  16. string content = sr.ReadToEnd();
  17. sr.Close();
  18. //反序列化content
  19. JObject date = JsonConvert.DeserializeObject<JObject>(content);
  20. string datestr = date["result"].ToString();
  21. //再次反序列化datestr
  22. JObject da = JsonConvert.DeserializeObject<JObject>(datestr);
  23. string datejson = da["score"].ToString();//获取闽值
  24. try
  25. {
  26. if (Convert.ToDouble(datejson.ToString()) >= 80)
  27. {
  28. MessageBox.Show("人脸识别成功!");
  29. return;
  30. }
  31. else
  32. {
  33. MessageBox.Show("人脸识别失败,请重试!");
  34. return;
  35. }
  36. }
  37. catch (Exception)
  38. {
  39. throw;
  40. }
  41. }

图片转base64的ImageToBase64()方法

  1. private string ImageToBase64(string fileFullName)
  2. {
  3. Bitmap bmp = new Bitmap(fileFullName);
  4. MemoryStream ms = new MemoryStream();
  5. bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
  6. byte[] arr = new byte[ms.Length]; ms.Position = 0;
  7. ms.Read(arr, 0, (int)ms.Length); ms.Close();
  8. return Convert.ToBase64String(arr);
  9. }

第六步,在按钮中 调用方法 Contrast();

 界面可以自行搭建,

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/894001
推荐阅读
  

闽ICP备14008679号