horizon client 无法识别域_百度人脸识别服务使用

博客介绍了百度人脸识别API的使用。从注册人脸识别、确认协议、创建应用开始,给出了maven依赖和源码链接。还说明了创建运行百度人脸识别服务的application,准备工具类。最后展示了通过URL访问方式检测图片颜值得分的代码及结果。

说起来为什么玩这个呢?是因为朋友说可以给照片评分,看你颜值多少分

身为一只妹子,当然要试一下啦~

其实这个东西是个api非常非常详细的东西了

所以从注册人脸识别开始

78d41f399b91adb9ac2c65a48fc1e2fa.png

然后就是确认一个什么协议啦(就不放截图了)下一个就需要创建一个应用:

f6c2fe3bac792bd23be449932a65f1f7.png

随便填一填:

c33f9eb92073426c07cdcfeae40b4f75.png

然后就创建成功啦:

789de82e195993e04aa34915f13cce03.png

接下来有用的是哪些呢?就是截图上圈起来的内容了,等一下代码中要用到哦:

50291c29ff2531fcd611f0132cc235a1.png

先上maven:

<dependency>
    

源码链接在这里:github链接:https://github.com/Baidu-AIP/java-sdk

然后就是建一个application啦:

里面主要是要有一个main方法来运行百度人脸识别服务的(最好使用单例,这样就不用一直拿token啦)

package com.carelonger.api.util.baidu;

import com.baidu.aip.face.AipFace;
import com.carelonger.api.job.Jobs;
import com.carelonger.api.util.common.MyLog;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * 百度人脸识别处理核心
 * 作者:王大傻
 * 时间:2019年9月09日17:38:13
 * 版本:v0.2
 */
public class FaceUtil {
    // 认证参数
    private static final String APP_ID = "截图里面的App_id";
    private static final String API_KEY = "截图里面的API_key";
    private static final String SECRET_KEY = "截图里面的Secret_key";

    // 全局返回状态码
    private static final String SUCCESS = "SUCCESS";

    private static AipFace client = null;

    /**
     * 初始化
     *
     * @return
     */
    static {
        //初始化一个AipFace
        client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
        // 设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);// 建立连接的超时时间(单位:毫秒)
        client.setSocketTimeoutInMillis(60000);// 通过打开的连接传输数据的超时时间(单位:毫秒)
        // 设置代理服务器地址, http和socket二选一,或者均不设置
        // client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        // client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理
    }

    
    public static void main(String[] args) throws JSONException {
        //取决于image_type参数,传入BASE64字符串或URL字符串或FACE_TOKEN字符串
        String image = "照片的地址";
        String imageType = "URL";
        FaceUtil faceUtil = new FaceUtil();
        //人脸检测
        JSONObject jsonObject = faceUtil.faceUpload(image, imageType);
        System.out.println(jsonObject.toString(2));
        //解析并输出结果
        JSONObject res = faceUtil.faceCheck(jsonObject);
        System.out.println(res.getString("error_msg"));
        JSONObject face_list = jsonObject.getJSONObject("result").getJSONArray("face_list").getJSONObject(0);
        if (SUCCESS.equals(res.get("error_msg"))) {
            String s = faceUtil.faceSearch(face_list.getString("face_token"), "FACE_TOKEN", "test", "999").toString(2);
            System.out.println(s);
        }
    }
}

当然到这里并不是完成了呐,我们需要一些工具类:

    // 检测人脸附加参数
    private static final String FACE_FIELD = "quality";
     /**
     * 人脸检测
     */
    public JSONObject faceUpload(String image, String imageType) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", FACE_FIELD);
        options.put("liveness_control", "LOW");// 活体
        // 发起调用
        JSONObject res = client.detect(image, imageType, options);
        return res;
    }

    /**
     * 解析人脸上传结果
     */
    public JSONObject faceCheck(JSONObject res) throws JSONException {
        // 判断结果
        if (!SUCCESS.equals(res.getString("error_msg"))) {
            res.remove("error_msg");
            res.put("error_msg", "无法识别,请重试!");
            return res;
        }
        JSONObject face_list = res.getJSONObject("result").getJSONArray("face_list").getJSONObject(0);
        // 验证人脸质量
        JSONObject quality = face_list.getJSONObject("quality");
        boolean checkQuality = checkQuality(quality);
        // 验证姿态
        boolean checkAttitude = checkAttitude(face_list);
        if (!(checkQuality && checkAttitude)) {
            res.remove("error_msg");
            res.put("error_msg", "无法识别,请重试!");
            return res;
        }
        return res;
    }

    /**
     * 人脸注册
     */
    public String faceRegister(String image, String imageType, String groupId, String userId, String userInfo) {
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("liveness_control", "LOW");
        options.put("userInfo", userInfo);
        // 人脸注册
        JSONObject res = client.addUser(image, imageType, groupId, userId, options);
        try {
            return res.toString(2);
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 人脸搜索
     */
    public JSONObject faceSearch(String image, String imageType, String groupId, String userId) {
        HashMap<String, String> options = new HashMap<String, String>();
        // 指定user_id进行比对,即人脸认证功能
        options.put("user_id", userId);
        options.put("liveness_control", "LOW");
        // 人脸搜索
        JSONObject res = client.search(image, imageType, groupId, options);
        return res;
    }

    /**
     * 删除人脸
     */
    public void faceDelete(String groupId, String userId, String faceToken) {
        HashMap<String, String> options = new HashMap<String, String>();
        // 人脸删除
        JSONObject res = client.faceDelete(userId, groupId, faceToken, options);
    }

    /**
     * 验证人脸质量
     */
    private boolean checkQuality(JSONObject quality) throws JSONException {
        // 验证遮挡范围
        JSONObject occlusion = quality.getJSONObject("occlusion");
        double leftEye = occlusion.getDouble("left_eye");
        if (leftEye > 0.6) {
            return false;
        }
        double rightEye = occlusion.getDouble("right_eye");
        if (rightEye > 0.6) {
            return false;
        }
        double nose = occlusion.getDouble("nose");
        if (nose > 0.7) {
            return false;
        }
        double mouth = occlusion.getDouble("mouth");
        if (mouth > 0.7) {
            return false;
        }
        double leftCheck = occlusion.getDouble("left_cheek");
        if (leftCheck > 0.8) {
            return false;
        }
        double rightCheck = occlusion.getDouble("right_cheek");
        if (rightCheck > 0.8) {
            return false;
        }
        double chinContour = occlusion.getDouble("chin_contour");
        if (chinContour > 0.6) {
            return false;
        }

        // 模糊度
        double blur = quality.getDouble("blur");
        if (blur > 0.7) {
            return false;
        }

        // 光照范围
        double illumination = quality.getDouble("illumination");
        if (illumination < 40) {
            return false;
        }

        // 人脸完整度
        double completeness = quality.getInt("completeness");
        if (completeness == 0) {
            return false;
        }
        return true;
    }

    /**
     * 验证姿态
     */
    private boolean checkAttitude(JSONObject face_list) throws JSONException {
        // 姿态角度
        JSONObject angle = face_list.getJSONObject("angle");
        double roll = angle.getDouble("roll");
        if (roll <= -20 || roll >= 20) {
            return false;
        }
        double pitch = angle.getDouble("pitch");
        if (pitch <= -20 || pitch >= 20) {
            return false;
        }
        double yaw = angle.getDouble("yaw");
        if (yaw <= -20 || yaw >= 20) {
            return false;
        }

        // 人脸大小
        JSONObject location = face_list.getJSONObject("location");
        int width = location.getInt("width");
        int height = location.getInt("height");
        if (width < 100 || height < 100) {
            return false;
        }
        return true;
    }

这样准备基本可以啦!

那么我们怎么检测图片呢?

我们首先要找到一张图片

嗯,还得是人,这里我们采用最方便的url访问方式

我在网上找了一张乔妹的照片

我们来看看她的颜值得分吧!URL如下:

http://5b0988e595225.cdn.sohucs.com/images/20180604/cf9acd5afbe147c2957bf1d5989335d6.jpeg

上一下完整版的代码:

package com.carelonger.api.util.baidu;

import com.baidu.aip.face.AipFace;
import com.carelonger.api.job.Jobs;
import com.carelonger.api.util.common.MyLog;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * 百度人脸识别处理核心
 * 作者:王大傻
 * 时间:2019年9月09日17:38:13
 * 版本:v0.2
 */
public class FaceUtil {

    // 日志
    private final static MyLog log = MyLog.getLog(Jobs.class);

    // 认证参数
    private static final String APP_ID = "*****";
    private static final String API_KEY = "***************************";
    private static final String SECRET_KEY = "****************************";

    // 检测人脸附加参数
    private static final String FACE_FIELD = "quality,type,beauty,age";

    // 全局返回状态码
    private static final String SUCCESS = "SUCCESS";

    private static AipFace client = null;

    /**
     * 初始化
     *
     * @return
     */
    static {
        client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
        // 可选:设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);// 建立连接的超时时间(单位:毫秒)
        client.setSocketTimeoutInMillis(60000);// 通过打开的连接传输数据的超时时间(单位:毫秒)
        // 可选:设置代理服务器地址, http和socket二选一,或者均不设置
        // client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
        // client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理
    }

    /**
     * 人脸检测
     */
    public JSONObject faceUpload(String image, String imageType) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("face_field", FACE_FIELD);
        options.put("liveness_control", "LOW");// 活体
        // 发起调用
        JSONObject res = client.detect(image, imageType, options);
        return res;
    }

    /**
     * 解析人脸上传结果
     */
    public JSONObject faceCheck(JSONObject res) throws JSONException {
        // 判断结果
        if (!SUCCESS.equals(res.getString("error_msg"))) {
            res.remove("error_msg");
            res.put("error_msg", "无法识别,请重试!");
            return res;
        }
        JSONObject face_list = res.getJSONObject("result").getJSONArray("face_list").getJSONObject(0);
        // 验证人脸质量
        JSONObject quality = face_list.getJSONObject("quality");
        boolean checkQuality = checkQuality(quality);
        // 验证姿态
        boolean checkAttitude = checkAttitude(face_list);
        if (!(checkQuality && checkAttitude)) {
            res.remove("error_msg");
            res.put("error_msg", "无法识别,请重试!");
            return res;
        }
        return res;
    }

    /**
     * 人脸注册
     */
    public String faceRegister(String image, String imageType, String groupId, String userId, String userInfo) {
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("liveness_control", "LOW");
        options.put("userInfo", userInfo);
        // 人脸注册
        JSONObject res = client.addUser(image, imageType, groupId, userId, options);
        try {
            return res.toString(2);
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 人脸搜索
     */
    public JSONObject faceSearch(String image, String imageType, String groupId, String userId) {
        HashMap<String, String> options = new HashMap<String, String>();
        // 指定user_id进行比对,即人脸认证功能
        options.put("user_id", userId);
        options.put("liveness_control", "LOW");
        // 人脸搜索
        JSONObject res = client.search(image, imageType, groupId, options);
        return res;
    }

    /**
     * 删除人脸
     */
    public void faceDelete(String groupId, String userId, String faceToken) {
        HashMap<String, String> options = new HashMap<String, String>();
        // 人脸删除
        JSONObject res = client.faceDelete(userId, groupId, faceToken, options);
    }

    /**
     * 验证人脸质量
     */
    private boolean checkQuality(JSONObject quality) throws JSONException {
        // 验证遮挡范围
        JSONObject occlusion = quality.getJSONObject("occlusion");
        double leftEye = occlusion.getDouble("left_eye");
        if (leftEye > 0.6) {
            return false;
        }
        double rightEye = occlusion.getDouble("right_eye");
        if (rightEye > 0.6) {
            return false;
        }
        double nose = occlusion.getDouble("nose");
        if (nose > 0.7) {
            return false;
        }
        double mouth = occlusion.getDouble("mouth");
        if (mouth > 0.7) {
            return false;
        }
        double leftCheck = occlusion.getDouble("left_cheek");
        if (leftCheck > 0.8) {
            return false;
        }
        double rightCheck = occlusion.getDouble("right_cheek");
        if (rightCheck > 0.8) {
            return false;
        }
        double chinContour = occlusion.getDouble("chin_contour");
        if (chinContour > 0.6) {
            return false;
        }

        // 模糊度
        double blur = quality.getDouble("blur");
        if (blur > 0.7) {
            return false;
        }

        // 光照范围
        double illumination = quality.getDouble("illumination");
        if (illumination < 40) {
            return false;
        }

        // 人脸完整度
        double completeness = quality.getInt("completeness");
        if (completeness == 0) {
            return false;
        }
        return true;
    }

    /**
     * 验证姿态
     */
    private boolean checkAttitude(JSONObject face_list) throws JSONException {
        // 姿态角度
        JSONObject angle = face_list.getJSONObject("angle");
        double roll = angle.getDouble("roll");
        if (roll <= -20 || roll >= 20) {
            return false;
        }
        double pitch = angle.getDouble("pitch");
        if (pitch <= -20 || pitch >= 20) {
            return false;
        }
        double yaw = angle.getDouble("yaw");
        if (yaw <= -20 || yaw >= 20) {
            return false;
        }

        // 人脸大小
        JSONObject location = face_list.getJSONObject("location");
        int width = location.getInt("width");
        int height = location.getInt("height");
        if (width < 100 || height < 100) {
            return false;
        }
        return true;
    }

    public static void main(String[] args) throws JSONException {
        String image = "http://5b0988e595225.cdn.sohucs.com/images/20180604/cf9acd5afbe147c2957bf1d5989335d6.jpeg";
        String imageType = "URL";
        FaceUtil faceUtil = new FaceUtil();
        JSONObject jsonObject = faceUtil.faceUpload(image, imageType);
        System.out.println(jsonObject.toString(2));

        JSONObject res = faceUtil.faceCheck(jsonObject);
//        System.out.println(res.getString("error_msg"));
        JSONObject face_list = jsonObject.getJSONObject("result").getJSONArray("face_list").getJSONObject(0);
        if (SUCCESS.equals(res.get("error_msg"))) {
            String s = faceUtil.faceSearch(face_list.getString("face_token"), "FACE_TOKEN", "test", "999").toString(2);
            System.out.println(s);
        }
    }
}

结果如下图所示:

c1d442376c64a9ef0eab59319b376fad.png

当然啦,身为一个妹子肯定会测自己的,但是结果我是不会贴上来的~哈哈哈

就到这里吧~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值