Baidu AI开放平台人脸检测使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/FINE_ii/article/details/76805134

最近搜索百度云的时候无意间发现百度云计算的官网,也就是百度AI
-面向企业及开发者的智能云计算服务平台
没事点进去看了看,发现百度云的人工智能下有个人脸识别的解决方案,在人脸识别这一块发现网页上检测人脸特征点的测试。我上传了好几张照片,发现这个AI经过特征点匹配得出来的特征点非常准确,而且还有如检测人的性别、年龄、肤色等等。而且发现算法可以作为开发使用,所以准备将算法移植到Win32控制台上。
BaiduAI人脸检测图
它的技术文档有很多支持,如C#,Java,PHP,Python等,https://cloud.baidu.com/doc/FACE/index.html
C#下的技术支持https://cloud.baidu.com/doc/FACE/Face-Csharp-SDK.html#.E6.8E.A5.E5.8F.A3.E8.83.BD.E5.8A.9B
C#下离线SDK的下载http://ai.baidu.com/sdk#sdk-category-bfr
参考其技术文档,移植到Windows控制台应用,添加Dll引用并添加FaceDemo.cs文件。

AI提供的模板FaceDemo.cs文件

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using Baidu.Aip;
using Newtonsoft.Json;

namespace Baidu.Aip.Demo
{
    class FaceDemo
    {
        public static void FaceMatch()
        {
            var client = new Baidu.Aip.Face.Face("Api Key", "Secret Key");
            var image1 = File.ReadAllBytes("图片文件路径");
            var image2 = File.ReadAllBytes("图片文件路径");
            var image3 = File.ReadAllBytes("图片文件路径");

            var images = new byte[][] {image1, image2, image3};

            var result = client.FaceMatch(images);
        }

        public static void FaceDetect()
        {
            var client = new Baidu.Aip.Face.Face("Api Key", "Secret Key");          
            var image = File.ReadAllBytes(@"1.jpg");
            var options = new Dictionary<string, object>()
            {
                {"face_fields", "age,beauty,expression,faceshape,gender,glasses,landmark,race,qualities"}
            };
            var result = client.FaceDetect(image, options);
            Console.WriteLine(result);
            string resultString = result.ToString();
            byte[] byteArray = System.Text.Encoding.Default.GetBytes(resultString);
            FileStream file = new FileStream("Result_Sdk.txt", FileMode.OpenOrCreate);
            file.Write(byteArray, 0, byteArray.Length);
            file.Flush();
            file.Close();
        }

        public static void FaceRegister()
        {
            var client = new Baidu.Aip.Face.Face("Api Key", "Secret Key");
            var image1 = File.ReadAllBytes("图片文件路径");

            var result = client.User.Register(image1, "uid", "user info here", new []{"groupId"});
        }

        public static void FaceUpdate()
        {
            var client = new Baidu.Aip.Face.Face("Api Key", "Secret Key");
            var image1 = File.ReadAllBytes("图片文件路径");

            var result = client.User.Update(image1, "uid", "groupId", "new user info");
        }

        public static void FaceDelete()
        {
            var client = new Baidu.Aip.Face.Face("Api Key", "Secret Key");
            var result = client.User.Delete("uid");
            result = client.User.Delete("uid", new []{"group1"});
        }

        public static void FaceVerify()
        {
            var client = new Baidu.Aip.Face.Face("Api Key", "Secret Key");
            var image1 = File.ReadAllBytes("图片文件路径");

            var result = client.User.Verify(image1, "uid", new []{"groupId"}, 1);
        }

        public static void FaceIdentify()
        {
            var client = new Baidu.Aip.Face.Face("Api Key", "Secret Key");
            var image1 = File.ReadAllBytes("图片文件路径");

            var result = client.User.Identify(image1, new []{"groupId"}, 1, 1);
        }

        public static void UserInfo()
        {
            var client = new Baidu.Aip.Face.Face("Api Key", "Secret Key");
            var result = client.User.GetInfo("uid");
        }

        public static void GroupList()
        {
            var client = new Baidu.Aip.Face.Face("Api Key", "Secret Key");
            var result = client.Group.GetAllGroups(0, 100);
        }

        public static void GroupUsers()
        {
            var client = new Baidu.Aip.Face.Face("Api Key", "Secret Key");
            var result = client.Group.GetUsers("groupId", 0, 100);
        }

        public static void GroupAddUser()
        {
            var client = new Baidu.Aip.Face.Face("Api Key", "Secret Key");
            var result = client.Group.AddUser(new []{"toGroupId"}, "uid", "fromGroupId");
        }

        public static void GroupDeleteUser()
        {
            var client = new Baidu.Aip.Face.Face("Api Key", "Secret Key");
            var result = client.Group.DeleteUser(new []{"groupId"}, "uid");
        }

    }
}

这里使用AI提供的ApiKey和SecretKey来替换程序的”ApiKey”和”SecretKey”,获取可以参考http://ai.baidu.com/docs#/Begin/top,其中@”1.jpg”为要检测的人脸的图像。

用户主程序入口Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
using Baidu.Aip;
using Newtonsoft.Json;
using Baidu.Aip.Demo;

namespace _AIFace
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This is a face detect test program with Baidu.AI\n");

            //访问静态方法
            FaceDemo.FaceDetect();

            Console.WriteLine("\nPress Enter to exit");
            while (true)
            {
                ConsoleKeyInfo info = Console.ReadKey();
                switch (info.Key)
                {
                    case ConsoleKey.Enter:
                        Environment.Exit(0);
                        break;
                    default:
                        break;
                }
            }
        }
    }
}

程序运行结果:

{
  "result_num": 1,
  "result": [
    {
      "location": {
        "left": 71,
        "top": 153,
        "width": 170,
        "height": 151
      },
      "face_probability": 1,
      "rotation_angle": -9,
      "yaw": 6.1190032958984,
      "pitch": 12.54426574707,
      "roll": -9.3933248519897,
      "landmark": [
        {
          "x": 112,
          "y": 174
        },
        {
          "x": 189,
          "y": 161
        },
        {
          "x": 153,
          "y": 223
        },
        {
          "x": 164,
          "y": 254
        }
      ],
      "landmark72": [
        {
          "x": 73,
          "y": 166
        },
        ...

        //72个特征点的位置坐标就不一一列出了

        ...
        {
          "x": 153,
          "y": 255
        }
      ],
      "age": 21.859697341919,
      "beauty": 68.754791259766,
      "expression": 0,
      "expression_probablity": 0.99997997283936,
      "faceshape": [
        {
          "type": "square",
          "probability": 0.0030876870732754
        },
        {
          "type": "triangle",
          "probability": 0.00013317406410351
        },
        {
          "type": "oval",
          "probability": 0.15438774228096
        },
        {
          "type": "heart",
          "probability": 0.79370421171188
        },
        {
          "type": "round",
          "probability": 0.048687182366848
        }
      ],
      "gender": "female",
      "gender_probability": 0.99999630451202,
      "glasses": 0,
      "glasses_probability": 0.99999976158142,
      "race": "yellow",
      "race_probability": 0.99999988079071,
      "qualities": {
        "occlusion": {
          "left_eye": 0,
          "right_eye": 0,
          "nose": 0,
          "mouth": 0,
          "left_cheek": 0,
          "right_cheek": 0,
          "chin": 0
        },
        "blur": 0,
        "illumination": 0,
        "completeness": 0,
        "type": {
          "human": 0.90241307020187,
          "cartoon": 0.097586907446384
        }
      }
    }
  ],
  "log_id": 558853559
}
}

百度AI开放平台提供的人脸检测的程序相对于Dlib和OpenCV人脸检测来说实用起来还是相对简单的,而且可以检测请求图片中的人脸,返回人脸位置、72个关键点坐标、及人脸相关属性信息,如检测人的年龄、性别、肤色及人脸的旋转角度。
作为简单的特征点提取和人脸属性的提取时,它的实用性和方便性还是很不错的,在AI提供的SDK模板中,也支持对人脸的识别、对比、认证、注册、更新、删除等。如果感兴趣可以继续深入了解。

猜你喜欢

转载自blog.csdn.net/FINE_ii/article/details/76805134