赞
踩
(1)maven包:
- <!-- https://mvnrepository.com/artifact/com.baidu.aip/java-sdk -->
- <dependency>
- <groupId>com.baidu.aip</groupId>
- <artifactId>java-sdk</artifactId>
- <version>4.15.1</version>
- </dependency>
(1)配置类(需要去百度人脸识别官网注册账号获取appid和密匙对):BaiduFaceConfig
- /**
- * @qq 1393180819
- * @author wuguixin
- */
- package utils.baidu;
-
-
- import com.baidu.aip.face.AipFace;
- import com.baidu.aip.face.FaceVerifyRequest;
- import com.baidu.aip.face.MatchRequest;
- import org.json.JSONArray;
- import org.json.JSONObject;
-
- import java.util.ArrayList;
- import java.util.HashMap;
-
- public class BaiduFaceConfig {
-
- //设置APPID/AK/SK
- public static String APP_ID = null;
- public static String API_KEY = null;
- public static String SECRET_KEY = null;
-
- private static AipFace client = null;
-
- public static final String IMG_TYPE_BASE64 = "BASE64";
- public static final String IMG_TYPE_URL = "URL";
- public static final String IMG_TYPE_FACE_TOKEN = "FACE_TOKEN";
-
-
- public static void createClient(){
- if(null==client){
- client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
- }
- }
-
-
-
-
- // 人脸检测
- public static JSONObject detectFace(String image, String imageType){
- createClient();
- // 可选:设置网络连接参数
- client.setConnectionTimeoutInMillis(2000);
- client.setSocketTimeoutInMillis(60000);
- // 可选:设置代理服务器地址, http和socket二选一,或者均不设置
- //client.setHttpProxy("proxy_host", proxy_port); // 设置http代理
- //client.setSocketProxy("proxy_host", proxy_port); // 设置socket代理
- // 可选:设置log4j日志输出格式,若不设置,则使用默认配置
- // 也可以直接通过jvm启动参数设置此环境变量
- //System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");
- // 传入可选参数调用接口
- HashMap<String, String> options = new HashMap<>();
- options.put("face_field", "quality"); // 选择可返回字段
- //options.put("max_face_num", "2");
- //options.put("face_type", "LIVE");
- // 图片大小应小于10M
- // 取决于image_type参数,传入BASE64字符串或URL字符串或FACE_TOKEN字符串
- JSONObject res = client.detect(image, imageType, options);
- System.out.println(res);
- return res;
- }
-
- /*Base64.encode(BaiduFaceUtils.File2byte(file))*/
- // 在线活体检测
- public static JSONObject detectLivingFace(String image,String imageType) {
- createClient();
- FaceVerifyRequest req = new FaceVerifyRequest(image, imageType);
- ArrayList<FaceVerifyRequest> list = new ArrayList<FaceVerifyRequest>();
- list.add(req);
- JSONObject res = client.faceverify(list);
- System.out.println(res.toString(2));
- return res;
- }
-
-
-
-
- // 人脸对比
- public static JSONObject matchFace(String image1,String image2,String imageType){
- createClient();
- MatchRequest req1 = new MatchRequest(image1, imageType);
- MatchRequest req2 = new MatchRequest(image2, imageType);
- ArrayList<MatchRequest> requests = new ArrayList<MatchRequest>();
- requests.add(req1);
- requests.add(req2);
- JSONObject res = client.match(requests);
- System
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。