後臺接入face++人臉識別

 


調用api實現源碼

/**
	 * 調用Face++ API實現人臉檢測
	 * 
	 * @param picUrl 待檢測圖片的訪問地址
	 * @return List<Face> 人臉列表
	 */
	private static List<Face> faceDetect(String picUrl) {
		List<Face> faceList = new ArrayList<Face>();
		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<String, String> map = new HashMap<>();
	      HashMap<String, byte[]> 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;
	}

 

 

點擊掃碼體驗

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章