这里我用的是百度的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{
{"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 { get; set; }
public string log_id { get; set; }
}
定义json解析函数:
RootObject rb = JsonConvert.DeserializeObject(result);
List 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识别也有失策的时候)
备注:测试图片从网上荡来的,如有侵权,请及时联系我下撤。