该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

package com.tz.util;

import java.io.File;

import org.json.JSONArray;

import org.json.JSONObject;

import com.facepp.http.HttpRequests;

import com.facepp.http.PostParameters;

/**

* 人工智能人脸识别的工具类

* @author 城

* @version v1.0

*/

public class FaceMessageUtil {

/**

* 人脸识别工具类

* @param path 需要被识别的图像路径

* @return String 返回的人脸识别后的信息

*/

public static String getFaceMessage(String path){

try {

// 创建http请求

HttpRequests hrs =new HttpRequests("uhqTWEu1sP6mA28de0WkyFAO844739mj","X0zajAlHITVUlP4NpxjIHP-_R_vpaCLs");

PostParameters pps =new PostParameters();

pps.setImg(new File(path));

JSONObject json = hrs.detectionDetect(pps);

// 设置只识别脸部

JSONArray array = json.getJSONArray("face");

//存储人脸信息

StringBuffer buffer =new StringBuffer();

for(int i=0;i

JSONObject jo =array.getJSONObject(i);

JSONObject attrJson =jo.getJSONObject("attribute");

//年龄

JSONObject jsonAge = attrJson.getJSONObject("age");

int range=jsonAge.getInt("range");

int value=jsonAge.getInt("value");

buffer.append("年龄:"+value+"岁");

//性别

String genderStr =attrJson.getJSONObject("gender").getString("value");

Double confidence =attrJson.getJSONObject("gender").getDouble("confidence");

buffer.append("性别:"+genderStr+"(正确率:"+confidence+"%)
");

//种族

String raceStr = attrJson.getJSONObject("race").getString("value");

Double raceConfindence=attrJson.getJSONObject("race").getDouble("confidence");

buffer.append("种族:"+raceStr+"(正确率:"+raceConfindence+"%)
");

//是否在笑

Double smiling =attrJson.getJSONObject("smiling").getDouble("value");

buffer.append("正在笑:"+smiling+"%");

}

return buffer.toString();

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

public static void main (String[] args){

System.out.println("测试人脸识别");

String path="D:\\kankan\\1030.jpg";

String face = getFaceMessage(path);

System.out.println(face);

//1.获取需要被识别的图像,进行人脸识别

//2.通过Face++识别人脸细节

//3.存储在容器中返回到页面中进行展示,同步到数据库中

}

}

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐