Baidu ai 人脸识别程序

一.简介

   本次实验主要针对使用windows窗体开发设计使用vs2022进行开发利用百度ai的人脸识别,人脸搜索,人脸对比,人脸登录实现功能。

二.主要实现

 1.注册百度ai,领取免费权限

       打开下面链接进入百度智能云网址:百度智能云-云智一体深入产业百度智能云致力于为企业和开发者提供全球领先的人工智能、大数据和云计算服务,加速产业智能化转型升级icon-default.png?t=N7T8https://cloud.baidu.com/

2.进入以下界面进行登录

并点击人脸实名认证,进入以下界面,立即使用

3.创建应用

先点击免费尝鲜领取对应权限

然后在下面界面创建对应的应用并输入对应的功能

4.进行配置

查看官方文档对c#的sdk进行配置.

同时我们也需要在vs中安装以下库

最后我们需要在下列库中添加人脸和用户

三.框架构建和代码编写

1.页面设计

利用winform搞出来一个下面的页面:

2.导入对应的配置

        private string APP_ID = "?";
        private string API_KEY = "?";
        private string SECRET_KEY = "?";

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

        private FilterInfoCollection videoDevices = null;

        private VideoCaptureDevice videoSource;

一些其他的函数

 public string ConvertImageToBase64(Image file)
 {
     using (MemoryStream memoryStream = new MemoryStream())
     {
         file.Save(memoryStream, file.RawFormat);
         byte[] imageBytes = memoryStream.ToArray();
         return Convert.ToBase64String(imageBytes);
     }
 }


 

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();
}

将图片转为base64,连接摄像头等函数

3.人脸检测功能的实现    

 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);
         }
     }
     
 }

上述代码主要是它首先将图像转换为base64编码格式,然后使用第三方API进行人脸检测和分析。它检查人脸的各种质量指标,如模糊程度、完整性、光照条件、遮挡情况,以及俯视角度和翻转角度,并将分析结果显示在文本框中。如果人脸检测和分析顺利,则会显示"OK".

4.人脸对比实现

 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)
     { }
 }

该代码实现了人脸比对的功能。当用户在两个文本框中输入了需要比对的两张人脸图片的路径时,该代码会首先检查是否有输入,如果有输入,则会读取这两张图片并将其转换为Base64编码格式。然后,它会将这两张人脸图片的信息打包成一个JSON数组,并调用第三方API进行人脸比对。最终,比对结果会显示在第一个文本框中。

四.最后的运行结果

1.人脸的检测报告

颜值只有42.75,呜呜呜。

2.人脸对比的结果

也是成功对比上了。

五.总结

对于上述实验我有以下心得体会:

  1. 接口配置的重要性:
    在使用人脸识别功能时,需要依赖第三方服务提供商(如百度)的接口和算法支持。因此,在启动小程序前,务必先完成百度接口的配置工作,包括注册账号、创建应用并获取 API Key 和 Secret Key。这些配置信息是小程序顺利运行的前提条件。

  2. 人脸注册的灵活性:
    该小程序提供了静态图像注册和实时图像注册两种方式。静态注册适用于提前准备好个人照片的场景,而实时注册则更加便捷,只需在摄像头前保持人脸正对即可完成。这种灵活的注册方式,为用户提供了更多的选择。

  3. 识别准确性的保证:
    通过事先完成详细的人脸注册,小程序能够准确识别已录入的用户。当摄像头监测到这些已注册的人脸时,能够准确地弹出相应的欢迎语,体现了良好的识别效果。这对于应用的使用体验和安全性都有重要影响。

  4. 易用性和可扩展性:
    整个部署和使用流程相对简单,用户只需遵循文档中的步骤即可完成。同时,该小程序具有一定的可扩展性,未来可以针对更多用户和场景进行优化和升级。

总之,通过学习和实践这款人脸识别小程序,我对其整体功能和使用流程有了更深入的理解。相信只要按照文档要求进行正确部署和配置,就能够充分发挥该程序的识别能力,为用户提供便捷、安全的应用体验。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值