后台接入face++人脸识别

后台接入face++人脸识别_第1张图片

 


调用api实现源码

/**
	 * 调用Face++ API实现人脸检测
	 * 
	 * @param picUrl 待检测图片的访问地址
	 * @return List 人脸列表
	 */
	private static List faceDetect(String picUrl) {
		List faceList = new ArrayList();
		try {
//		  String picUrl = "http://pic11.nipic.com/20101111/6153002_002722872554_2.jpg";
		  byte[] buff = getFileStream(picUrl);
		  String url = "https://api-cn.faceplusplus.com/facepp/v3/detect";
	      HashMap map = new HashMap<>();
	      HashMap byteMap = new HashMap<>();
	      map.put("api_key", "你的api_key");
	      map.put("api_secret", "你的api_secret");
		  map.put("return_landmark", "1");
	      map.put("return_attributes", "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus");
	      byteMap.put("image_file", buff);
          byte[] bacd = post(url, map, byteMap);
          String json = new String(bacd);
          System.out.println(json);
			// 解析返回json中的Face列表
			JSONArray jsonArray = JSONObject.fromObject(json).getJSONArray("faces");
			// 遍历检测到的人脸
			for (int i = 0; i < jsonArray.size(); i++) {
				// face
				JSONObject faceObject = (JSONObject) jsonArray.get(i);
				// attribute
				JSONObject attrObject = faceObject.getJSONObject("attributes");
				// position
				JSONObject posObject = faceObject.getJSONObject("face_rectangle");
				Face face = new Face();
				face.setFaceId(faceObject.getString("face_token"));
				face.setAgeValue(attrObject.getJSONObject("age").getInt("value"));
//				face.setAgeRange(attrObject.getJSONObject("age").getInt("range"));
				face.setGenderValue(genderConvert(attrObject.getJSONObject("gender").getString("value")));
//				face.setGenderConfidence(attrObject.getJSONObject("gender").getDouble("confidence"));
				face.setRaceValue(raceConvert(attrObject.getJSONObject("ethnicity").getString("value")));
//				face.setRaceConfidence(attrObject.getJSONObject("race").getDouble("confidence"));
				face.setSmilingValue(attrObject.getJSONObject("smile").getDouble("value"));
				face.setCenterX(posObject.getDouble("left") + posObject.getDouble("width"));
				face.setCenterY(posObject.getDouble("top") + posObject.getDouble("height"));
				faceList.add(face);
			}
		// 将检测出的Face按从左至右的顺序排序
		Collections.sort(faceList);
		} catch (Exception e) {
			faceList = null;
			e.printStackTrace();
		}
		return faceList;
	}

 

 

点击扫码体验

后台接入face++人脸识别_第2张图片

 

你可能感兴趣的:(后台接入face++人脸识别)