赞
踩
第一步,注册百度al进行登入
第二部,找到人脸识别,并创建公有云获取API Key和Secret Key
第三步,找到人脸识别API文档
第四步,获取 access_token
第五步 ,把对应的参数流写入http请求中 (注意这是方法,还调用了上面GetAccessToken()方法,这里需要上传两张照片,以json格式)
- string access_token = GetAccessToken();
- string url = "https://aip.baidubce.com/rest/2.0/face/v3/match?access_token=" + access_token + "";
- //Bitmap image=(Bitmap)pictureBox1.Image;
- string base64image = ImageToBase64(a+".jpg");//以自己情况,把图片放在Debag目录下的照片
- string base64image1 = ImageToBase64("kl.jpeg");//以自己情况,把图片放在Debag目录下的照片
-
-
- //这里需要上传两张照片
- 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的ImageToBase64()方法
- 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);
-
- }
第六步,在按钮中 调用方法 Contrast();
界面可以自行搭建,
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。