0


使用百度ai人脸识别,实现人脸检测、人脸对比

一、前期准备

1、注册“百度AI开放平台”

搜索进入“百度AI开放平台”,点击“开放能力”-“人脸与人体”-“人脸识别云服务”

人脸识别_人脸识别_准确率99.99%_免费试用-百度AI开放平台 (baidu.com)

点击“立即使用”

点击“免费尝鲜”领取人脸识别接口,点击“创建应用”创建新的应用

勾选需要用到的接口,点击“0元领取”

可以在“可视化人脸库”添加人脸

在“应用列表”可以看到自己的APPID、API Key、Secret Key

2、安装Nuget程序包

在VS2022中安装以下Nuget程序包

二、代码设计

1、页面设计

添加控件:button、label、textbox、panel、videosourceplayer

2、前期配置

根据百度AI人脸识别“创建应用”处所给内容将API填入

  1. private string APP_ID = "换成你的APP_ID";
  2. private string API_KEY = "换成你的API_KEY";
  3. private string SECRET_KEY = "换成你的SECRET_KEY";
  4. private Face client = null;
  5. private bool IsStart = false;
  6. private FaceLocation location = null;
  7. private FilterInfoCollection videoDevices = null;
  8. private VideoCaptureDevice videoSource;

将一个

  1. Image

对象转换为其Base64编码的字符串形式。

Base64编码常用于在文本格式中嵌入二进制数据,例如在网络编程中,当需要将图像、文件或其他二进制数据作为文本传输时。

  1. public string ConvertImageToBase64(Image file)
  2. {
  3. using (MemoryStream memoryStream = new MemoryStream())
  4. {
  5. file.Save(memoryStream, file.RawFormat);
  6. byte[] imageBytes = memoryStream.ToArray();
  7. return Convert.ToBase64String(imageBytes);
  8. }
  9. }

定义一个名为

  1. ReadImg

的方法,该方法接受一个字符串参数

  1. img

指向图像文件的路径。

该方法的功能是读取该图像文件的所有字节,并将其转换为 Base64 编码的字符串。

  1. public string ReadImg(string img)
  2. {
  3. return Convert.ToBase64String(File.ReadAllBytes(img));
  4. }

连接并打开摄像头

  1. private void CameraConn()
  2. {
  3. if (comboBox1.Items.Count<=0)
  4. {
  5. MessageBox.Show("请插入视频设备");
  6. return;
  7. }
  8. videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
  9. videoSource.DesiredFrameSize = new System.Drawing.Size(320, 240);
  10. videoSource.DesiredFrameRate = 1;
  11. videoSourcePlayer1.VideoSource = videoSource;
  12. videoSourcePlayer1.Start();
  13. }

3、人脸检测

(1)参数验证:首先,验证传入的 image 对象是否为非空的 Bitmap 类型。这是为了确保后续处理的数据是有效的图像数据。

(2)图像转换:如果图像验证通过,将 Bitmap 图像转换为 Base64 编码的字符串 image1。这是因为很多网络服务(如人脸检测API)通常要求图像数据以Base64编码的字符串形式发送。

(3)设置检测选项:代码创建了一个包含检测选项的字典 options。这些选项指定了检测API的行为,例如返回的人脸数量上限(max_face_num)和需要检测的人脸属性(face_fields)。

(4)调用人脸检测API:使用设置好的选项和Base64编码的图像字符串,代码调用了 client.Detect 方法(这个方法可能是对某个外部API的封装)。这个API执行人脸检测,并返回检测结果。

(5)解析检测结果:检测结果(假设是JSON格式的字符串)被反序列化为 FaceDetectInfo 类型的对象 detect。这个对象包含了检测到的人脸信息,如年龄、位置、质量等。

(6)处理检测结果:如果检测到人脸,代码会读取并显示第一个检测到的人脸的年龄信息。同时,它还会检查人脸的质量属性,如模糊度、完整性、光照和遮挡情况,并根据预设的阈值判断是否存在问题。

  1. public void Detect(object image)
  2. {
  3. if (image!=null && image is Bitmap)
  4. {
  5. try
  6. {
  7. Bitmap img = (Bitmap)image;
  8. var imgByte = Bitmap2Byte(img);
  9. Image im =img ;
  10. string image1 = ConvertImageToBase64(im);
  11. string imageType = "BASE64";
  12. if (imgByte != null)
  13. {
  14. // 如果有可选参数
  15. var options = new Dictionary<string, object>{
  16. {"max_face_num", 2},
  17. {"face_fields", "age,qualities,beauty"}
  18. };
  19. var result = client.Detect(image1, imageType,options);
  20. FaceDetectInfo detect = JsonHelper.DeserializeObject<FaceDetectInfo>(result.ToString());
  21. if (detect!=null && detect.result_num>0)
  22. {
  23. ageText.Text = detect.result[0].age.TryToString();
  24. this.location = detect.result[0].location;
  25. StringBuilder sb = new StringBuilder();
  26. if (detect.result[0].qualities != null)
  27. {
  28. if (detect.result[0].qualities.blur >= 0.7)
  29. {
  30. sb.AppendLine("人脸过于模糊");
  31. }
  32. if (detect.result[0].qualities.completeness >= 0.4)
  33. {
  34. sb.AppendLine("人脸不完整");
  35. }
  36. if (detect.result[0].qualities.illumination <= 40)
  37. {
  38. sb.AppendLine("灯光光线质量不好");
  39. }
  40. if (detect.result[0].qualities.occlusion!=null)
  41. {
  42. if (detect.result[0].qualities.occlusion.left_cheek>=0.8)
  43. {
  44. sb.AppendLine("左脸颊不清晰");
  45. }
  46. if (detect.result[0].qualities.occlusion.left_eye >= 0.6)
  47. {
  48. sb.AppendLine("左眼不清晰");
  49. }
  50. if (detect.result[0].qualities.occlusion.mouth >= 0.7)
  51. {
  52. sb.AppendLine("嘴巴不清晰");
  53. }
  54. if (detect.result[0].qualities.occlusion.nose >= 0.7)
  55. {
  56. sb.AppendLine("鼻子不清晰");
  57. }
  58. if (detect.result[0].qualities.occlusion.right_cheek >= 0.8)
  59. {
  60. sb.AppendLine("右脸颊不清晰");
  61. }
  62. if (detect.result[0].qualities.occlusion.right_eye >= 0.6)
  63. {
  64. sb.AppendLine("右眼不清晰");
  65. }
  66. if (detect.result[0].qualities.occlusion.chin >= 0.6)
  67. {
  68. sb.AppendLine("下巴不清晰");
  69. }
  70. if (detect.result[0].pitch>=20)
  71. {
  72. sb.AppendLine("俯视角度太大");
  73. }
  74. if (detect.result[0].roll>=20)
  75. {
  76. sb.AppendLine("脸部应该放正");
  77. }
  78. if (detect.result[0].yaw>=20)
  79. {
  80. sb.AppendLine("脸部应该放正点");
  81. }
  82. }
  83. }
  84. if (detect.result[0].location.height<=100 || detect.result[0].location.height<=100)
  85. {
  86. sb.AppendLine("人脸部分过小");
  87. }
  88. textBox4.Text = sb.ToString();
  89. if (textBox4.Text.IsNull())
  90. {
  91. textBox4.Text = "OK";
  92. }
  93. }
  94. }
  95. }
  96. catch (Exception ex)
  97. {
  98. ClassLoger.Error("Form1.image", ex);
  99. }
  100. }
  101. }

运行结果

4、选择人脸图

允许用户通过点击按钮(

  1. button3

)打开一个文件选择对话框,选择一个文件,并将所选文件的完整路径显示在文本框中。

  1. private void button3_Click(object sender, EventArgs e)
  2. {
  3. OpenFileDialog dialog = new OpenFileDialog();
  4. dialog.InitialDirectory = "D:\\";
  5. dialog.Filter = "所有文件|*.*";
  6. dialog.RestoreDirectory = true;
  7. dialog.FilterIndex = 2;
  8. if (dialog.ShowDialog() == DialogResult.OK)
  9. {
  10. if (string.IsNullOrEmpty(textBox2.Text))
  11. {
  12. textBox2.Text = dialog.FileName;
  13. }
  14. else
  15. {
  16. textBox3.Text = dialog.FileName;
  17. }
  18. }
  19. }

5、人脸对比

(1)验证输入:

首先,检查textBox2和textBox3是否都包含了有效的文本。这两个文本框可能分别用于输入两张待比对的人脸图片的路径。如果任何一个文本框为空(即用户没有输入图片路径),则显示一个消息框提示用户“请选择要对比的人脸图片”,并立即退出该方法,不进行后续操作。

(2)读取图片并转换为Base64编码:

ReadImg用于读取图片文件并将其转换为Base64编码的字符串。从textBox2和textBox3中获取图片路径,并分别调用ReadImg方法将图片转换为Base64编码的字符串。

(3)构建请求数据:

使用JArray和JObject构建了一个包含两张人脸图片信息的JSON数组faces。对于每张图片,都包含了一个JSON对象,该对象描述了图片的Base64编码、图片类型(这里固定为"BASE64")、人脸类型(这里固定为"LIVE",表示活体检测)、质量控制(这里设置为"LOW")和活体检测控制(这里设置为"NONE",表示不进行活体检测)。

(4)调用人脸比对API:

使用构建好的faces数组作为参数,调用client.Match方法。client.Match方法执行人脸比对操作,并返回比对结果。

(5)处理并显示结果:

将client.Match方法的返回结果转换为字符串,并显示在textBox1中。

  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3. if (string.IsNullOrEmpty(textBox2.Text) || string.IsNullOrEmpty(textBox3.Text))
  4. {
  5. MessageBox.Show("请选择要对比的人脸图片");
  6. return;
  7. }
  8. try
  9. {
  10. string path1=textBox2.Text;
  11. string path2=textBox3.Text;
  12. var faces = new JArray
  13. {
  14. new JObject
  15. {
  16. {"image", ReadImg(path1)},
  17. {"image_type", "BASE64"},
  18. {"face_type", "LIVE"},
  19. {"quality_control", "LOW"},
  20. {"liveness_control", "NONE"},
  21. },
  22. new JObject
  23. {
  24. {"image", ReadImg(path2)},
  25. {"image_type", "BASE64"},
  26. {"face_type", "LIVE"},
  27. {"quality_control", "LOW"},
  28. {"liveness_control", "NONE"},
  29. }
  30. };
  31. // 带参数调用人脸比对
  32. var result = client.Match(faces);
  33. textBox1.Text = result.ToString();
  34. }
  35. catch (Exception ex)
  36. { }
  37. }

运行结果

三、总结

本次实验,成功调用百度ai人脸识别,实现了人脸检测和两张人脸图片的比对功能。可以通过界面选择待比对的图片,程序能够自动读取图片、调用人脸识别API进行比对,并在界面上显示比对结果。最后,我意识到人脸比对技术在日常实习工作技术领域中具有广泛的应用前景。无论是身份验证、安防监控还是社交娱乐等领域,都需要用到人脸比对技术。通过本次实验,我不仅掌握了相关的编程技能,还了解了人脸比对技术的原理和应用场景,这将为我在实习工作中更好地应用该技术提供有力的支持。

标签: 人工智能 windows c#

本文转载自: https://blog.csdn.net/henjiandan___/article/details/139886833
版权归原作者 henjiandan___ 所有, 如有侵权,请联系我们删除。

“使用百度ai人脸识别,实现人脸检测、人脸对比”的评论:

还没有评论