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 }
