1 package com.jf.face;
2
3
4 import java.util.HashMap;
5 import org.json.JSONObject;
6 import com.baidu.aip.face.AipFace;
7
8
9 public class Sample {
10 //设置APPID/AK/SK
11 public static final String APP_ID = "111xxxxx";
12 public static final String API_KEY = "tjStp4vNfXB9oxxxxxxxxxx";
13 public static final String SECRET_KEY = "iEkdRbG89Igxxxxxxxxxx";
14
15
16 public static void main(String[] args) {
17 // 初始化一个AipFace
18 AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
19
20
21 // 可选:设置网络连接参数
22 client.setConnectionTimeoutInMillis(2000);
23 client.setSocketTimeoutInMillis(60000);
24
25
26 // // 可选:设置代理服务器地址, http和socket二选一,或者均不设置
27 // client.setHttpProxy("ieproxy.xxxx.com", 80); // 设置http代理
28 // client.setSocketProxy("proxy_host", 80); // 设置socket代理
29 // // 可选:设置log4j日志输出格式,若不设置,则使用默认配置
30 // // 也可以直接通过jvm启动参数设置此环境变量
31 // System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");
32
33
34 //----------------------------------------
35 // // 调用接口 单张照片人脸分析
36 // String path = "D:/IMG_0561.JPG";
37 // HashMap<String, String> options = new HashMap<String, String>();
38 // options.put("max_face_num", "2");
39 // options.put("face_fields", "age,beauty");
40 //
41 // JSONObject res = client.detect(path, options);
42 // System.out.println(res.toString(2));
43 //----------------------------------------
44 // // 调用接口 两张照片对比
45 // HashMap<String, String> options = new HashMap<String, String>();
46 //// options.put("image_liveness", "faceliveness,faceliveness"); //对两张照片都做活体检测
47 // String path1 = "D:/IMG_0598.JPG";
48 // String path2 = "D:/IMG_0317.JPG";
49 // ArrayList<String> images = new ArrayList<String>();
50 // images.add(path1);
51 // images.add(path2);
52 // JSONObject res = client.match(images, options);
53 // System.out.println(res.toString(2));
54 //----------------------------------------
55 // //人脸库写入
56 // HashMap<String, String> options = new HashMap<String, String>();
57 // options.put("action_type", "replace");
58 //
59 // String uid = "002";
60 // String userInfo = "哈哈哈";
61 // String groupId = "001";
62 //
63 // // 参数为本地图片路径
64 // String image = "D:/IMG_0598.JPG";
65 // JSONObject res = client.addUser(uid, userInfo, groupId, image, options);
66 // System.out.println(res.toString(2));
67 //----------------------------------------
68 //人脸识别 应用场景:如人脸闸机,考勤签到,安防监控等
69 HashMap<String, String> options = new HashMap<String, String>();
70 options.put("ext_fields", "faceliveness");
71 options.put("user_top_num", "1");
72 String groupId = "001";
73 // 参数为本地图片路径
74 String image = "D:/1506089277306.png";
75 JSONObject res = client.identifyUser(groupId, image, options);
76 System.out.println(res.toString(2));
77 //----------------------------------------
78 // //人脸认证 应用场景:如人脸登录,人脸签到等
79 // HashMap<String, String> options = new HashMap<String, String>();
80 // options.put("top_num", "3");
81 // options.put("ext_fields", "faceliveness");
82 //
83 // String uid = "001";
84 // String groupId = "001";
85 //
86 // // 参数为本地图片路径
87 // String image = "D:/1506089277306.png";
88 // JSONObject res = client.verifyUser(uid, groupId, image, options);
89 // System.out.println(res.toString(2));
90 }
91 }