百度SDK移动开发平台学习 - 人体分析

本文详细介绍使用百度SDK进行人体属性识别的过程,包括获取API密钥、调用BodyAnalysis服务、解析JSON结果及部分测试数据解析。展示了如何通过代码获取图片中人物的性别、年龄、着装等详细信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里我用的是百度的SDK:

1、连接百度开发平台:从百度管理平台应用列表可以获取到

 string API_KEY = "He53N3WsvqUiqHgdTlMYn1eF";
 string SECRET_KEY = "2zvvVYNRmQhRj76uDt6V9XOnUa6QGoBP";

 var client = new Baidu.Aip.BodyAnalysis.Body(API_KEY, SECRET_KEY);
 client.Timeout = 60000;  // 修改超时时间

2、获取图片信息:以json的形式保存在了result里面

 

var image = File.ReadAllBytes("./2.jpg");

 1、获取全部图片信息

var result = client.BodyAttr(image);

2、获取特定特征值信息

 var options = new Dictionary<string, object>{
  {"type", "gender"} };
  var result = client.BodyAttr(image, options);
  CutJson(result.ToString(), 1);

 

获取全部图片信息得到的 json结果:

{
  "person_num": 1,
  "person_info": [
    {
      "attributes": {
        "upper_wear_fg": {
          "score": 0.97389006614685059,
          "name": "T恤"
        },
        "cellphone": {
          "score": 0.99958902597427368,
          "name": "未使用手机"
        },
        "lower_cut": {
          "score": 0.98942846059799194,
          "name": "有下方截断"
        },
        "umbrella": {
          "score": 0.999944806098938,
          "name": "未打伞"
        },
        "orientation": {
          "score": 0.99943989515304565,
          "name": "正面"
        },
        "is_human": {
          "score": 0.96589845418930054,
          "name": "不确定"
        },
        "headwear": {
          "score": 0.99876141548156738,
          "name": "无帽"
        },
        "gender": {
          "score": 0.68398737907409668,
          "name": "男性"
        },
        "age": {
          "score": 0.60736602544784546,
          "name": "中年"
        },
        "upper_cut": {
          "score": 0.99997174739837646,
          "name": "无上方截断"
        },
        "glasses": {
          "score": 0.99150675535202026,
          "name": "无眼镜"
        },
        "lower_color": {
          "score": 0.69293707609176636,
          "name": "不确定"
        },
        "bag": {
          "score": 0.97373586893081665,
          "name": "无背包"
        },
        "upper_wear_texture": {
          "score": 0.86429083347320557,
          "name": "图案"
        },
        "smoke": {
          "score": 0.99937230348587036,
          "name": "未吸烟"
        },
        "vehicle": {
          "score": 0.99966096878051758,
          "name": "无交通工具"
        },
        "lower_wear": {
          "score": 0.98588055372238159,
          "name": "不确定"
        },
        "carrying_item": {
          "score": 0.684053897857666,
          "name": "无手提物"
        },
        "upper_wear": {
          "score": 0.99992609024047852,
          "name": "短袖"
        },
        "occlusion": {
          "score": 0.98508137464523315,
          "name": "无遮挡"
        },
        "upper_color": {
          "score": 0.99971216917037964,
          "name": "蓝"
        }
      },
      "location": {
        "height": 286,
        "width": 202,
        "top": 134,
        "score": 0.98026180267333984,
        "left": 297
      }
    },
  "log_id": 4793816596900492591
}

 

3、解析json

定义json解析类:

public class Upper_wear_fg {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Cellphone {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Lower_cut {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Umbrella {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Orientation {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Is_human {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Headwear {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Gender {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Age {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Upper_cut {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Glasses {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Lower_color {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Bag {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Upper_wear_texture {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Smoke {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Vehicle {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Lower_wear {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Carrying_item {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Upper_wear {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Occlusion {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Upper_color {
	public string score  { get; set; }
	public string name  { get; set; }
}

public class Attributes {
	public Upper_wear_fg upper_wear_fg { get; set; }
	public Cellphone cellphone { get; set; }
	public Lower_cut lower_cut { get; set; }
	public Umbrella umbrella { get; set; }
	public Orientation orientation { get; set; }
	public Is_human is_human { get; set; }
	public Headwear headwear { get; set; }
	public Gender gender { get; set; }
	public Age age { get; set; }
	public Upper_cut upper_cut { get; set; }
	public Glasses glasses { get; set; }
	public Lower_color lower_color { get; set; }
	public Bag bag { get; set; }
	public Upper_wear_texture upper_wear_texture { get; set; }
	public Smoke smoke { get; set; }
	public Vehicle vehicle { get; set; }
	public Lower_wear lower_wear { get; set; }
	public Carrying_item carrying_item { get; set; }
	public Upper_wear upper_wear { get; set; }
	public Occlusion occlusion { get; set; }
	public Upper_color upper_color { get; set; }
}

public class Location {
	public string height  { get; set; }
	public string width  { get; set; }
	public string top  { get; set; }
	public string score  { get; set; }
	public string left  { get; set; }
}

public class Person_info {
	public Attributes attributes { get; set; }
	public Location location { get; set; }
}

public class RootObject {
	public string person_num  { get; set; }
	public List<Person_info> person_info { get; set; }
	public string log_id  { get; set; }
}

定义json解析函数: 

                RootObject rb = JsonConvert.DeserializeObject<RootObject>(result);
                List<Person_info> person_info = rb.person_info;
                for (int i = 0; i < person_info.Count; i++)
                {
                    value = person_info[i].Attributes.gender.name.ToString()+ "  "+                 
                    person_info[i].Attributes.age.name.ToString() + "  " + 
                    person_info[i].Attributes.glasses.name.ToString() + "  " + 
                    person_info[i].Attributes.upper_wear.name.ToString()+ "  " + 
                    person_info[i].Attributes.upper_cut.name.ToString() + "  " + 
                    person_info[i].Attributes.bag.name.ToString() + "  " + 
                    person_info[i].Attributes.upper_color.name.ToString();
                    UpdateShow(value);
                }

4、测试 ,这里我只解析了部分数据(图片测试效果不太理想,百度AI识别也有失策的时候)

备注:测试图片从网上荡来的,如有侵权,请及时联系我下撤。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ya土豆儿~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值