【人体分析-人体属性识别】-Java示例代码
756665228 发布于2018-02-28 11:29 浏览:1162 回复:4
5
收藏

人体属性识别,即对于输入的一张图片(可正常解码,且长宽比适宜),输出图片中的所有人体的静态属性,包含坐标信息、性别、年龄阶段、衣着(含颜色)、是否带帽子、是否带眼镜、是否背包、是否抽烟、是否使用手机、动作姿态等(如站立/坐/躺…)

-----------------------------------------------------下面开始代码----------------------------------------------------

  • 人体属性识别接口示例代码-(JavaAPI)
package com.xs.image;

import java.net.URLEncoder;

import com.alibaba.fastjson.JSON;
import com.xs.common.image.ImageAPI;
import com.xs.pojo.image.BodyAttrBean;
import com.xs.util.baidu.Base64Util;
import com.xs.util.baidu.FileUtil;
import com.xs.util.baidu.HttpUtil;

public class BodyAttrSample {
	public static void main(String[] args) throws Exception {
		String lower_color = "";
		//返回字符串
//		String result = getBodyAttrResult("G:/girl.jpg", "自己的token");
//		System.out.println(result);
		//返回java对象
		BodyAttrBean bodyAttrBean = getBodyAttrean("G:/girl.jpg", "自己的token");
		if(bodyAttrBean.getPerson_info().get(0).getAttributes().getLower_color()==null){
			lower_color="未能分析出";
		}else{
			lower_color=bodyAttrBean.getPerson_info().get(0).getAttributes().getLower_color().getName();
		}
		System.out.println("图中有位:"+bodyAttrBean.getPerson_info().get(0).getAttributes().getAge().getName()+"-"+bodyAttrBean.getPerson_info().get(0).getAttributes().getGender().getName()
				+",上身穿着:"+bodyAttrBean.getPerson_info().get(0).getAttributes().getUpper_wear().getName()
				+",下身穿着:"+bodyAttrBean.getPerson_info().get(0).getAttributes().getLower_wear().getName()
				+",上衣颜色:"+bodyAttrBean.getPerson_info().get(0).getAttributes().getUpper_color().getName()
				+",下衣颜色:"+lower_color
				+",动作姿态:"+bodyAttrBean.getPerson_info().get(0).getAttributes().getAction().getName());
	}
	/**
	 * 人体属性识别Demo
	 * @param imagePath
	 * @param accessToken
	 * @return 字符串
	 * @throws Exception
	 */
	public static String getBodyAttrResult(String imagePath,String accessToken) throws Exception{
		byte[] imgData = FileUtil.readFileByBytes(imagePath);
        String imgStr = Base64Util.encode(imgData);
		String param = "image=" + URLEncoder.encode(imgStr,"UTF-8")+"&type=gender,age,action, hair_length, bag,lower_wear,upper_wear, headwear,glasses,upper_color, lower_color,smoke,cellphone";;
        // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
		String result = HttpUtil.post(ImageAPI.BODYATTR_API, accessToken, param);
        System.out.println(result);
        return result;
	}
	/**
	 * 人体属性识别Demo
	 * @param imagePath
	 * @param accessToken
	 * @return CarMode对象
	 * @throws Exception
	 */
	public static BodyAttrBean getBodyAttrean(String imagePath,String accessToken) throws Exception{
		byte[] imgData = FileUtil.readFileByBytes(imagePath);
        String imgStr = Base64Util.encode(imgData);
		String param = "image=" + URLEncoder.encode(imgStr,"UTF-8")+"&type=gender,age,action, hair_length, bag,lower_wear,upper_wear, headwear,glasses,upper_color, lower_color,smoke,cellphone";
        // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
		String result = HttpUtil.post(ImageAPI.BODYATTR_API, accessToken, param);
		BodyAttrBean bodyAttrBean = JSON.parseObject(result,BodyAttrBean.class);
        System.out.println(result);
        return bodyAttrBean;
	}
}
  • 识别返回的JSON数据(特别长,不做格式化)
{"person_num": 1, "person_info": [{"attributes": {"cellphone": {"score": 0.7408543825149536, "name": "未使用手机"}, "gender": {"score": 0.9897438883781433, "name": "女性"}, "age": {"score": 0.6026508212089539, "name": "青年"}, "smoke": {"score": 0.9972188472747803, "name": "未吸烟"}, "lower_wear": {"score": 0.6392033100128174, "name": "长裤"}, "action": {"score": 0.6042985916137695, "name": "站立"}, "glasses": {"score": 0.6502364277839661, "name": "无眼镜"}, "upper_wear": {"score": 0.5536559820175171, "name": "短袖"}, "upper_color": {"score": 0.7737929821014404, "name": "蓝"}}, "location": {"width": 316, "top": 233, "height": 516, "left": 102}}], "log_id": 1210489514564349440}
  • 识别返处理后的数据输出
图中有位:青年-女性,上身穿着:短袖,下身穿着:长裤,上衣颜色:蓝,下衣颜色:未能分析出,动作姿态:站立
  • 原图文件

 

-----------------------------------------------------代码结束-----------------------------------------------------

 

字符串返回不太方便处理。那就对象处理。不会转对象的。我已经帮忙做了。

  • BodyAttrBean
package com.xs.pojo.image;

import java.util.List;

/**
 * 人体属性识别
 * @author 小帅丶
 *
 */
public class BodyAttrBean {
	//人体数目
    private int person_num;
    //人体姿态信息
    private List<Person_info> person_info;
    //唯一的log id,用于问题定位
    private long log_id;
    
    public int getPerson_num() {
		return person_num;
	}
	public void setPerson_num(int person_num) {
		this.person_num = person_num;
	}
	public List<Person_info> getPerson_info() {
		return person_info;
	}
	public void setPerson_info(List<Person_info> person_info) {
		this.person_info = person_info;
	}
	public long getLog_id() {
		return log_id;
	}
	public void setLog_id(long log_id) {
		this.log_id = log_id;
	}
	public static class Person_info{
    	  private Attributes attributes;
    	  private Location location;
		public Attributes getAttributes() {
			return attributes;
		}
		public void setAttributes(Attributes attributes) {
			this.attributes = attributes;
		}
		public Location getLocation() {
			return location;
		}
		public void setLocation(Location location) {
			this.location = location;
		}
    	  
    }
    public static class Attributes{
    	private Gender gender;
    	private Age age;
    	private Action action;
    	private Upper_color upper_color;
    	private Lower_color lower_color;
    	private Hair_length hair_length;
    	private Smoke smoke;
    	private Cellphone cellphone;
        private Lower_wear lower_wear;
        private Upper_wear upper_wear;
        private Headwear headwear;
        private Glasses glasses;
		public Hair_length getHair_length() {
			return hair_length;
		}
		public void setHair_length(Hair_length hair_length) {
			this.hair_length = hair_length;
		}
		public Headwear getHeadwear() {
			return headwear;
		}
		public void setHeadwear(Headwear headwear) {
			this.headwear = headwear;
		}
		public Lower_color getLower_color() {
			return lower_color;
		}
		public void setLower_color(Lower_color lower_color) {
			this.lower_color = lower_color;
		}
		public Cellphone getCellphone() {
			return cellphone;
		}
		public void setCellphone(Cellphone cellphone) {
			this.cellphone = cellphone;
		}
		public Gender getGender() {
			return gender;
		}
		public void setGender(Gender gender) {
			this.gender = gender;
		}
		public Age getAge() {
			return age;
		}
		public void setAge(Age age) {
			this.age = age;
		}
		public Smoke getSmoke() {
			return smoke;
		}
		public void setSmoke(Smoke smoke) {
			this.smoke = smoke;
		}
		public Lower_wear getLower_wear() {
			return lower_wear;
		}
		public void setLower_wear(Lower_wear lower_wear) {
			this.lower_wear = lower_wear;
		}
		public Action getAction() {
			return action;
		}
		public void setAction(Action action) {
			this.action = action;
		}
		public Glasses getGlasses() {
			return glasses;
		}
		public void setGlasses(Glasses glasses) {
			this.glasses = glasses;
		}
		public Upper_wear getUpper_wear() {
			return upper_wear;
		}
		public void setUpper_wear(Upper_wear upper_wear) {
			this.upper_wear = upper_wear;
		}
		public Upper_color getUpper_color() {
			return upper_color;
		}
		public void setUpper_color(Upper_color upper_color) {
			this.upper_color = upper_color;
		}
        
  }
  public static class Cellphone{
	  private double score;
	  private String name;
		public double getScore() {
			return score;
		}
		public void setScore(double score) {
			this.score = score;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
  }
  public static class Hair_length{
	  private double score;
	  private String name;
		public double getScore() {
			return score;
		}
		public void setScore(double score) {
			this.score = score;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
  }
  public static class Headwear{
	  private double score;
	  private String name;
		public double getScore() {
			return score;
		}
		public void setScore(double score) {
			this.score = score;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
  }
  public static class Gender{
	  private double score;
	  private String name;
		public double getScore() {
			return score;
		}
		public void setScore(double score) {
			this.score = score;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
  }
  public static class Age{
	  private double score;
	  private String name;
		public double getScore() {
			return score;
		}
		public void setScore(double score) {
			this.score = score;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
  }
  public static class Smoke{
	  private double score;
	  private String name;
		public double getScore() {
			return score;
		}
		public void setScore(double score) {
			this.score = score;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
  }
  public static class Lower_wear{
	  private double score;
	  private String name;
		public double getScore() {
			return score;
		}
		public void setScore(double score) {
			this.score = score;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
  }
  public static class Lower_color{
	  private double score;
	  private String name;
		public double getScore() {
			return score;
		}
		public void setScore(double score) {
			this.score = score;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
  }
  public static class Action{
	  private double score;
	  private String name;
		public double getScore() {
			return score;
		}
		public void setScore(double score) {
			this.score = score;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
  }
  public static class Glasses{
	  private double score;
	  private String name;
		public double getScore() {
			return score;
		}
		public void setScore(double score) {
			this.score = score;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
  }
  public static class Upper_wear{
	  private double score;
	  private String name;
		public double getScore() {
			return score;
		}
		public void setScore(double score) {
			this.score = score;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
  }
  public static class Upper_color{
	  private double score;
	  private String name;
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		this.score = score;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	  
  }
   public static class Location{
    	private int width;
	    private int top;
	    private int height;
	    private int left;
		public int getWidth() {
			return width;
		}
		public void setWidth(int width) {
			this.width = width;
		}
		public int getTop() {
			return top;
		}
		public void setTop(int top) {
			this.top = top;
		}
		public int getHeight() {
			return height;
		}
		public void setHeight(int height) {
			this.height = height;
		}
		public int getLeft() {
			return left;
		}
		public void setLeft(int left) {
			this.left = left;
		}
	    
  }

 

  • 字段简单说明
| 字段            | 是否必选 | 类型       | 说明               |
| ------------- | ---- | -------- | ---------------- |
| person_info   | 是    | object[] | 人体姿态信息           |
| +attributes   | 否    | object   | 人体属性内容           |
| ++gender      | 否    | object   | 性别               |
| +++name       | 否    | string   | 如“男性”            |
| +++score      | 否    | float    | 对应概率分数           |
| ++age         | 否    | object   | 年龄阶段             |
| +++name       | 否    | string   | 如“青年”            |
| +++score      | 否    | float    | 对应概率分数           |
| ++action      | 否    | object   | 动作姿态             |
| +++name       | 否    | string   | 如“站立”            |
| +++score      | 否    | float    | 对应概率分数           |
| ++upper_color | 否    | object   | 上半身衣着颜色          |
| +++name       | 否    | string   | 如“灰”             |
| +++score      | 否    | float    | 对应概率分数           |
| ++lower_color | 否    | object   | 下半身衣着颜色          |
| +++name       | 否    | string   | 如“黑”             |
| +++score      | 否    | float    | 对应概率分数           |
| ++hair_length | 否    | object   | 发长               |
| +++name       | 否    | string   | 如“短发”            |
| +++score      | 否    | float    | 对应概率分数           |
| ++smoke       | 否    | object   | 是否吸烟             |
| +++name       | 否    | string   | 如“未吸烟”           |
| +++score      | 否    | float    | 对应概率分数           |
| ++bag         | 否    | object   | 身体附件(主要指是否背包)    |
| +++name       | 否    | string   | 如“双肩包”           |
| +++score      | 否    | float    | 对应概率分数           |
| ++cellphone   | 否    | object   | 是否使用手机           |
| +++name       | 否    | string   | 如“未使用手机”         |
| +++score      | 否    | float    | 对应概率分数           |
| ++lower_wear  | 否    | object   | 下半身服饰            |
| +++name       | 否    | string   | 如“休闲裤”           |
| +++score      | 否    | float    | 对应概率分数           |
| ++upper_wear  | 否    | object   | 上半身服饰            |
| +++name       | 否    | string   | 如“西装”            |
| +++score      | 否    | float    | 对应概率分数           |
| ++headwear    | 否    | object   | 头戴物(主要指是否戴帽子)    |
| +++name       | 否    | string   | 如“无帽”            |
| +++score      | 否    | float    | 对应概率分数           |
| ++glasses     | 否    | object   | 是否戴眼镜            |
| +++name       | 否    | string   | 如“戴眼镜”           |
| +++score      | 否    | float    | 对应概率分数           |
| +location     | 是    | object   | 人体坐标信息           |
| ++height      | 是    | float    | 人体区域的高度          |
| ++left        | 是    | float    | 人体区域离左边界的距离      |
| ++top         | 是    | float    | 人体区域离上边界的距离      |
| ++width       | 是    | float    | 人体区域的宽度          |
| person_num    | 是    | uint32   | **人体数目**         |
| log_id        | 是    | uint64   | 唯一的log id,用于问题定位 |

 

以上就是人体属性识别示例代码-JavaAPI形式

代码地址:https://gitee.com/xshuai/ai/blob/master/AIDemo/src/main/java/com/xs/image/BodyAnalysisSample.java

收藏
点赞
5
个赞
共4条回复 最后由goJhou回复于2019-01-20 23:03
#5goJhou回复于2019-01-20 23:03:33
#4 呜污污污污回复
请问我该怎么办啊?错误代码3,Unsupported openapi method 调用的API不存在,请检查请求URL后重新尝试,一般为URL中有非英文字符,如“-”,可手动输入重试。调试信息如下: 打开链接,开始发送请求1546071813 null--->[HTTP/1.1 200 OK] Server--->[Apache] Connection--->[keep-alive] Vary--->[Accept-Encoding] Set-Cookie--->[BAIDUID=746B3C5D08C5F7FBB1A86E9EDD01B023:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2145916555; path=/; domain=.baidu.com; version=1] Content-Length--->[57] P3p--->[CP=" OTI DSP COR IVA OUR IND COM "] Date--->[Sat, 29 Dec 2018 08:23:39 GMT] Content-Type--->[application/json] 请求结束1546071816 result:{"error_code":3,"error_msg":"Unsupported openapi method"} {"error_code":3,"error_msg":"Unsupported openapi method"} 图中有0个人
展开

你这给谁看呀,语言也不说。接口地址也不发。。。。

 

错误中说了可能是你url的问题

0
#4呜污污污污回复于2018-12-29 16:30:27

请问我该怎么办啊?错误代码3,Unsupported openapi method
调用的API不存在,请检查请求URL后重新尝试,一般为URL中有非英文字符,如“-”,可手动输入重试。调试信息如下:

打开链接,开始发送请求1546071813
null--->[HTTP/1.1 200 OK]
Server--->[Apache]
Connection--->[keep-alive]
Vary--->[Accept-Encoding]
Set-Cookie--->[BAIDUID=746B3C5D08C5F7FBB1A86E9EDD01B023:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2145916555; path=/; domain=.baidu.com; version=1]
Content-Length--->[57]
P3p--->[CP=" OTI DSP COR IVA OUR IND COM "]
Date--->[Sat, 29 Dec 2018 08:23:39 GMT]
Content-Type--->[application/json]
请求结束1546071816
result:{"error_code":3,"error_msg":"Unsupported openapi method"}
{"error_code":3,"error_msg":"Unsupported openapi method"}
图中有0个人

0
#3求代课1357回复于2018-10-29 22:09:29

人体属性的识别确实不易

0
#2风搅火回复于2018-03-02 08:40:47

赞一个,太烈害了

0
TOP
切换版块