测试百度人脸检测的例子-18/04/07(参考网上)

阅读更多

public class FaceDetect {

    /**
    * 重要提示代码中所需工具类
    * FileUtil,Base64Util,HttpUtil,GsonUtils请从
    * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
    * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
    * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
    * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
    * 下载
    */
    public static String detect(String filePath) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v1/detect";
        try {
            // 本地文件路径
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");
            //,默认只返回人脸框、概率和旋转角度
            String param = "max_face_num=" + 5 + "&image=" + imgParam;
         //以下显示face_fields 否 String 包括age,beauty,expression,faceshape,gender,glasses,landmark,race,qualities信息,逗号分隔
         //   String param = "max_face_num=" + 5 + "&face_fields=" + "age,beauty,expression,faceshape,gender,glasses,landmark,race,qualities" + "&image=" + imgParam;
            // 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
            String accessToken = AuthService.getAuth();

            String result = HttpUtil.post(url, accessToken, param);
            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
   
    public static void main(String[] args) {
    String filePath = "D:/detect2.jpg";
    detect(filePath);
    }

}

输出结果:

{"result_num":1,"result":[{"location":{"left":44,"top":70,"width":109,"height":117},"face_probability":1,"rotation_angle":3,"yaw":-1.5595326423645,"pitch":-0.52279132604599,"roll":2.8435711860657,"landmark":[{"x":72,"y":91},{"x":124,"y":94},{"x":97,"y":118},{"x":95,"y":146}],"landmark72":[{"x":42,"y":92},{"x":42,"y":111},{"x":43,"y":130},{"x":45,"y":149},{"x":56,"y":168},{"x":73,"y":184},{"x":91,"y":190},{"x":110,"y":186},{"x":128,"y":173},{"x":141,"y":155},{"x":147,"y":137},{"x":150,"y":118},{"x":152,"y":100},{"x":60,"y":92},{"x":66,"y":88},{"x":72,"y":87},{"x":79,"y":89},{"x":84,"y":93},{"x":78,"y":95},{"x":72,"y":95},{"x":66,"y":94},{"x":72,"y":91},{"x":52,"y":82},{"x":60,"y":74},{"x":71,"y":72},{"x":81,"y":74},{"x":90,"y":81},{"x":80,"y":80},{"x":71,"y":79},{"x":61,"y":80},{"x":112,"y":95},{"x":118,"y":91},{"x":124,"y":90},{"x":130,"y":92},{"x":135,"y":96},{"x":130,"y":98},{"x":124,"y":98},{"x":118,"y":97},{"x":124,"y":94},{"x":108,"y":82},{"x":117,"y":76},{"x":127,"y":75},{"x":137,"y":77},{"x":143,"y":86},{"x":135,"y":83},{"x":127,"y":81},{"x":118,"y":82},{"x":91,"y":94},{"x":88,"y":102},{"x":85,"y":110},{"x":79,"y":121},{"x":88,"y":123},{"x":106,"y":125},{"x":114,"y":123},{"x":110,"y":112},{"x":108,"y":103},{"x":106,"y":95},{"x":97,"y":118},{"x":71,"y":141},{"x":83,"y":138},{"x":96,"y":140},{"x":108,"y":140},{"x":119,"y":145},{"x":109,"y":155},{"x":94,"y":158},{"x":80,"y":153},{"x":83,"y":142},{"x":95,"y":144},{"x":107,"y":144},{"x":107,"y":150},{"x":95,"y":151},{"x":82,"y":148}],"age":35.630561828613,"beauty":49.604648590088,"expression":1,"expression_probablity":0.99998414516449,"faceshape":[{"type":"square","probability":0.049925819039345},{"type":"triangle","probability":0.10576634854078},{"type":"oval","probability":0.73162740468979},{"type":"heart","probability":0.0030444983858615},{"type":"round","probability":0.10963594168425}],"gender":"male","gender_probability":0.99999749660492,"glasses":0,"glasses_probability":0.99857616424561,"race":"white","race_probability":0.99962675571442,"qualities":{"occlusion":{"left_eye":0,"right_eye":0,"nose":0,"mouth":0,"left_cheek":0,"right_cheek":0,"chin":0},"blur":0,"illumination":0,"completeness":0,"type":{"human":0.9997450709343,"cartoon":0.00025490892585367}}}],"log_id":3556777629}

result_num 人脸数目 age 年龄 race 人种 具体参考文档中心

你可能感兴趣的:(人脸检测)