• 你的第一个人脸识别demo


    1、成为开发者

    百度ai官网地址:http://ai.baidu.com/

    STEP1:点击百度AI开放平台导航右侧的控制台,点击需要使用的AI服务项,使用百度账号登录。如还未持有百度账户,可以点击此处注册百度账户
    STEP2:首次使用,登录后将会进入开发者认证页面,请填写相关信息完成开发者认证。注:(如您之前已经是百度云用户或百度开发者中心用户,此步可略过。)
    STEP3:进入具体AI服务项的控制面板(如文字识别、人脸识别),进行相关业务操作。

    2. 创建应用

    创建好账号后,在正式调用AI能力之前,需要您创建一下应用,这个是调用服务的基础能力单元,不可或缺哦。在百度云控制台,已默认为您免费开通百度语音、文字识别、人脸识别、自然语言处理、图像审核、知识图谱服务六项服务。您可以选择您需要的服务,进入对应服务的控制台中。进入某个服务的控制台后,您将首先见到此服务的控制台概览页(以人脸识别为例),如下图所示,点击创建应用:

     

    点击上图中的「创建应用」,即可进入应用创建界面,如下图所示:

     

    点击创建就会生成一个应用,包含API Key和SecretKey,这两个字段是获取accessToken的依据,是调用api接口的必备字段:

    3、获取签名

    获取accessToken:通过调用接口:https://aip.baidubce.com/oauth/2.0/token?
        grant_type=client_credentials&
        client_id=Va5yQRHlA4Fq4eR3LT0vuXV4&
        client_secret= 0rDSjzQ20XUj5itV7WRtznPQSzr5pVw2&获取;

    4、进行开发

    首先需要下载开发必须用到的jar文件,引入项目:

    然后编写调用接口的编码,其中用到的文件操作,base64编码处理以及http请求相关的util文件,百度都已经封装好,可以直接使用:

    package com.hengyunsoft.face.aip;
    
    import java.net.URLEncoder;
    
    import com.baidu.aip.util.Base64Util;
    import com.hengyunsoft.face.util.AuthService;
    import com.hengyunsoft.face.util.FileUtil;
    import com.hengyunsoft.face.util.HttpUtil;
    
    /**
    * 人脸探测
    */
    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 + "&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) {
            //testDetect1();
            //测试人脸检测
            String path = System.getProperty("user.dir")+"\image\person3.jpg";
            FaceDetect.detect(path);
    
        }

     原图:

    检测结果:

    {"result_num":3,"result":[{"location":{"left":270,"top":48,"width":42,"height":39},"face_probability":1,"rotation_angle":-12,"yaw":-9.428524017334,"pitch":-1.9272558689117,"roll":-14.768548965454,"landmark":[{"x":288,"y":50},{"x":305,"y":46},{"x":300,"y":58},{"x":302,"y":69}],"landmark72":[{"x":272,"y":57},{"x":274,"y":63},{"x":278,"y":70},{"x":283,"y":76},{"x":290,"y":80},{"x":298,"y":81},{"x":305,"y":80},{"x":311,"y":77},{"x":314,"y":71},{"x":315,"y":65},{"x":315,"y":58},{"x":314,"y":52},{"x":313,"y":46},{"x":283,"y":52},{"x":285,"y":50},{"x":287,"y":49},{"x":290,"y":49},{"x":292,"y":51},{"x":290,"y":51},{"x":288,"y":52},{"x":286,"y":52},{"x":288,"y":50},{"x":279,"y":50},{"x":281,"y":47},{"x":285,"y":45},{"x":289,"y":45},{"x":292,"y":46},{"x":289,"y":47},{"x":285,"y":47},{"x":282,"y":48},{"x":301,"y":48},{"x":303,"y":46},{"x":305,"y":45},{"x":307,"y":45},{"x":309,"y":46},{"x":307,"y":47},{"x":305,"y":48},{"x":303,"y":48},{"x":305,"y":46},{"x":299,"y":44},{"x":301,"y":42},{"x":304,"y":41},{"x":307,"y":41},{"x":310,"y":42},{"x":307,"y":42},{"x":305,"y":43},{"x":302,"y":44},{"x":294,"y":50},{"x":295,"y":54},{"x":295,"y":57},{"x":295,"y":62},{"x":298,"y":61},{"x":303,"y":60},{"x":305,"y":60},{"x":303,"y":56},{"x":301,"y":52},{"x":299,"y":49},{"x":300,"y":58},{"x":295,"y":71},{"x":298,"y":68},{"x":302,"y":66},{"x":305,"y":66},{"x":308,"y":68},{"x":306,"y":70},{"x":303,"y":71},{"x":299,"y":71},{"x":298,"y":69},{"x":302,"y":68},{"x":305,"y":68},{"x":305,"y":68},{"x":302,"y":69},{"x":299,"y":70}],"age":38.027809143066,"beauty":24.773380279541,"expression":0,"expression_probablity":0.99999582767487,"faceshape":[{"type":"square","probability":0.53995013237},{"type":"triangle","probability":0.003084430238232},{"type":"oval","probability":0.35069698095322},{"type":"heart","probability":0.0041946368291974},{"type":"round","probability":0.10207384079695}],"gender":"male","gender_probability":0.99982494115829,"glasses":0,"glasses_probability":0.99990594387054,"race":"yellow","race_probability":0.99971705675125,"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.99693930149078,"cartoon":0.0030607103835791}}},{"location":{"left":44,"top":50,"width":40,"height":42},"face_probability":1,"rotation_angle":-4,"yaw":13.170437812805,"pitch":2.0879020690918,"roll":-2.8054466247559,"landmark":[{"x":51,"y":55},{"x":70,"y":54},{"x":58,"y":67},{"x":61,"y":78}],"landmark72":[{"x":45,"y":58},{"x":45,"y":64},{"x":47,"y":70},{"x":49,"y":76},{"x":52,"y":82},{"x":56,"y":88},{"x":62,"y":91},{"x":69,"y":89},{"x":77,"y":85},{"x":82,"y":77},{"x":85,"y":69},{"x":85,"y":62},{"x":85,"y":54},{"x":47,"y":56},{"x":49,"y":55},{"x":50,"y":54},{"x":53,"y":54},{"x":55,"y":56},{"x":53,"y":56},{"x":51,"y":57},{"x":49,"y":57},{"x":51,"y":55},{"x":45,"y":52},{"x":47,"y":50},{"x":49,"y":50},{"x":52,"y":50},{"x":55,"y":52},{"x":52,"y":52},{"x":49,"y":52},{"x":47,"y":52},{"x":65,"y":55},{"x":67,"y":53},{"x":69,"y":52},{"x":71,"y":53},{"x":74,"y":54},{"x":72,"y":55},{"x":69,"y":55},{"x":67,"y":55},{"x":70,"y":54},{"x":62,"y":51},{"x":65,"y":49},{"x":68,"y":48},{"x":72,"y":48},{"x":76,"y":50},{"x":72,"y":50},{"x":69,"y":50},{"x":65,"y":51},{"x":57,"y":56},{"x":56,"y":60},{"x":55,"y":65},{"x":54,"y":69},{"x":57,"y":70},{"x":62,"y":69},{"x":65,"y":69},{"x":64,"y":64},{"x":63,"y":60},{"x":62,"y":56},{"x":58,"y":67},{"x":55,"y":78},{"x":57,"y":76},{"x":60,"y":75},{"x":64,"y":76},{"x":68,"y":78},{"x":64,"y":80},{"x":60,"y":81},{"x":57,"y":80},{"x":57,"y":78},{"x":60,"y":77},{"x":64,"y":77},{"x":64,"y":78},{"x":60,"y":78},{"x":57,"y":78}],"age":33.576271057129,"beauty":24.980932235718,"expression":0,"expression_probablity":0.99999821186066,"faceshape":[{"type":"square","probability":0.21030895411968},{"type":"triangle","probability":0.00028864710475318},{"type":"oval","probability":0.69324284791946},{"type":"heart","probability":0.038888353854418},{"type":"round","probability":0.057271171361208}],"gender":"male","gender_probability":0.99993813037872,"glasses":0,"glasses_probability":0.99995875358582,"race":"yellow","race_probability":0.99995911121368,"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.9932644367218,"cartoon":0.0067355595529079}}},{"location":{"left":163,"top":54,"width":37,"height":42},"face_probability":1,"rotation_angle":-1,"yaw":-7.0411062240601,"pitch":-8.2431039810181,"roll":-2.4302639961243,"landmark":[{"x":176,"y":63},{"x":192,"y":62},{"x":186,"y":72},{"x":185,"y":82}],"landmark72":[{"x":163,"y":64},{"x":164,"y":71},{"x":165,"y":77},{"x":167,"y":84},{"x":172,"y":90},{"x":179,"y":94},{"x":186,"y":95},{"x":192,"y":93},{"x":196,"y":87},{"x":199,"y":81},{"x":200,"y":75},{"x":201,"y":69},{"x":200,"y":63},{"x":172,"y":63},{"x":174,"y":62},{"x":176,"y":61},{"x":178,"y":62},{"x":180,"y":63},{"x":178,"y":63},{"x":176,"y":64},{"x":174,"y":64},{"x":176,"y":63},{"x":169,"y":58},{"x":171,"y":54},{"x":174,"y":53},{"x":178,"y":53},{"x":181,"y":55},{"x":178,"y":55},{"x":175,"y":55},{"x":172,"y":56},{"x":189,"y":63},{"x":191,"y":61},{"x":193,"y":61},{"x":194,"y":61},{"x":196,"y":62},{"x":194,"y":63},{"x":193,"y":63},{"x":191,"y":63},{"x":192,"y":62},{"x":189,"y":55},{"x":192,"y":52},{"x":194,"y":52},{"x":197,"y":54},{"x":198,"y":57},{"x":196,"y":55},{"x":194,"y":55},{"x":192,"y":55},{"x":183,"y":63},{"x":182,"y":67},{"x":181,"y":70},{"x":180,"y":74},{"x":183,"y":74},{"x":189,"y":74},{"x":191,"y":73},{"x":189,"y":70},{"x":188,"y":66},{"x":187,"y":63},{"x":186,"y":72},{"x":176,"y":81},{"x":181,"y":79},{"x":186,"y":79},{"x":190,"y":79},{"x":193,"y":80},{"x":190,"y":84},{"x":186,"y":86},{"x":180,"y":84},{"x":181,"y":80},{"x":186,"y":80},{"x":190,"y":80},{"x":190,"y":83},{"x":186,"y":84},{"x":181,"y":83}],"age":48.371246337891,"beauty":20.235898971558,"expression":1,"expression_probablity":0.98125779628754,"faceshape":[{"type":"square","probability":0.043237078934908},{"type":"triangle","probability":0.0095938919112086},{"type":"oval","probability":0.30381575226784},{"type":"heart","probability":0.072911836206913},{"type":"round","probability":0.57044142484665}],"gender":"male","gender_probability":0.99851101636887,"glasses":0,"glasses_probability":0.99994230270386,"race":"yellow","race_probability":0.99906295537949,"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.96879607439041,"cartoon":0.031203903257847}}}],"log_id":3532732135}

    关于人脸识别的一些接口:注册,更新,删除,检测,识别,验证等:

    package com.hengyunsoft.face.aip;
    
    import java.util.HashMap;
    
    import org.json.JSONObject;
    
    import com.baidu.aip.face.AipFace;
    
    public class FaceManage {
        
        /**
         * 注册用户信息
         * @param client
         * @param imagePath
         */
        public static void addUser(AipFace client,String imagePath) {
            // 传入可选参数调用接口
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("action_type", "replace");
    
            String uid = "user1";
            String userInfo = "user's info";
            String groupId = "group1,group2";
    
            JSONObject res = client.addUser(uid, userInfo, groupId, imagePath, options);
            System.out.println(res.toString(2));
    
            // 参数为本地图片二进制数组
            // byte[] file = readImageFile(image);
            //res = client.addUser(file, uid, userInfo, groupId, options);
            //System.out.println(res.toString(2));
        }
        
        /**
         * 更新用户信息,包括图片
         * @param client
         * @param imagePath
         */
        public static void updateUser(AipFace client,String imagePath) {
            // 传入可选参数调用接口
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("action_type", "replace");
    
            String uid = "user1";
            String userInfo = "user's info";
            String groupId = "group1";
    
            JSONObject res = client.updateUser(uid, userInfo, groupId, imagePath, options);
            System.out.println(res.toString(2));
    
            // 参数为本地图片二进制数组
    //        byte[] file = readImageFile(image);
    //        res = client.updateUser(file, uid, userInfo, groupId, options);
    //        System.out.println(res.toString(2));
        }
        
        /**
         * 删除用户信息
         * @param client
         */
        public static void deleteUser(AipFace client) {
             // 传入可选参数调用接口
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("group_id", "group1");
    
            String uid = "user1";
    
            // 人脸删除
            JSONObject res = client.deleteUser(uid, options);
            System.out.println(res.toString(2));
        }
        
        /**
         * 根据用户id和group信息获取用户详情
         * @param client
         */
        public static void getUserById(AipFace client) {
             // 传入可选参数调用接口
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("group_id", "group1");
    
            String uid = "user1";
    
            // 用户信息查询
            JSONObject res = client.getUser(uid, options);
            System.out.println(res.toString(2));
        }
        
        /**
         * 用户组查询
         * @param client
         */
        public static void getGroupList(AipFace client) {
            // 传入可选参数调用接口
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("start", "0");
            options.put("end", "50");
    
            // 组列表查询
            JSONObject res = client.getGroupList(options);
            System.out.println(res.toString(2));
    
        }
        
        /**
         * 用户组内用户列表查询
         * @param client
         */
        public static void getGroupUsers(AipFace client) {
            // 传入可选参数调用接口
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("start", "0");
            options.put("end", "50");
    
            String groupId = "group1";
    
            // 组内用户列表查询
            JSONObject res = client.getGroupUsers(groupId, options);
            System.out.println(res.toString(2));
    
        }
        
        /**
         * 人脸验证,用于登录,签到等
         * @param client
         */
        public static void verifyUser(AipFace client,String imagePath) {
            // 传入可选参数调用接口
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("top_num", "1");
            options.put("ext_fields", "faceliveness");
    
            String uid = "user1";
            String groupId = "group1,group2";
    
            JSONObject res = client.verifyUser(uid, groupId, imagePath, options);
            System.out.println(res.toString(2));
    
    //        // 参数为本地图片二进制数组
    //        byte[] file = readImageFile(image);
    //        res = client.verifyUser(file, uid, groupId, options);
    //        System.out.println(res.toString(2));
        }
        
        /**
         * 人脸识别,用于闸机,签到
         * @param client
         */
        public static void identifyUser(AipFace client,String imagePath) {
            // 传入可选参数调用接口
            HashMap<String, String> options = new HashMap<String, String>();
            options.put("ext_fields", "faceliveness");
            options.put("user_top_num", "3");
    
            String groupId = "group1,group2";
    
            JSONObject res = client.identifyUser(groupId, imagePath, options);
            System.out.println(res.toString(2));
    
    //        // 参数为本地图片二进制数组
    //        byte[] file = readImageFile(image);
    //        res = client.identifyUser(file, groupId, options);
    //        System.out.println(res.toString(2));
        }
    }

    测试代码:

    package com.hengyunsoft.face.main;
    
    import java.util.HashMap;
    
    import org.json.JSONObject;
    
    import com.baidu.aip.face.AipFace;
    import com.hengyunsoft.face.aip.FaceDetect;
    import com.hengyunsoft.face.aip.FaceManage;
    
    public class TestFace {
         //设置APPID/AK/SK
        public static final String APP_ID = "10580034";
        public static final String API_KEY = "你的api key";
        public static final String SECRET_KEY = "你的secret key";
        private static String basePath = System.getProperty("user.dir")+"\image\";
    
        public static void main(String[] args) {
            
            AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
            String path = System.getProperty("user.dir")+"\image\person3.jpg";
            String image = basePath;
            
            //测试人脸检测
            //FaceDetect.detect(path);
            
            //测试添加
            // 参数为本地图片路径
    //        image = basePath + "liushishi.jpg";
    //        FaceManage.addUser(client,image);
            //测试更新
            // 参数为本地图片路径
    //        image = basePath + "zhoubichang.jpg";
    //        FaceManage.updateUser(client,image);
            //测试删除
    //        FaceManage.deleteUser(client);
            
            
    //        //用户查询,根据id查询
    //        FaceManage.getUserById(client);
    //        
    //        //用户组列表查询
    //        FaceManage.getGroupList(client);
            
    //        //用户组内用户列表查询
    //        FaceManage.getGroupUsers(client);
            //测试人脸验证
            image = basePath + "liushishi.jpg";
            FaceManage.verifyUser(client, image);
            
    //        //测试人脸识别
    //        image = basePath + "liushishi.jpg";
    //        FaceManage.identifyUser(client, image);
            
            
    
        }
    
        private static void testDetect1() {
            // 初始化一个AipFace
            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代理
    
            // 调用接口
            String path = System.getProperty("user.dir")+"\image\test.png";
            JSONObject res = client.detect(path, new HashMap<String, String>());
            System.out.println(res.toString(2));
        }
    
    }

     

     

     

     

  • 相关阅读:
    windows下nginx以服务自启动
    redis数据库可视化工具(RedisDesktopManager)
    myecplise中的svn如何切换账号
    oracle创建视图包含clob字段,报错:数据类型不一致:应为-,但却获得CLOB
    java.lang.UnsatisfiedLinkError: no jacob-1.18-x64 in java.library.path
    java中关于日期的处理
    js截取字符串
    关于sql developer中表或视图不存在以及查找日志窗口
    在html页面切换标题栏目时出现页面抖动
    严重: IOException while loading persisted sessions: java.io.EOFException
  • 原文地址:https://www.cnblogs.com/guo-eric/p/8109411.html
Copyright © 2020-2023  润新知