调用百度AI人脸识别,c#实现人脸对比,人脸识别,登录验证

目录

一.如何使用百度云ai

​编辑

二.代码编写

(1)设计页面:

(2)前期NuGet准备:​编辑

(3)导入配置

 连续打开摄像头

人脸检测功能:这个代码的主要实现的功能是接受并处理图像数据,也就是将传入的图像转换为 Bitmap 格式,并进一步处理为百度人脸识别所需的 Base64 格式字符串。然后调用百度人脸检测API,并返回检测结果。最后是解析并显示人脸检测结果,提取出人脸的位置、年龄、模糊程度等信息,并在用户界面上显示这些信息。

人脸对比功能,对比两张图片的相似度

三.程序总结


一.如何使用百度云ai

首先注册账号然后领取免费使用特权

然后创建应用以及用户像下面这样

当这些处理好后就可以通过vs编写程序了

二.代码编写

(1)设计页面:

(2)前期NuGet准备:

(3)导入配置

        private string APP_ID = "33711276";
        private string API_KEY = "1nhS4AcrLFffxqDKB73bb1U7";
        private string SECRET_KEY = "rtnRFv5RcE0dMxBWqmL6PX3LYwVAdETW";

        private Face client = null;
        /// <summary>
        /// 是否可以检测人脸
        /// </summary>
        private bool IsStart = false;
        /// <summary>
        /// 人脸在图像中的位置
        /// </summary>
        private FaceLocation location = null;

        private FilterInfoCollection videoDevices = null;

        private VideoCaptureDevice videoSource;

 连续打开摄像头

 private void CameraConn()
 {
     if (comboBox1.Items.Count<=0)
     {
         MessageBox.Show("请插入视频设备");
         return;
     }
     videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
     videoSource.DesiredFrameSize = new System.Drawing.Size(320, 240);
     videoSource.DesiredFrameRate = 1;
     
     videoSourcePlayer1.VideoSource = videoSource;
     videoSourcePlayer1.Start();
 }

人脸检测功能:

这个代码的主要实现的功能是接受并处理图像数据,也就是将传入的图像转换为 Bitmap 格式,并进一步处理为百度人脸识别所需的 Base64 格式字符串。然后调用百度人脸检测API,并返回检测结果。最后是解析并显示人脸检测结果,提取出人脸的位置、年龄、模糊程度等信息,并在用户界面上显示这些信息。

 public void Detect(object image)
 {
     if (image!=null &&  image is Bitmap)
     {
         try
         {
             Bitmap img = (Bitmap)image;
             var imgByte = Bitmap2Byte(img);
             Image im =img ;
             string image1 = ConvertImageToBase64(im);
             string imageType = "BASE64";

             if (imgByte != null)
             {
                 // 如果有可选参数
                 var options = new Dictionary<string, object>{
                     {"max_face_num", 2},
                     {"face_fields", "age,qualities,beauty"}
                 };
                 var result = client.Detect(image1, imageType,options);
                 FaceDetectInfo detect = JsonHelper.DeserializeObject<FaceDetectInfo>(result.ToString());
                 if (detect!=null && detect.result_num>0)
                 {
                     ageText.Text = detect.result[0].age.TryToString();
                     this.location = detect.result[0].location;
                     StringBuilder sb = new StringBuilder();
                     if (detect.result[0].qualities != null)
                     {
                         if (detect.result[0].qualities.blur >= 0.7)
                         {
                             sb.AppendLine("人脸过于模糊");
                         }
                         if (detect.result[0].qualities.completeness >= 0.4)
                         {
                             sb.AppendLine("人脸不完整");
                         }
                         if (detect.result[0].qualities.illumination <= 40)
                         {
                             sb.AppendLine("灯光光线质量不好");
                         }
                         if (detect.result[0].qualities.occlusion!=null)
                         {
                             if (detect.result[0].qualities.occlusion.left_cheek>=0.8)
                             {
                                 sb.AppendLine("左脸颊不清晰");
                             }
                             if (detect.result[0].qualities.occlusion.left_eye >= 0.6)
                             {
                                 sb.AppendLine("左眼不清晰");
                             }
                             if (detect.result[0].qualities.occlusion.mouth >= 0.7)
                             {
                                 sb.AppendLine("嘴巴不清晰");
                             }
                             if (detect.result[0].qualities.occlusion.nose >= 0.7)
                             {
                                 sb.AppendLine("鼻子不清晰");
                             }
                             if (detect.result[0].qualities.occlusion.right_cheek >= 0.8)
                             {
                                 sb.AppendLine("右脸颊不清晰");
                             }
                             if (detect.result[0].qualities.occlusion.right_eye >= 0.6)
                             {
                                 sb.AppendLine("右眼不清晰");
                             }
                             if (detect.result[0].qualities.occlusion.chin >= 0.6)
                             {
                                 sb.AppendLine("下巴不清晰");
                             }
                             if (detect.result[0].pitch>=20)
                             {
                                 sb.AppendLine("俯视角度太大");
                             }
                             if (detect.result[0].roll>=20)
                             {
                                 sb.AppendLine("脸部应该放正");
                             }
                             if (detect.result[0].yaw>=20)
                             {
                                 sb.AppendLine("脸部应该放正点");
                             }
                         }
                         
                     }
                     if (detect.result[0].location.height<=100 || detect.result[0].location.height<=100)
                     {
                         sb.AppendLine("人脸部分过小");
                     }
                     textBox4.Text = sb.ToString();
                     if (textBox4.Text.IsNull())
                     {
                         textBox4.Text = "OK";
                     }
                 }
             }
         }
         catch (Exception ex)
         {
             ClassLoger.Error("Form1.image", ex);
         }
     }
     
 }

这个的结果:

这里显示倪雨涵的颜值就是beauty的值。

人脸对比功能,对比两张图片的相似度

代码如下:

 private void button2_Click(object sender, EventArgs e)
 {
     if (string.IsNullOrEmpty(textBox2.Text) || string.IsNullOrEmpty(textBox3.Text))
     {
         MessageBox.Show("请选择要对比的人脸图片");
         return;
     }
     try
     {
         string path1=textBox2.Text;
         string path2=textBox3.Text;
         
         var faces = new JArray
         {
             new JObject
             {
                 {"image", ReadImg(path1)},
                 {"image_type", "BASE64"},
                 {"face_type", "LIVE"},
                 {"quality_control", "LOW"},
                 {"liveness_control", "NONE"},
             },
             new JObject
             {
                 {"image", ReadImg(path2)},
                 {"image_type", "BASE64"},
                 {"face_type", "LIVE"},
                 {"quality_control", "LOW"},
                 {"liveness_control", "NONE"},
             }
          };
         
         // 带参数调用人脸比对
         var result = client.Match(faces);
         textBox1.Text = result.ToString();
     }
     catch (Exception ex)
     { }
 }

结果显示:

三.程序总结

这个博客主要介绍了利用百度云人脸识别和c#接口实现图片人脸识别、对比以及验证登录等等功能,希望本博客能为大家提供帮助,谢谢!

  • 13
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值