百度人脸识别 返回json 数据提取

版权声明:本博客系博主原创或转载,允许转载,但请保留原文出处。 https://blog.csdn.net/heivy/article/details/81391906

我们直奔主题:
下面为得到的人脸检测的一个数据样例:

result = {{
  "error_code": 0,
  "error_msg": "SUCCESS",
  "log_id": 9425351545101,
  "timestamp": 1533196016,
  "cached": 0,
  "result": {
    "face_num": 3,
    "face_list": [
      {
        "face_token": "1929940fc6cd6d260f9232bdf85b576f",
        "location": {
          "left": 87.22742462,
          "top": 159.038208,
          "width": 71,
          "height": 65,
          "rotation": 5
        },
        "face_probability": 1,
        "angle": {
          "yaw": -4.833847046,
          "pitch": -8.676548004,
          "roll": 4.778661251
        }
      },
      {
        "face_token": "e2cdebd565d7ab9c4087f998bdf9f1c2",
        "location": {
          "left": 283.049469,
          "top": 209.8593292,
          "width": 70,
          "height": 64,
          "rotation": 8
        },
        "face_probability": 1,
        "angle": {
          "yaw": -7.892694473,
          "pitch": -4.45338726,
          "roll": 6.554013252
        }
      },
      {
        "face_token": "ab8205865df39afe8b562f1f33744bb0",
        "location": {
          "left": 39.11988449,
          "top": 420.4474792,
          "width": 69,
          "height": 53,
          "rotation": -11
        },
        "face_probability": 0.8695431948,
        "angle": {
          "yaw": -42.27268982,
          "pitch": 9.21941185,
          "roll": -12.62914181
        }
      }
    ]
  }
}}

从上面可以看出:此数据结构有4层:root层–result层–face_list 层–location、angle层

接下来我们从内到外进行数据的类定义:

public class Location
        {
            public float Left { get; set; }
            public float Top { get; set; }
            public float Width { get; set; }
            public float Height { get; set; }
            public float Rotation { get; set; }

        }
        public class Angle
        {
            public string Yaw { get; set; }
            public string Pitch { get; set; }
            public string Roll { get; set; }
        }

N: 成员名称要和json数据里面的key 一致(命名和格式保持一样,同时首字母大写)
下面再一层层向外延伸

public class FaceList
        {

            public string Face_token { get; set; }
            public Location Location { get; set; }
            public string Face_probability { get; set; }
            public Angle Angle { get; set; }

        }

        public  class FaceResult
        {
            public int  Face_num { get; set; }
            public  List<FaceList> Face_list { get; set; }
        }
        public class RootResult
        {
            public string Error_code { get; set; }
            public string Error_msg { get; set; }
            public string Log_id { get; set; }
            public string Timestamp { get; set; }
            public string Cached { get; set; }
            public FaceResult Result { get; set; }
        }

数据的提取函数:

var result = client.Detect(image, "BASE64", options);
RootResult rt = JsonConvert.DeserializeObject<RootResult>(result.ToString());

result 是百度返回给我们的人脸检测的结果信息;
我们要提取结果里面的某一个参数值时,需要先把其进行标准化;本文中结果标准如上所示 ##RootResult ##类

这样我们就可以取值了:例如:脸一的人脸宽度

string facewidth = rt.Result.Face_list[0].Location.Width

猜你喜欢

转载自blog.csdn.net/heivy/article/details/81391906